Skip to content

Fix timer_service crash when use_service races on multi-threaded pool#230

Merged
mvandeberg merged 1 commit intocppalliance:developfrom
mvandeberg:pr/fix-mt-delay
Mar 12, 2026
Merged

Fix timer_service crash when use_service races on multi-threaded pool#230
mvandeberg merged 1 commit intocppalliance:developfrom
mvandeberg:pr/fix-mt-delay

Conversation

@mvandeberg
Copy link
Contributor

@mvandeberg mvandeberg commented Mar 12, 2026

When multiple pool threads call use_service<timer_service>() concurrently, the double-checked lock in use_service_impl can create a duplicate that is immediately deleted. Without a destructor, the std::thread member is destroyed non-joined, calling std::terminate().

Add ~timer_service() that calls shutdown() to join the background thread, and a concurrent delay test that reproduces the bug.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Enhanced timer service resource cleanup to properly synchronize thread shutdown in all scenarios, preventing potential resource leaks when services are discarded without explicit cleanup.

When multiple pool threads call use_service<timer_service>()
concurrently, the double-checked lock in use_service_impl can
create a duplicate that is immediately deleted. Without a
destructor, the std::thread member is destroyed non-joined,
calling std::terminate().

Add ~timer_service() that calls shutdown() to join the background
thread, and a concurrent delay test that reproduces the bug.
@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

📝 Walkthrough

Walkthrough

Adds explicit resource cleanup to the timer_service class by introducing a public destructor and private stop_and_join() helper method. The destructor ensures proper thread shutdown and joining. The shutdown logic is refactored to centralize cleanup operations, addressing a discard path where duplicate service instances were deleted without proper shutdown.

Changes

Cohort / File(s) Summary
Header Declaration
include/boost/capy/ex/detail/timer_service.hpp
Adds public destructor ~timer_service() with comments explaining thread shutdown and cleanup behavior, plus private stop_and_join() helper declaration.
Implementation
src/ex/detail/timer_service.cpp
Implements destructor calling stop_and_join(), introduces stop_and_join() helper to stop service and join background thread, and refactors shutdown() to delegate to the new helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Poem

🐰 A thread that runs must learn to rest,
With join and stop, we cleanup best,
No dangling hops, no lost resource,
The destructor sets our proper course! 🧵✨

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: adding a destructor to prevent crashes when timer_service races occur in multi-threaded contexts.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan for PR comments
  • Generate coding plan

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
include/boost/capy/ex/detail/timer_service.hpp (1)

50-53: Minor comment inconsistency: destructor calls stop_and_join(), not shutdown().

The comment states "Calls shutdown()" but the implementation directly calls stop_and_join(). While functionally equivalent (since shutdown() also delegates to stop_and_join()), the comment could confuse maintainers inspecting the destructor implementation.

📝 Suggested comment clarification
-    // Calls shutdown() to join the background thread.
+    // Calls stop_and_join() to join the background thread.
     // Handles the discard path in use_service_impl where
     // a duplicate service is deleted without shutdown().
     ~timer_service();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@include/boost/capy/ex/detail/timer_service.hpp` around lines 50 - 53, Update
the destructor comment for timer_service to accurately reflect the
implementation: replace "Calls shutdown()" with "Calls stop_and_join()" (or
mention both with clarification that shutdown() delegates to stop_and_join()) so
it matches the destructor behavior and clarifies the discard path in
use_service_impl where duplicates may be deleted without shutdown().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@include/boost/capy/ex/detail/timer_service.hpp`:
- Around line 50-53: Update the destructor comment for timer_service to
accurately reflect the implementation: replace "Calls shutdown()" with "Calls
stop_and_join()" (or mention both with clarification that shutdown() delegates
to stop_and_join()) so it matches the destructor behavior and clarifies the
discard path in use_service_impl where duplicates may be deleted without
shutdown().

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bc1ffa6b-4149-49a1-bd0e-fbde037ae665

📥 Commits

Reviewing files that changed from the base of the PR and between 18c30d2 and c5c3894.

⛔ Files ignored due to path filters (1)
  • test/unit/delay.cpp is excluded by !**/test/**
📒 Files selected for processing (2)
  • include/boost/capy/ex/detail/timer_service.hpp
  • src/ex/detail/timer_service.cpp

@cppalliance-bot
Copy link

An automated preview of the documentation is available at https://230.capy.prtest3.cppalliance.org/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-03-12 16:41:56 UTC

@cppalliance-bot
Copy link

GCOVR code coverage report https://230.capy.prtest3.cppalliance.org/gcovr/index.html
LCOV code coverage report https://230.capy.prtest3.cppalliance.org/genhtml/index.html
Coverage Diff Report https://230.capy.prtest3.cppalliance.org/diff-report/index.html

Build time: 2026-03-12 16:55:10 UTC

@mvandeberg mvandeberg merged commit ecdb076 into cppalliance:develop Mar 12, 2026
36 of 37 checks passed
@mvandeberg mvandeberg deleted the pr/fix-mt-delay branch March 12, 2026 17:00
@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.39%. Comparing base (18c30d2) to head (c5c3894).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #230      +/-   ##
===========================================
+ Coverage    92.38%   92.39%   +0.01%     
===========================================
  Files          162      162              
  Lines         8854     8866      +12     
===========================================
+ Hits          8180     8192      +12     
  Misses         674      674              
Flag Coverage Δ
linux 92.39% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
include/boost/capy/ex/detail/timer_service.hpp 100.00% <ø> (ø)
src/ex/detail/timer_service.cpp 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 18c30d2...c5c3894. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants