Skip to content

Return the TLS write buffer when TCP client connection socket setup fails - #4941

Open
pepone wants to merge 1 commit into
icerpc:mainfrom
pepone:fix/tcp-connection-ctor-cleanup
Open

Return the TLS write buffer when TCP client connection socket setup fails#4941
pepone wants to merge 1 commit into
icerpc:mainfrom
pepone:fix/tcp-connection-ctor-cleanup

Conversation

@pepone

@pepone pepone commented Sep 11, 2026

Copy link
Copy Markdown
Member

When the TcpClientConnection constructor failed to bind or configure its socket (for example a LocalNetworkAddress
the socket cannot bind), its two catch blocks disposed the socket but never returned the TLS write buffer rented in the
base-constructor initializer. Since the constructor throws, no instance escapes and Dispose(), which returns the
buffer, was never called.

The catch blocks now call Dispose() instead of Socket.Dispose(). At that point the SSL stream is still null and the
socket is not connected, so Dispose() closes the socket and returns the write buffer to the pool.

Fixes #4828

What's Changed entry

None — the default pool doesn't leak, and a client socket setup failure means every connection attempt fails.

Copilot AI lite review requested due to automatic review settings September 11, 2026 09:59
@pepone pepone added this to the 0.6.1 milestone Sep 11, 2026
@pepone pepone added ai-audit AI-generated audit finding — needs human triage core IceRPC core code not covered by another label (e.g. the icerpc protocol implementation) labels Sep 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The reviewed changes address both failure paths and include regression coverage.

Pull request overview

This pull request fixes resource leaks during failed TCP/TLS connection construction.

Changes:

  • Defers TLS write-buffer rental until socket setup succeeds.
  • Disposes sockets when buffer rental fails.
  • Adds regression tests for client and server failure paths.
File summaries
File Summary
tests/IceRpc.Tests/Transports/Tcp/TcpTransportTests.cs Adds resource-cleanup regression tests.
src/IceRpc/Transports/Tcp/Internal/TcpConnection.cs Refactors socket ownership and construction cleanup.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bernardnormier bernardnormier left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the constructors as they are. For the client leak, calling Dispose() instead of Socket.Dispose() in
the two catch blocks of the TcpClientConnection constructor returns the buffer as well: the SSL stream is still
null and closing an unconnected socket is fine. Two lines, no CreateSocket, no base constructor change.

The server path needs a pool whose Rent throws, which the default pool never does for our segment sizes. Drop that
part.

No test for this one: the leak is only observable through a pool built to count rentals, and the fix is two lines.

No What's Changed entry either: None — the default pool doesn't leak, and a client socket setup failure means every connection attempt fails.

…ails

When the TcpClientConnection constructor failed to bind or configure its socket, its catch blocks disposed the socket
but never returned the TLS write buffer rented in the base-constructor initializer. The catch blocks now call Dispose()
instead of Socket.Dispose(): the SSL stream is still null and the socket is not connected, so Dispose() closes the
socket and returns the write buffer to the pool.

Fixes icerpc#4828
@pepone
pepone force-pushed the fix/tcp-connection-ctor-cleanup branch from 58cc1da to e391a32 Compare September 11, 2026 16:22
@pepone pepone changed the title Release the socket and write buffer when TCP connection construction fails Return the TLS write buffer when TCP client connection socket setup fails Sep 11, 2026
@pepone

pepone commented Sep 11, 2026

Copy link
Copy Markdown
Member Author

🤖 Claude: Reworked as suggested in e391a32: the constructors are back as they were, the two catch blocks in the TcpClientConnection constructor now call Dispose() instead of Socket.Dispose(), and the server change, the tests, and the What's Changed entry are dropped.

@pepone
pepone requested review from bernardnormier and a balanced review from Copilot September 11, 2026 16:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

It lacks regression coverage and does not address all resource-loss paths identified by #4828.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Balanced

catch (SocketException exception)
{
Socket.Dispose();
Dispose();
catch
{
Socket.Dispose();
Dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-audit AI-generated audit finding — needs human triage core IceRPC core code not covered by another label (e.g. the icerpc protocol implementation)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Audit-Medium] TCP exceptional construction paths do not release all acquired resources

3 participants