Fix race in Acceptor::stop() by calling onStop() after thread_join()#740
Open
nkbolg wants to merge 1 commit into
Open
Fix race in Acceptor::stop() by calling onStop() after thread_join()#740nkbolg wants to merge 1 commit into
nkbolg wants to merge 1 commit into
Conversation
Previously onStop() was called before joining the acceptor thread, so teardown could destroy resources while the thread running onStart() was still using them. Move onStop() after thread_join() so the thread is fully joined before teardown. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Previously onStop() was called before joining the acceptor thread, so teardown could destroy resources while the thread running onStart() was still using them. Move onStop() after thread_join() so the thread is fully joined before teardown.
onStop()is the derived-class hook that tears down acceptor resources (e.g.SocketAcceptorcloses its listening sockets and destroys the socket server).The acceptor's background thread runs
onStart()and keeps using thoseresources until it actually exits.
In the previous order,
stop()setm_stop = trueand then immediately calledonStop()while the worker thread was still running. This created a racebetween teardown and the still-live
onStart()loop:onStop()could destroy sockets / server state that the worker thread wasconcurrently reading from or writing to, leading to use-after-free and
undefined behavior on shutdown.
thread_join()then waited on a thread that had been racingagainst destruction of the very objects it depended on.
Setting the
m_stopflag, joining the thread, and only then runningonStop()guarantees the worker thread has fully exited before any of its resources are
released — eliminating the race.