Add channel_->tie() in Connector::connecting() to fix lifetime safety#736
Open
Louisyoung7 wants to merge 1 commit into
Open
Add channel_->tie() in Connector::connecting() to fix lifetime safety#736Louisyoung7 wants to merge 1 commit into
Louisyoung7 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Add channel_->tie() in Connector::connecting()
Problem
The FIXME comments in Connector::connecting() indicate potential dangling
pointer issues when Channel callbacks execute:
If Connector is destroyed while Channel is handling events, callbacks like
handleWrite()/handleError()would access a danglingthispointer.Solution
Since TcpClient manages Connector via
shared_ptr<Connector>and Connectorinherits
std::enable_shared_from_this, we can safely callchannel_->tie(shared_from_this())to extend Connector's lifetime untilChannel finishes handling events.
Testing
Regression tests pass:
louis@louis-VMware-Virtual-Platform:~/OpenSourceProjects/Net/muduo/build$ cd /home/louis/OpenSourceProjects/Net/muduo/build && ./bin/tcpclient_reg1 2>&1
20260509 09:09:21.672204Z 606126 INFO TcpClient::TcpClient[TcpClient] - connector 0x64BC591356F0 - TcpClient.cc:60
20260509 09:09:21.672236Z 606126 INFO TcpClient::connect[TcpClient] - connecting to 127.0.0.1:2 - TcpClient.cc:87
20260509 09:09:21.772421Z 606126 WARN fd = 6 Channel::handle_event() POLLHUP - Channel.cc:71
20260509 09:09:21.772446Z 606126 ERROR Connector::handleError state=1 - Connector.cc:166
20260509 09:09:21.772470Z 606126 INFO Connector::retry - Retry connecting to 127.0.0.1:2 in 500 milliseconds. - Connector.cc:179
20260509 09:09:21.772532Z 606126 INFO timeout - TcpClient_reg1.cc:14
20260509 09:09:22.672373Z 606126 INFO TcpClient::
TcpClient[TcpClient] - connector 0x64BC591356F0 - TcpClient.cc:64/OpenSourceProjects/Net/muduo/build$ cd /home/louis/OpenSourceProjects/Net/muduo/build && ./bin/tcpclient_reg2 2>&1 && ./bin/tcpclient_reg3 2>&1louis@louis-VMware-Virtual-Platform:
20260509 09:09:27.463890Z 606556 DEBUG EventLoop EventLoop created 0x7FFE710C1DB0 in thread 606556 - EventLoop.cc:77
20260509 09:09:27.464049Z 606557 DEBUG Connector ctor[0x7D4594000B80] - Connector.cc:26
20260509 09:09:27.464066Z 606557 INFO TcpClient::TcpClient[TcpClient] - connector 0x7D4594000B80 - TcpClient.cc:60
20260509 09:09:27.464068Z 606557 INFO TcpClient::connect[TcpClient] - connecting to 127.0.0.1:1234 - TcpClient.cc:87
20260509 09:09:27.464160Z 606556 WARN fd = 6 Channel::handle_event() POLLHUP - Channel.cc:71
20260509 09:09:27.464176Z 606556 ERROR Connector::handleError state=1 - Connector.cc:166
20260509 09:09:27.464190Z 606556 INFO Connector::retry - Retry connecting to 127.0.0.1:1234 in 500 milliseconds. - Connector.cc:179
20260509 09:09:27.964343Z 606556 WARN fd = 6 Channel::handle_event() POLLHUP - Channel.cc:71
20260509 09:09:27.964366Z 606556 ERROR Connector::handleError state=1 - Connector.cc:166
20260509 09:09:27.964380Z 606556 INFO Connector::retry - Retry connecting to 127.0.0.1:1234 in 1000 milliseconds. - Connector.cc:179
20260509 09:09:28.464158Z 606557 INFO TcpClient::~TcpClient[TcpClient] - connector 0x7D4594000B80 - TcpClient.cc:64
20260509 09:09:28.964432Z 606556 DEBUG startInLoop do not connect - Connector.cc:45
20260509 09:09:29.464367Z 606556 DEBUG ~Connector dtor[0x7D4594000B80] - Connector.cc:30
20260509 09:09:30.463957Z 606556 DEBUG ~EventLoop EventLoop 0x7FFE710C1DB0 of thread 606556 destructs in thread 606556 - EventLoop.cc:95
20260509 09:09:30.466203Z 606723 DEBUG EventLoop EventLoop created 0x7650EC5EE980 in thread 606723 - EventLoop.cc:77
20260509 09:09:30.466287Z 606722 DEBUG Connector ctor[0x5B761DAA24B0] - Connector.cc:26
20260509 09:09:30.466312Z 606722 INFO TcpClient::TcpClient[TcpClient] - connector 0x5B761DAA24B0 - TcpClient.cc:60
20260509 09:09:30.466322Z 606722 INFO TcpClient::connect[TcpClient] - connecting to 127.0.0.1:1234 - TcpClient.cc:87
20260509 09:09:30.466413Z 606723 WARN fd = 6 Channel::handle_event() POLLHUP - Channel.cc:71
20260509 09:09:30.466424Z 606723 ERROR Connector::handleError state=1 - Connector.cc:166
20260509 09:09:30.466436Z 606723 INFO Connector::retry - Retry connecting to 127.0.0.1:1234 in 500 milliseconds. - Connector.cc:179
20260509 09:09:30.966427Z 606722 INFO TcpClient::~TcpClient[TcpClient] - connector 0x5B761DAA24B0 - TcpClient.cc:64
20260509 09:09:30.966583Z 606723 DEBUG startInLoop do not connect - Connector.cc:45
20260509 09:09:31.966521Z 606723 DEBUG ~Connector dtor[0x5B761DAA24B0] - Connector.cc:30
20260509 09:09:31.966588Z 606723 DEBUG ~EventLoop EventLoop 0x7650EC5EE980 of thread 606723 destructs in thread 606723 - EventLoop.cc:95
Diff