Skip to content

Fix MSVC thread_pool join test failure#228

Open
mvandeberg wants to merge 1 commit intocppalliance:developfrom
mvandeberg:pr/msvc-thread-pool
Open

Fix MSVC thread_pool join test failure#228
mvandeberg wants to merge 1 commit intocppalliance:developfrom
mvandeberg:pr/msvc-thread-pool

Conversation

@mvandeberg
Copy link
Contributor

@mvandeberg mvandeberg commented Mar 11, 2026

The run_async trampoline coroutine used suspend_never for final_suspend, relying on automatic frame destruction when the coroutine falls through. MSVC's symmetric transfer implementation (which uses an internal trampoline loop rather than true tail calls) can mishandle this pattern, potentially double-destroying the frame. When the work_guard destructor fires twice, outstanding_work_ reaches zero one task early, stop_ is set, and the remaining queued task is abandoned without its handler running.

Replace suspend_never with an explicit destroyer awaiter that calls h.destroy() in await_suspend and returns void. This gives MSVC's symmetric transfer loop a clean exit point and avoids the problematic auto-destruction codepath. Both trampoline specializations (allocator-based and
memory_resource*) are updated.

Summary by CodeRabbit

  • Bug Fixes
    • Improved async operation finalization handling to enhance compatibility and ensure proper resource cleanup during coroutine completion.

The run_async trampoline coroutine used suspend_never for
final_suspend, relying on automatic frame destruction when
the coroutine falls through. MSVC's symmetric transfer
implementation (which uses an internal trampoline loop
rather than true tail calls) can mishandle this pattern,
potentially double-destroying the frame. When the work_guard
destructor fires twice, outstanding_work_ reaches zero one
task early, stop_ is set, and the remaining queued task is
abandoned without its handler running.

Replace suspend_never with an explicit destroyer awaiter that
calls h.destroy() in await_suspend and returns void. This
gives MSVC's symmetric transfer loop a clean exit point and
avoids the problematic auto-destruction codepath. Both
trampoline specializations (allocator-based and
memory_resource*) are updated.
@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

This pull request modifies the final_suspend() method in the run_async_trampoline promise_type class and its std::pmr::memory_resource* specialization. The return type changes from std::suspend_never to auto, with the method now returning a custom destroyer awaiter that explicitly destroys the coroutine frame instead of suspending, addressing MSVC trampoline compatibility issues.

Changes

Cohort / File(s) Summary
Promise Type Final Suspension Logic
include/boost/capy/ex/run_async.hpp
Modified final_suspend() return type from std::suspend_never to auto in both primary run_async_trampoline<Ex, Handlers, Alloc>::promise_type and specialized run_async_trampoline<Ex, Handlers, std::pmr::memory_resource*>::promise_type. Implementation now delegates to a private destroyer awaiter that invokes h.destroy() for explicit frame destruction instead of suspend behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A frame that danced in MSVC's domain,
Now bows at the end without suspend's chain.
The destroyer arrives with gentle flair,
To clean up with care—no memory despair! 🥕

🚥 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 accurately reflects the main change: fixing an MSVC-specific issue with thread_pool join by modifying final_suspend behavior in the run_async coroutine.

✏️ 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

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/run_async.hpp (1)

262-275: Consider extracting the shared destroyer awaiter.

The destroyer struct is duplicated between the primary template and this specialization. While acceptable for detail code, you could extract it to reduce duplication.

♻️ Optional: Extract shared destroyer awaiter

Add before the run_async_trampoline primary template:

/// Awaiter that explicitly destroys the coroutine frame at final suspension.
/// Returning void from await_suspend provides a clean exit for MSVC's
/// symmetric transfer trampoline loop.
struct final_destroyer
{
    bool await_ready() noexcept { return false; }
    void await_suspend(std::coroutine_handle<> h) noexcept { h.destroy(); }
    void await_resume() noexcept {}
};

Then in both promise_type::final_suspend() implementations:

 auto final_suspend() noexcept
 {
-    struct destroyer
-    {
-        bool await_ready() noexcept { return false; }
-        void await_suspend(
-            std::coroutine_handle<> h) noexcept
-        {
-            h.destroy();
-        }
-        void await_resume() noexcept {}
-    };
-    return destroyer{};
+    return final_destroyer{};
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@include/boost/capy/ex/run_async.hpp` around lines 262 - 275, The duplicate
local destroyer awaiter in promise_type::final_suspend should be extracted into
a shared type: add a struct named (for example) final_destroyer before the
run_async_trampoline primary template with the same members and noexcept
signatures (bool await_ready() noexcept, void
await_suspend(std::coroutine_handle<> h) noexcept, void await_resume()
noexcept), then replace the existing local destroyer return statements in both
promise_type::final_suspend() implementations with return final_destroyer{}; to
remove duplication and keep behavior identical.
🤖 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/run_async.hpp`:
- Around line 262-275: The duplicate local destroyer awaiter in
promise_type::final_suspend should be extracted into a shared type: add a struct
named (for example) final_destroyer before the run_async_trampoline primary
template with the same members and noexcept signatures (bool await_ready()
noexcept, void await_suspend(std::coroutine_handle<> h) noexcept, void
await_resume() noexcept), then replace the existing local destroyer return
statements in both promise_type::final_suspend() implementations with return
final_destroyer{}; to remove duplication and keep behavior identical.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 05f5a17e-cde2-4153-8f40-734ad835ca6e

📥 Commits

Reviewing files that changed from the base of the PR and between e2de31c and 600f672.

📒 Files selected for processing (1)
  • include/boost/capy/ex/run_async.hpp

@cppalliance-bot
Copy link

An automated preview of the documentation is available at https://228.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-11 23:28:24 UTC

@cppalliance-bot
Copy link

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

Build time: 2026-03-11 23:39:24 UTC

@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 42.85714% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.45%. Comparing base (55efc7f) to head (600f672).
⚠️ Report is 3 commits behind head on develop.

Files with missing lines Patch % Lines
include/boost/capy/ex/run_async.hpp 42.85% 8 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #228      +/-   ##
===========================================
+ Coverage    92.41%   92.45%   +0.04%     
===========================================
  Files          162      162              
  Lines         8854     9035     +181     
===========================================
+ Hits          8182     8353     +171     
- Misses         672      682      +10     
Flag Coverage Δ
linux 92.23% <ø> (-0.16%) ⬇️

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

Files with missing lines Coverage Δ
include/boost/capy/ex/run_async.hpp 82.03% <42.85%> (-3.57%) ⬇️

... and 4 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 55efc7f...600f672. 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