Fix Windows IOCP TcpClient: Init() returns true even when bind fails#3
Merged
Merged
Conversation
Agent-Logs-Url: https://github.com/lmshao/lmnet/sessions/6cbdf043-c0c5-48ef-bfa2-caab65f11dac Co-authored-by: lmshao <3686112+lmshao@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
lmshao
May 10, 2026 01:09
View session
There was a problem hiding this comment.
Pull request overview
Fixes a Windows IOCP TcpClientImpl initialization bug where Init() could return success even when ReInit() failed (e.g., local bind() failure), leaving the client in an invalid-but-“running” state.
Changes:
- Ensure
ReInit()closes the socket and marks itINVALID_SOCKETon early-return error paths (bind failures, remoteinet_ptonfailure). - Make
Init()validatesocket_afterReInit()and returnfalse(withtaskQueue_->Stop()cleanup) when initialization fails.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
123
to
+133
| localAddr_.sin_family = AF_INET; | ||
| localAddr_.sin_port = htons(localPort_); | ||
| if (localIp_.empty()) { | ||
| localIp_ = "0.0.0.0"; | ||
| } | ||
| inet_pton(AF_INET, localIp_.c_str(), &localAddr_.sin_addr); | ||
|
|
||
| if (bind(socket_, (sockaddr *)&localAddr_, sizeof(localAddr_)) != 0) { | ||
| LMNET_LOGE("bind failed: %d", WSAGetLastError()); | ||
| closesocket(socket_); | ||
| socket_ = INVALID_SOCKET; |
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.
TcpTest.InitBindFailureInvalidatesClientSocketwas failing on Windows IOCP becauseInit()always returnedtrueeven when the local-addressbind()insideReInit()failed (WSAEADDRINUSE / error 10048).Root cause
ReInit()logged the bind error and returned early, but neither calledclosesocket()nor setsocket_ = INVALID_SOCKET.Init()then unconditionally setisRunning_ = trueand returnedtruewith a live (unbound) socket — violating the expected post-condition.The same missing cleanup existed on the
inet_ptonremote-address failure path.Changes —
src/platforms/windows/tcp_client_impl.cppReInit()— every early-return error path now callsclosesocket(socket_)and setssocket_ = INVALID_SOCKETbefore returning:bind()failureinet_ptonremote-address failure(IOCP association failure already did this correctly — now consistent throughout)
Init()— checkssocket_ == INVALID_SOCKETafterReInit()and returnsfalsewithtaskQueue_->Stop()cleanup: