Skip to content

osd: free the stack of a coroutine that completes without yielding - #72704

Closed
aainscow wants to merge 2 commits into
ceph:mainfrom
aainscow:wip-osd-coroutine-complete-leak
Closed

aainscow wants to merge 2 commits into
ceph:mainfrom
aainscow:wip-osd-coroutine-complete-leak

Conversation

@aainscow

@aainscow aainscow commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Warning

This PR was generated with AI assistance (Claude Code) to aid the productivity of the PR owner. It has not been reviewed by a human yet, which is why the commits have no Signed-off-by. The fix has been shown to work as follows (more under Testing below):

PrimaryLogPG::do_op() runs a CALL op on a pool with EC optimizations in a coroutine. coro_resumer owns the coroutine, and the coroutine body ends with on_coroutine_complete(), which resets coro_resumer. Since #71327, CoroHandles keeps only a weak_ptr, so coro_resumer is the only strong reference. When the op completes without yielding, the reset destroys the coroutine while it is still running on its own stack. Boost then never unwinds the fiber, and its 128KB stack leaks once per such op. (https://tracker.ceph.com/issues/81777)

A coroutine that yields is safe: the objects_read_sync() completion resumes it through a locked weak_ptr, which keeps it alive until it returns.

rados/verify valgrind jobs show the leak as Leak_DefinitelyLost/Leak_PossiblyLost in create_control_block<...push_coroutine...> from the do_op lambda. Teuthology reports only the first valgrind error, and on main that is usually the monitor CRYPTO_malloc leak (https://tracker.ceph.com/issues/79831), so this one has been hidden. For example, jobs 629993 and 604377 have it on every OSD. With the monitor leak suppressed, it is the remaining failure.

This PR moves the coroutine start into start_coroutine() in osd/Coroutines.h. It holds a local reference while the coroutine runs, so a completed coroutine is destroyed after it returns, outside its own stack.

The in-body reset was introduced by 52f862c (#67079). #71327 (https://tracker.ceph.com/issues/79515) fixed the use-after-free in on_change() but kept this reset.

Testing:

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

🤖 Generated with Claude Code

PrimaryLogPG::do_op() runs an EC CALL op in a coroutine owned by
coro_resumer, and the coroutine body ends with on_coroutine_complete(),
which resets coro_resumer. CoroHandles only keeps a weak_ptr, so when the
op completes without yielding, that reset drops the last reference and
destroys the push_type while the coroutine is still running on its own
stack. The fiber is then never unwound and its 128KB stack leaks, once
per such op. rados/verify valgrind jobs report it as Leak_DefinitelyLost
in create_control_block, hidden behind the monitor leak that teuthology
reports first.

A coroutine that yields is resumed by the objects_read_sync() completion,
which holds a locked reference, so only the synchronous path leaks.

Start the coroutine with start_coroutine(), which holds a local reference
while it runs, so a completed coroutine is destroyed after it returns.

Fixes: https://tracker.ceph.com/issues/81777
Assisted-by: Claude Code
Count the stacks a coroutine started with start_coroutine() allocates,
and check that each is freed when the coroutine completes without
yielding, completes after a yield, or is destroyed while suspended.

Without the previous commit, CompleteWithoutYieldFreesStack fails and
valgrind reports the 128KB stack as definitely lost.

Refs: https://tracker.ceph.com/issues/81777
Assisted-by: Claude Code
@aainscow

Copy link
Copy Markdown
Contributor Author

jenkins test make check

@rzarzynski

Copy link
Copy Markdown
Contributor

I think I recall @MattyWilliams22 has been recently poking with this code.

@aainscow

Copy link
Copy Markdown
Contributor Author

@MattyWilliams22 I believe you have been look into this. Please put a quick update of your findings.

@MattyWilliams22

Copy link
Copy Markdown
Contributor

I have looked into this code and attempted to test it in QA, but I haven't been able to produce any conclusive proof that this code eliminates any memory leaks.

I have made another branch (https://github.com/MattyWilliams22/ceph/tree/wip-free-coro-stack) which contains a commit that I think has more promise at solving this problem. I am in the process of testing it now.

@MattyWilliams22

Copy link
Copy Markdown
Contributor

I have created another PR for this issue, which seems to work more effectively: #73193

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants