Fix data races in connection lifecycle code (TSAN findings, #460)#482
Draft
p-alik wants to merge 1 commit into
Draft
Fix data races in connection lifecycle code (TSAN findings, #460)#482p-alik wants to merge 1 commit into
p-alik wants to merge 1 commit into
Conversation
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>
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.
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_listongearman_server_con_stare set by one thread (e.g. IO thread) and polled by another (proc/main thread) with no synchronization at all in several places. Converted tostd::atomic<bool>._thread_packet_flush()walkedcon->io_packet_list(dereferencing->packet/->next) without a lock, while the producer (gearman_server_io_packet_add()) always appends undercon->thread->lock. Now the head pointer/->nextare read under that lock before use.gearman_server_con_to_be_freed_next()had an unlocked pre-check readingthread->to_be_freed_listbefore 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_countwakeup checks), matching the discussion in the issue that broad changes shouldn't be packed into 2.0.0.Test plan
-fsanitize=thread(CXXFLAGS='-fsanitize=thread -g -O1') in an isolated tree.gearmand --threads=4under TSAN, hammered with 16 concurrentgearmanclients submitting 300 background jobs each (4800 total, no worker — mirrors the issue's repro).is_dead,io_packet_list,to_be_freed_nextpre-check) reproduce anymore; remaining TSAN reports are exactly the intentionally-unlocked fast-paths and acon->retrace 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).