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

    • Final suspension behavior updated to use a destroyer awaiter that reliably destroys coroutine frames, improving compatibility and ensuring proper resource cleanup during async completion.
  • Documentation

    • Added inline explanatory comments clarifying compatibility rationale and final-suspension behavior.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9cb5e9c4-cb04-4f5d-85bc-e72a574011a6

📥 Commits

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

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

📝 Walkthrough

Walkthrough

This change replaces final_suspend() in run_async_trampoline promise_types to return a custom destroyer awaiter (via auto final_suspend() noexcept) that, on suspension, calls h.destroy() to explicitly destroy the coroutine frame, addressing MSVC trampoline safety concerns.

Changes

Cohort / File(s) Summary
Promise Type Final Suspension
include/boost/capy/ex/run_async.hpp
Changed promise_type::final_suspend() signature from std::suspend_never final_suspend() noexcept to auto final_suspend() noexcept for primary and std::pmr::memory_resource* specialization. Implementation now returns a private destroyer awaiter whose await_ready() is false, and whose await_suspend() calls h.destroy() and await_resume() is empty. Added explanatory comments about MSVC trampoline issues.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant CoroutineHandle as "coroutine_handle<h>"
    participant Promise
    participant DestroyerAwaiter as "destroyer awaiter"

    Caller->>CoroutineHandle: resume / run coroutine
    CoroutineHandle->>Promise: reaches final_suspend()
    Promise->>DestroyerAwaiter: return destroyer awaiter
    CoroutineHandle->>DestroyerAwaiter: await_suspend(handle)
    DestroyerAwaiter->>CoroutineHandle: h.destroy()
    DestroyerAwaiter-->>Caller: completes (no resume value)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A tiny trampoline hops to the end,
The destroyer waits — a tidy friend.
When the final bell rings, it takes the frame,
Nips memory worries, and skips the blame. 🥕

🚥 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 summarizes the main change: fixing an MSVC-specific thread_pool join test failure by replacing suspend_never with a destroyer awaiter in the run_async trampoline 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
📝 Coding Plan
  • Generate coding plan for human review comments

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

cppalliance-bot commented Mar 11, 2026

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-13 21:08:25 UTC

@cppalliance-bot
Copy link

cppalliance-bot commented Mar 11, 2026

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-13 21:22:21 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.37%. Comparing base (55efc7f) to head (f4f9db6).
⚠️ Report is 9 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.37%   -0.04%     
===========================================
  Files          162      162              
  Lines         8854     8976     +122     
===========================================
+ Hits          8182     8292     +110     
- Misses         672      684      +12     
Flag Coverage Δ
linux 92.34% <ø> (-0.05%) ⬇️

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 21 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...f4f9db6. Read the comment docs.

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

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.
@mvandeberg mvandeberg force-pushed the pr/msvc-thread-pool branch from 600f672 to f4f9db6 Compare March 13, 2026 21:01
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