fix: prevent worker threads from blocking forever on an empty queue - #57
Open
RamsesRodenburg wants to merge 1 commit into
Open
RamsesRodenburg wants to merge 1 commit into
RamsesRodenburg wants to merge 1 commit into
Conversation
process_host() guards its loop with host_queue.qsize() and then performs a blocking host_queue.get(). With --threads N, several workers can pass the qsize() check while a single item remains: one wins the get(), the others block in it forever. Every queued item still gets its task_done(), so work_queue.join() returns and main() runs to completion - the scan finishes and the results are written. But the leaked workers are non-daemon threads, so threading._shutdown() joins them forever at interpreter exit and the process never terminates. Use a non-blocking get(), which makes the existing "except queue.Empty: break" reachable. The sibling loops in this file, remove_from_queue() and process_output(), already read their queues with block=False.
RamsesRodenburg
force-pushed
the
fix/worker-threads-block-on-empty-queue
branch
from
September 4, 2026 10:53
e3315ee to
15e2bc2
Compare
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.
Problem
process_host()guards its loop withhost_queue.qsize()and then performs a blockinghost_queue.get():With
--threads N, several workers can pass theqsize()check while only one item remains. One wins theget(); the others block in it forever.Every queued item still gets its
task_done(), sowork_queue.join()inloop_hosts()returns,main()runs all the way through, and the results are written — the scan looks completely successful. But the leaked workers are non-daemon threads, sothreading._shutdown()joins them forever at interpreter exit and the process never terminates.This is easy to miss precisely because the output is complete. The log just ends normally and then hangs:
It is very visible under Docker, where
analyze_hostsis PID 1: the container staysUpindefinitely,docker run --rmnever returns and so never cleans up, and whatever invoked it waits on it. We accumulated six such containers over a month of nightly scans — one of them had ~150 zombie children — before tracking it down.Reproduction
This is the
loop_hosts/process_hostshape reduced to just the queue handling:Run it under
timeout 120 python3 repro.py.timeouthad to kill it — exit code 124).The fix
Read the queue with
block=False, which makes the existingexcept queue.Empty: breakreachable and lets a worker that loses the race exit cleanly.The two sibling loops in this same file already do exactly that —
remove_from_queue()usesfinished_queue.get(block=False)andprocess_output()usesoutput_queue.get(block=False).process_host()looks like the one that was missed.Breaking out is the correct behaviour here: a worker only sees
queue.Emptywhen the queue is genuinely empty, and every target is enqueued before the workers start, so an empty queue means there is no more work.Unrelated, and happy to open a separate issue rather than bundle it: the current
gofwd/analyze_hostsimage prints[-] Please install required modules ...: No module named 'pkg_resources'at start-up and silently disables--framework.python-Wappalyzerimportspkg_resources, which ships with setuptools — and since Python 3.12venvno longer seeds setuptools, while setuptools ≥ 82 has removedpkg_resourcesoutright, so adding setuptools back does not help. Just say the word if that would be useful.