Skip to content

Add channel_->tie() in Connector::connecting() to fix lifetime safety#736

Open
Louisyoung7 wants to merge 1 commit into
chenshuo:masterfrom
Louisyoung7:fix-Connector
Open

Add channel_->tie() in Connector::connecting() to fix lifetime safety#736
Louisyoung7 wants to merge 1 commit into
chenshuo:masterfrom
Louisyoung7:fix-Connector

Conversation

@Louisyoung7
Copy link
Copy Markdown

Fix: Add channel_->tie() in Connector::connecting()

Problem

The FIXME comments in Connector::connecting() indicate potential dangling
pointer issues when Channel callbacks execute:

// channel_->tie(shared_from_this()); is not working,
// as channel_ is not managed by shared_ptr

If Connector is destroyed while Channel is handling events, callbacks like
handleWrite() / handleError() would access a dangling this pointer.

Solution

Since TcpClient manages Connector via shared_ptr<Connector> and Connector
inherits std::enable_shared_from_this, we can safely call
channel_->tie(shared_from_this()) to extend Connector's lifetime until
Channel 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
louis@louis-VMware-Virtual-Platform:
/OpenSourceProjects/Net/muduo/build$ cd /home/louis/OpenSourceProjects/Net/muduo/build && ./bin/tcpclient_reg2 2>&1 && ./bin/tcpclient_reg3 2>&1
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

diff --git a/muduo/net/Connector.cc b/muduo/net/Connector.cc
index 0b492ad..3fb947f 100644
--- a/muduo/net/Connector.cc
+++ b/muduo/net/Connector.cc
@@ -130,10 +130,11 @@ void Connector::connecting(int sockfd)
   setState(kConnecting);
   assert(!channel_);
   channel_.reset(new Channel(loop_, sockfd));
+  channel_->tie(shared_from_this());
   channel_->setWriteCallback(
-      std::bind(&Connector::handleWrite, this)); // FIXME: unsafe
+      std::bind(&Connector::handleWrite, this));
   channel_->setErrorCallback(
-      std::bind(&Connector::handleError, this)); // FIXME: unsafe
+      std::bind(&Connector::handleError, this));
 
   // channel_->tie(shared_from_this()); is not working,
   // as channel_ is not managed by shared_ptr
@@ -146,7 +147,7 @@ int Connector::removeAndResetChannel()
   channel_->remove();
   int sockfd = channel_->fd();
   // Can't reset channel_ here, because we are inside Channel::handleEvent
-  loop_->queueInLoop(std::bind(&Connector::resetChannel, this)); // FIXME: unsafe
+  loop_->queueInLoop(std::bind(&Connector::resetChannel, this));
   return sockfd;
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant