Skip to content

fix(concurrency): drain queue by design before shutdown signals worker exit#469

Merged
mvillmow merged 1 commit into
mainfrom
322-auto-impl
Apr 26, 2026
Merged

fix(concurrency): drain queue by design before shutdown signals worker exit#469
mvillmow merged 1 commit into
mainfrom
322-auto-impl

Conversation

@mvillmow
Copy link
Copy Markdown
Collaborator

Summary

  • Add drain_cv_ condition variable to ThreadPool so shutdown() explicitly waits for the work queue to empty before waking workers to exit.
  • Workers notify drain_cv_ (under the queue lock) immediately after dequeueing the last item, making the drain observable without any polling.
  • Add GracefulShutdownDrainsQueueExplicitly test that submits 20 tasks then calls shutdown() immediately — asserts all 20 complete with no sleep_for reliance.

Root cause (issue #322): The previous shutdown() set shutdown_requested_ first and relied on the worker exit-condition check (work_queue_.empty()) to drain remaining items. This was correct but incidental — if the exit condition were ever changed, the drain guarantee would silently vanish. The fix promotes the guarantee to an explicit, observable drain phase.

Test plan

  • GracefulShutdownDrainsQueueExplicitly passes (new test)
  • GracefulShutdown continues to pass (existing test)
  • DestructorShutdown continues to pass (existing test)
  • NoWorkAfterShutdown continues to pass (existing test)
  • Format check: just format-check

Closes #322.

🤖 Generated with Claude Code

@mvillmow mvillmow enabled auto-merge (rebase) April 26, 2026 00:43
@github-actions
Copy link
Copy Markdown

✅ Dependency Audit

Severity Count
Critical 0
High 0
Medium 0
Low 0

See the Security tab for detailed findings.


Workflow: Dependency Audit

@github-actions
Copy link
Copy Markdown

Security Scan Results

  • ❌ Secret Scanning: Potential secrets found
  • ✅ SAST: Completed (check Security tab for details)
  • ✅ Dependency Scanning: Completed
  • ✅ C++ Static Analysis: Completed
  • ✅ Docker Image Scanning: 0 high, 22 medium vulnerabilities (acceptable)

Recommendations

  • Review findings in the GitHub Security tab
  • Check artifact uploads for detailed reports
  • Address critical Docker vulnerabilities immediately

Workflow: Security Scanning

…worker exit

The shutdown() method previously set shutdown_requested_ immediately,
relying on the workers' exit-condition check (queue.empty()) to drain
remaining items — correct but incidental.  This commit makes the drain
guarantee explicit and by-design:

* Add drain_cv_ condition variable to ThreadPool.
* Workers notify drain_cv_ under the queue lock immediately after
  dequeueing the last item.
* shutdown() waits on drain_cv_ until the queue is empty before issuing
  the final condition_.notify_all() that causes workers to exit.
* Add GracefulShutdownDrainsQueueExplicitly test that submits a burst of
  20 tasks and asserts all complete after shutdown(), with no sleep().

Closes #322.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mvillmow mvillmow merged commit 9443463 into main Apr 26, 2026
8 of 9 checks passed
@mvillmow mvillmow deleted the 322-auto-impl branch April 26, 2026 18:31
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.

GracefulShutdown test doesn't drain queue before setting flag

1 participant