Repository navigation
Sockets: fix the start/close races that hang the test runner - #1849
Merged
Merged
Conversation
Three independent defects could hang a socket test forever; on CI they show up as a stuck runner (the parallel UnitTestRunner never finishes, the job sits until the 8 h timeout). 1) ServerSocket_Qt::start() set m_starting = true before checking whether the call was actually accepted. make_server() keeps the QTcpServer even when listen() fails, so every retry of the start-server loop was rejected with m_starting still set. The destructor then deadlocked in close(), which waits for !m_starting. Only set the flag when a start actually begins, and on a failed listen destroy the server so the object is left exactly as it was (matching the POSIX/WinSocket backends, where a failed listen leaves no listener behind). 2) ServerSocket_POSIX / ServerSocket_WinSocket: start() stored the new accept task in the single m_accept_thread slot. The assignment destroys the previous AsyncTask, and its destructor blocks until that task has finished. When the caller's wait timed out while the pool was starved, the old task was still running: a successful old task had entered the accept loop and would only exit via close(), which the caller could not reach because it was stuck in start() - a permanent hang. start() now appends to a vector of tasks and close() joins them all. Each accept task captures a generation number; a task that notices it was replaced stays silent (its listen result would corrupt the attempt that superseded it) and exits. 3) The tests' start_server() retry loop waited on "listen_finished > 0", a counter that is never reset between attempts. After a transient bind failure the wait returned immediately on the next attempt with the previous attempt's result: either the stale error was misread as this attempt's failure, or - worse - the server was already LISTENING on the previous attempt's port, the new start() was rejected, and the loop handed back a port the server was not actually listening on. The client then got "Connection refused" and the test spun for a full 60 s wait on a server that would never see the connection. Each attempt now waits for the counter to advance past its own baseline.
MSVC's flow analysis does not track that "listening" is read only when "destructing" is false (short-circuit on the second condition), and treats the warning as an error on CI. Initialize both locals; the destructing==true path never uses "listening".
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.
What
Follow-up to #1832 / the master-hang investigation (watchdog PR #1848). While chasing the intermittent stuck CI runners, I found three independent defects that can each hang a socket test forever; a hung test is what stalls the parallel runner until the 8 h job timeout.
1)
ServerSocket_Qt::start()leftm_startingset on the rejected pathm_startingwas set before the "am I allowed to start" check.make_server()keeps theQTcpServereven whenlisten()fails, so every retry of the test's start-retry loop was rejected withm_startingstill true — and the destructor deadlocks inclose(), which waits on!m_starting. This matches the Clang CI hang in Sockets (Qt) - Completion lifetime under event-thread stall (test thread parked inclose()'s first cv wait, server already listening from an earlier attempt).Fix: only set the flag when a start actually begins, and on a failed listen destroy the server so the object is left exactly as it was (the POSIX/WinSocket backends already behave that way: a failed listen leaves no listener behind).
2)
start()destroyed a still-running accept task (POSIX + WinSocket)start()stored the new accept task in the singlem_accept_threadslot; the assignment destroys the previousAsyncTask, whose destructor blocks until that task finishes. When the caller's wait timed out while the pool was starved, the old task was still running — and a successful old task had entered the accept loop, which only exits viaclose(). The caller was stuck instart()and could never reachclose()→ permanent hang (the MSVC CI hang in Sockets (WinSocket) - Send/close race).Fix:
start()appends to a vector of tasks;close()joins them all. Each accept task captures a generation number; a task that notices it was replaced stays silent (its listen result would corrupt the attempt that superseded it) and exits. The poll-loop exit check includes the generation too.3) The test harness's
start_server()retry loop read stale stateThe loop waited on
listen_finished > 0— a counter that is never reset between attempts. After a transient bind failure, the next attempt's wait returned immediately with the previous attempt's result. Either the stale error was misread as this attempt's failure, or — worse — the server was already LISTENING on the previous attempt's port, the newstart()was rejected, and the loop handed back a port the server was not actually listening on. The client then got "Connection refused" and the test spun through a full 60 s wait on a server that would never see the connection.Fix: each attempt records the counter baseline and waits for it to advance past that baseline.
Verification