Skip to content

Fix race in Acceptor::stop() by calling onStop() after thread_join()#740

Open
nkbolg wants to merge 1 commit into
quickfix:masterfrom
nkbolg:fix/acceptor-stop-onstop-after-thread-join
Open

Fix race in Acceptor::stop() by calling onStop() after thread_join()#740
nkbolg wants to merge 1 commit into
quickfix:masterfrom
nkbolg:fix/acceptor-stop-onstop-after-thread-join

Conversation

@nkbolg
Copy link
Copy Markdown

@nkbolg nkbolg commented Jun 8, 2026

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.
SocketAcceptor closes its listening sockets and destroys the socket server).
The acceptor's background thread runs onStart() and keeps using those
resources until it actually exits.

In the previous order, stop() set m_stop = true and then immediately called
onStop() while the worker thread was still running. This created a race
between teardown and the still-live onStart() loop:

  • onStop() could destroy sockets / server state that the worker thread was
    concurrently reading from or writing to, leading to use-after-free and
    undefined behavior on shutdown.
  • The subsequent thread_join() then waited on a thread that had been racing
    against destruction of the very objects it depended on.

Setting the m_stop flag, joining the thread, and only then running onStop()
guarantees the worker thread has fully exited before any of its resources are
released — eliminating the race.

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>
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.

1 participant