Skip to content

fix: call listen() once before the accept loop in PSK servers - #659

Open
MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:psk-listen-race
Open

MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:psk-listen-race

Conversation

@MarkAtwood

Copy link
Copy Markdown

Calls listen() once right after bind() in the two PSK servers that deferred it, instead of calling it on every pass of the accept loop.

psk/server-psk-nonblocking.c and psk/server-psk-threaded.c bound the listening socket and then ran wolfSSL_Init(), wolfSSL_CTX_new(), the PSK callback setup, wolfSSL_CTX_set_cipher_list() and wolfSSL_CTX_SetTmpDH_file() (which parses a DH PEM off disk) before ever reaching listen(). A TCP connect to a bound-but-not-listening port is refused immediately, so a client arriving in that window gets ECONNREFUSED and prints Connection Error from psk/client-psk.c:102. psk/server-psk.c already calls listen() directly after bind(); this makes the other two match it.

Measured on an idle 18-core x86_64 host against wolfSSL 5.9.4, time from exec until port 11111 enters LISTEN, min / median / max over 5 runs:

server before after
server-psk-nonblocking 18.4 / 19.5 / 27.3 ms 3.0 / 4.4 / 5.2 ms
server-psk-threaded 19.2 / 20.3 / 28.2 ms 3.2 / 4.4 / 4.8 ms

End to end, launching client-psk with no delay after the server, 5 trials each: before, 5/5 Connection Error on both servers; after, 2/5 and 1/5. With a 2 second delay it passes both before and after.

Deliberately not fixed here: the residual ~4 ms is process startup plus socket and bind, and no change to these examples removes it, so a caller that connects without waiting can still lose the race. Anything driving these servers should poll for readiness rather than sleep a fixed interval. This surfaced as an intermittent red in our nightly nightly-CDT-wolfssl-examples-repo-v2, whose harness does ./server-psk-nonblocking & sleep 2; that side is being fixed separately and is not part of this PR.

Two things that might otherwise look risky: calling listen() inside the accept loop was a no-op after the first iteration, so nothing depended on its position, and the fcntl(..., O_NONBLOCK) call applies to the accepted connfd rather than to listenfd, so the nonblocking behaviour is unchanged.

Testing: built both servers plus client-psk against wolfSSL 5.9.4 installed at /usr/local using the repo's psk/Makefile, no new warnings. Handshake still succeeds after the change (Server Message: I hear ya for shizzle).

server-psk-nonblocking.c and server-psk-threaded.c bound the listening
socket and then ran wolfSSL_Init(), wolfSSL_CTX_new(), the PSK callback
setup, wolfSSL_CTX_set_cipher_list() and wolfSSL_CTX_SetTmpDH_file()
before reaching listen(), which sat inside the accept loop.

A TCP connect to a bound-but-not-listening port is refused immediately,
so a client arriving in that window gets ECONNREFUSED. Measured on an
idle box, the port stayed unlistened for a median of 19.5 ms and 20.3 ms
respectively; after the move it is 4.4 ms for both.

server-psk.c already calls listen() directly after bind(). Calling it
once per accept iteration was a no-op after the first pass, and the
O_NONBLOCK fcntl applies to the accepted connfd, not to listenfd, so
the nonblocking behaviour is unchanged.
Copilot AI balanced review requested due to automatic review settings December 1, 2026 17:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the change that starts listening earlier in both PSK server variants.

No open issues 🎉

What changed in this PR

Moves listen() immediately after bind() in both PSK server variants, matching the basic PSK server and reducing the startup window in which TCP connections are refused.

Changes:

  • Calls listen() once before wolfSSL initialization instead of on each accept-loop iteration.
  • Preserves existing error handling and accepted-socket nonblocking behavior; clients still need to wait for server readiness.
File Description
psk/​server-psk-threaded.c Starts listening before wolfSSL setup and removes the repeated call from the accept loop.
psk/​server-psk-nonblocking.c Applies the same listening-order change without altering nonblocking socket handling.

Review effort: Balanced

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.

2 participants