Skip to content

Fix data races in connection lifecycle code (TSAN findings, #460)#482

Draft
p-alik wants to merge 1 commit into
gearman:masterfrom
p-alik:fix-460-connection-lifecycle-races
Draft

Fix data races in connection lifecycle code (TSAN findings, #460)#482
p-alik wants to merge 1 commit into
gearman:masterfrom
p-alik:fix-460-connection-lifecycle-races

Conversation

@p-alik

@p-alik p-alik commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

Addresses a scoped subset of the data races reported in #460 (found via TSAN while validating #459):

  • is_dead, io_list, proc_list, proc_removed, to_be_freed_list on gearman_server_con_st are set by one thread (e.g. IO thread) and polled by another (proc/main thread) with no synchronization at all in several places. Converted to std::atomic<bool>.
  • _thread_packet_flush() walked con->io_packet_list (dereferencing ->packet/->next) without a lock, while the producer (gearman_server_io_packet_add()) always appends under con->thread->lock. Now the head pointer/->next are read under that lock before use.
  • gearman_server_con_to_be_freed_next() had an unlocked pre-check reading thread->to_be_freed_list before acquiring the same lock the writer always uses. Removed the pre-check.

This is deliberately scoped to the 3 representative races called out in the issue body plus the same-pattern flags — it does not touch the intentionally-racy performance fast-paths mentioned in the issue's "other race sites" list (dcon_add_count, io_count/proc_count wakeup checks), matching the discussion in the issue that broad changes shouldn't be packed into 2.0.0.

Test plan

  • Normal build compiles cleanly with no new warnings.
  • Built with -fsanitize=thread (CXXFLAGS='-fsanitize=thread -g -O1') in an isolated tree.
  • Ran gearmand --threads=4 under TSAN, hammered with 16 concurrent gearman clients submitting 300 background jobs each (4800 total, no worker — mirrors the issue's repro).
  • Confirmed none of the fixed race sites (is_dead, io_packet_list, to_be_freed_next pre-check) reproduce anymore; remaining TSAN reports are exactly the intentionally-unlocked fast-paths and a con->ret race not named in the issue, left out of scope for this PR.

Closes part of #460 (leaves the documented "other race sites" list for a follow-up).

is_dead/io_list/proc_list/proc_removed/to_be_freed_list are set on one
thread and polled on another (IO/proc/main) with no synchronization,
so make them std::atomic<bool>. Also take con->thread->lock when
reading the io_packet_list head in _thread_packet_flush(), since the
FIFO is appended to under that lock by other threads, and drop the
unlocked pre-check in gearman_server_con_to_be_freed_next() that read
thread->to_be_freed_list before acquiring the same lock the writer
uses.

Verified under -fsanitize=thread with a --threads=4 gearmand instance
hammered by 16 concurrent clients (4800 background job submissions,
no worker): none of the previously reported races at these sites
reproduce anymore.

Signed-off-by: Alexei Pastuchov <info@maximka.de>
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