Skip to content

net: posix: make socket shutdown abort a retried connect - #329

Merged
WillemKauf merged 1 commit into
redpanda-data:v26.3.xfrom
WillemKauf:willem/sticky-socket-shutdown
Nov 18, 2026
Merged

WillemKauf merged 1 commit into
redpanda-data:v26.3.xfrom
WillemKauf:willem/sticky-socket-shutdown

Conversation

@WillemKauf

@WillemKauf WillemKauf commented Nov 17, 2026 •

Copy link
Copy Markdown

posix_socket_impl::shutdown() only shut down the file descriptor the socket held at that moment. find_port_and_connect() retries on a new one after a local port collision, so a shutdown that landed between two attempts did not stop the retry. Instead, it went on connecting as if no shutdown had been requested.

Remember a shutdown issued while connect() is in progress, and fail with ECONNABORTED instead of retrying. connect() clears it when it starts, so a shutdown() before connect() is still a no-op.

Found by investigating reported LSan leaks in https://redpandadata.atlassian.net/browse/CORE-18210.

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

The test helper can exhaust a common 1,024-descriptor limit before exercising shutdown, so it must explicitly skip on descriptor exhaustion or avoid reserving the full socket range.

1 open issue
What changed in this PR

This PR makes POSIX socket shutdown persist across connection retries, preventing a new attempt from continuing after the caller has shut down the socket.

Changes:

  • Track shutdown state and reject subsequent connection attempts with ECONNABORTED.
  • Add a regression test that forces local-port collisions and checks that shutdown resolves the pending connection.
File Description
tests/​unit/​connect_test.cc Adds fixtures and a shutdown-across-port-retry regression test.
src/​net/​posix-stack.cc Records shutdown state and checks it before creating connection sockets.

Review effort: Balanced

Comment thread tests/unit/connect_test.cc Resolved
@WillemKauf

Copy link
Copy Markdown
Author

upstream: scylladb#3853

@WillemKauf
WillemKauf force-pushed the willem/sticky-socket-shutdown branch from bcb05dd to ee44a2f Compare November 17, 2026 16:17
@StephanDollberg

Copy link
Copy Markdown
Member

What's the actual flow in the code?

Is it something like:

auto background_connect = conn.connect(...);
...
conn.shutdown();

?

@WillemKauf

Copy link
Copy Markdown
Author

What's the actual flow in the code?

Is it something like:

auto background_connect = conn.connect(...);
...
conn.shutdown();

?

https://github.com/redpanda-data/streaming-enterprise/blob/179c2903c999bb3ed19e035234b5e2e5679994d7/src/v/net/dial.cc#L44-L69

ss::with_timeout() 🫠

posix_socket_impl::shutdown() only shut down the file descriptor the
socket held at that moment. find_port_and_connect() retries on a new one
after a local port collision, so a shutdown that landed between two
attempts did not stop the retry. Instead, it went on connecting as if no
shutdown had been requested.

Remember a shutdown issued while connect() is in progress, and fail with
ECONNABORTED instead of retrying. connect() clears it when it starts, so
a shutdown() before connect() is still a no-op.
@WillemKauf
WillemKauf force-pushed the willem/sticky-socket-shutdown branch from ee44a2f to 0bde9f5 Compare November 17, 2026 16:51
Comment thread src/net/posix-stack.cc
break;
} catch (std::system_error& err) {
if (port != requested_port && (err.code().value() == EADDRINUSE || err.code().value() == EADDRNOTAVAIL)) {
if (std::exchange(_shutdown, false)) {

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.

What's the thinking for the exchange? To reuse a shut down connection somehow?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, Avi pointed out on the upstream PR that shutdown(2) doesn't prevent reuse of the socket, just a shutdown of the current connection.

You can check the conversation out on the upstream PR

@WillemKauf

WillemKauf commented Nov 17, 2026 •

Copy link
Copy Markdown
Author

Approved & merged by Avi upstream

@WillemKauf
WillemKauf enabled auto-merge (rebase) November 17, 2026 22:14
@WillemKauf

Copy link
Copy Markdown
Author

auto merge is on

@WillemKauf
WillemKauf merged commit dfcbf1c into redpanda-data:v26.3.x Nov 18, 2026
16 checks passed
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.

4 participants