Skip to content

Commit f7f278d

Browse files
Dendi Suhubdyclaude
andcommitted
Fix TcpAcceptor not setting connection state after accept
Both sync and async accept methods were accepting directly into conn->socket() without calling conn->accept(), leaving the connection state as Disconnected. This caused read() to immediately return NotConnected for every accepted connection. Fix: accept into a separate socket, then call conn->accept(socket) which properly sets state to Connected. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 78c391e commit f7f278d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/net/acceptor.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ std::expected<std::shared_ptr<TcpConnection>, AcceptorError> TcpAcceptor::accept
9595
}
9696

9797
try {
98-
auto conn = std::make_shared<TcpConnection>(io_context_);
9998
boost::system::error_code ec;
100-
acceptor_.accept(conn->socket(), ec);
99+
tcp::socket socket(io_context_);
100+
acceptor_.accept(socket, ec);
101101
if (ec) {
102102
return std::unexpected(AcceptorError::AcceptFailed);
103103
}
104+
auto conn = std::make_shared<TcpConnection>(io_context_);
105+
conn->accept(std::move(socket));
104106
return conn;
105107
} catch (...) {
106108
return std::unexpected(AcceptorError::AcceptFailed);
@@ -113,11 +115,13 @@ void TcpAcceptor::async_accept(AcceptHandler handler) {
113115
return;
114116
}
115117

116-
auto conn = std::make_shared<TcpConnection>(io_context_);
117-
acceptor_.async_accept(conn->socket(), [conn, handler](boost::system::error_code ec) {
118+
auto socket = std::make_shared<tcp::socket>(io_context_);
119+
acceptor_.async_accept(*socket, [this, socket, handler](boost::system::error_code ec) {
118120
if (ec) {
119121
handler(std::unexpected(AcceptorError::AcceptFailed));
120122
} else {
123+
auto conn = std::make_shared<TcpConnection>(io_context_);
124+
conn->accept(std::move(*socket));
121125
handler(conn);
122126
}
123127
});

0 commit comments

Comments
 (0)