Repository navigation
fix: call listen() once before the accept loop in PSK servers - #659
Open
MarkAtwood wants to merge 1 commit into
Open
MarkAtwood wants to merge 1 commit into
MarkAtwood wants to merge 1 commit into
Conversation
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.
Contributor
There was a problem hiding this comment.
🟢 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
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.
Calls
listen()once right afterbind()in the two PSK servers that deferred it, instead of calling it on every pass of the accept loop.psk/server-psk-nonblocking.candpsk/server-psk-threaded.cbound the listening socket and then ranwolfSSL_Init(),wolfSSL_CTX_new(), the PSK callback setup,wolfSSL_CTX_set_cipher_list()andwolfSSL_CTX_SetTmpDH_file()(which parses a DH PEM off disk) before ever reachinglisten(). A TCP connect to a bound-but-not-listening port is refused immediately, so a client arriving in that window gets ECONNREFUSED and printsConnection Errorfrompsk/client-psk.c:102.psk/server-psk.calready callslisten()directly afterbind(); 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-psk-nonblockingserver-psk-threadedEnd to end, launching
client-pskwith no delay after the server, 5 trials each: before, 5/5Connection Erroron 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 thefcntl(..., O_NONBLOCK)call applies to the acceptedconnfdrather than tolistenfd, so the nonblocking behaviour is unchanged.Testing: built both servers plus
client-pskagainst wolfSSL 5.9.4 installed at/usr/localusing the repo'spsk/Makefile, no new warnings. Handshake still succeeds after the change (Server Message: I hear ya for shizzle).