Bug #79515
closedFeature #75023: Support for Erasure Coded Metadata Pools
osd: coroutine lifecycle bugs in PrimaryLogPG/ECBackend
100%
Description
#74531 introduced coroutine-based synchronous EC reads. PrimaryLogPG::do_op() spawns a boost::coroutines2::push_type (resume_token_t) stored in unique_ptr<resume_token_t> coro_resumer. ECBackend::objects_read_sync() stores a CoroHandles struct which holds references to both the yield_token_t and the resume_token_t and passes them into the async-read completion callback
These memory leaks were obfuscated by overly broad valgrind suppression additions in #72938, which intentionally broad but overdone.
From my investigation, there are potentially two bugs, first the coroutine body resets its own owning push_type, at PrimaryLogPG.cc:2628, while still executing on its stack.
auto resumer = std::make_unique<resume_token_t>(
[this, op_raw](yield_token_t& yield) {
op_raw->coro_handles.emplace(CoroHandles{ yield, *coro_resumer });
do_op_impl(op_ref);
coro_resumer = nullptr; // destroys the owning push_type
on_coroutine_complete(); // while still executing on its stack
});
coro_resumer = std::move(resumer);
(*coro_resumer)();
Cleanup should likely happen outside of the coroutine body to avoid this.
Second, inside PrimaryLogPG::on_change() we can trigger a use after free, when completion callback calls resume() on a destroyed token
struct CoroHandles {
yield_token_t& yield;
resume_token_t& resume;
};
// ECBackend.cc completion callback captures CoroHandles by value:
Context *on_finish = new LambdaContext([&, coro](int r) {
if (waiting) {
coro.resume(); // calls operator() on resume_token_t&
}
});
// PrimaryLogPG::on_change() resets coro_resumer while coroutine is suspended:
coro_resumer = nullptr; // destroys resume_token_t
// coro.resume still holds a dangling reference
The solution to this I'm unsure of, potentially changing CoroHandles resume variable to a pointer, rather than a reference, so that it can be nulled.
--------------------------------------------
Further investigation and notes:
When a RADOS client calls a class method (e.g. rados.execute()) on an object in an Erasure Coded pool, the OSD needs to read the object's data before it can run the method. For EC pools this read is asynchronous, the OSD sends sub-reads to multiple OSDs and reassembles the data when they all reply.
PR https://github.com/ceph/ceph/pull/67079 introduced a mechanism to handle this without blocking the OSD worker thread: it wraps the operation in a coroutine, essentially a lightweight function that can be paused mid-execution and resumed later. Each coroutine gets its own private stack, allocated from the heap (128 KiB per coroutine). That stack is freed automatically when the coroutine finishes cleanly.
A leak was detected in a QA batch that included suppression fixes, removing 'UnknownInlinedFun' which was overly broad #77138 and suppressing the memory leaks from this path.
Logs:
skanta-2026-08-11_22:46:54-rados-wip-bharath7-testing-20260811.152323-main-distro-default-trial/458958/remote/trial008/log/valgrind/osd.8.log.gz
This leak happens on two potential paths:
- The operation is rejected early. do_op_impl has many early-exit checks (wrong shard, client blocklisted, object name too long, pool in error state, etc.). If any of these fire, the coroutine body reaches the cleanup lines without ever having suspended. The self-deletion occurs while the coroutine is still running, and the 128 KiB stack leaks.
- The PG changes state while the coroutine is waiting. If the coroutine does suspend (waiting on the EC read), and the OSD then undergoes a map change (e.g. a peer goes down), on_change() deletes the owner object from outside the coroutine. The coroutine's stack holds a live C++ reference to that now-deleted object. When Boost tries to safely unwind the suspended stack, it may touch that dangling reference before it can free the memory, and the allocation escapes.
The 3 leaked blocks in the Valgrind report are 3 in-flight class-method ops on OSD.8 that each hit one of these paths before the OSD shut down.
I created a unit test with dummy objects to isolate the bugged pattern and recreate the scenario. The tests intentionally fail to demonstrate the bug. They can be found on this branch: https://github.com/stzuraski898/ceph/tree/wip-sz-synchronous-EC-reads-leak-testing
SZ Updated by Steven Zuraski 4 months ago
- Related to Feature #74531: Synchronous Reads in EC added
LF Updated by Laura Flores 4 months ago
- Project changed from Ceph to RADOS
- Category deleted (
OSD)
LF Updated by Laura Flores 4 months ago
- Category set to EC Pools
SZ Updated by Steven Zuraski 4 months ago
Pasting the leak from the logs
/a/skanta-2026-08-11_22:46:54-rados-wip-bharath7-testing-20260811.152323-main-distro-default-trial/458958/remote/trial008/log/valgrind/osd.8.log.gz
<error>
<unique>0x31c5b</unique>
<tid>1</tid>
<kind>Leak_DefinitelyLost</kind>
<xwhat>
<text>393,216 bytes in 3 blocks are definitely lost in loss record 192 of 192</text>
<leakedbytes>393216</leakedbytes>
<leakedblocks>3</leakedblocks>
</xwhat>
<stack>
<frame>
<ip>0x61EF87E</ip>
<obj>/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
<fn>malloc</fn>
<dir>/builddir/build/BUILD/valgrind-3.25.1/coregrind/m_replacemalloc</dir>
<file>vg_replace_malloc.c</file>
<line>446</line>
</frame>
<frame>
<ip>0x46B270C</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>UnknownInlinedFun</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/redhat-linux-build/boost/include/boost/context</dir>
<file>fixedsize_stack.hpp</file>
<line>57</line>
</frame>
<frame>
<ip>0x46B270C</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>UnknownInlinedFun</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/redhat-linux-build/boost/include/boost/coroutine2/detail</dir>
<file>create_control_block.ipp</file>
<line>32</line>
</frame>
<frame>
<ip>0x46B270C</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>UnknownInlinedFun</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/redhat-linux-build/boost/include/boost/coroutine2/detail</dir>
<file>push_coroutine.ipp</file>
<line>158</line>
</frame>
<frame>
<ip>0x46B270C</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>UnknownInlinedFun</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/redhat-linux-build/boost/include/boost/coroutine2/detail</dir>
<file>push_coroutine.ipp</file>
<line>153</line>
</frame>
<frame>
<ip>0x46B270C</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>UnknownInlinedFun</fn>
<dir>/usr/include/c++/14/bits</dir>
<file>unique_ptr.h</file>
<line>1076</line>
</frame>
<frame>
<ip>0x46B270C</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>PrimaryLogPG::do_op(boost::intrusive_ptr<OpRequest>&)</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/src/osd</dir>
<file>PrimaryLogPG.cc</file>
<line>2624</line>
</frame>
<frame>
<ip>0x45E6C0E</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>OSD::dequeue_op(boost::intrusive_ptr<PG>, boost::intrusive_ptr<OpRequest>, ThreadPool::TPHandle&)</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/src/osd</dir>
<file>OSD.cc</file>
<line>10019</line>
</frame>
<frame>
<ip>0x4883C78</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>ceph::osd::scheduler::PGOpItem::run(OSD*, OSDShard*, boost::intrusive_ptr<PG>&, ThreadPool::TPHandle&)</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/src/osd/scheduler</dir>
<file>OpSchedulerItem.cc</file>
<line>29</line>
</frame>
<frame>
<ip>0x4610512</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>UnknownInlinedFun</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/src/osd/scheduler</dir>
<file>OpSchedulerItem.h</file>
<line>144</line>
</frame>
<frame>
<ip>0x4610512</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>OSD::ShardedOpWQ::_process(unsigned int, unsigned int, ceph::heartbeat_handle_d*)</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/src/osd</dir>
<file>OSD.cc</file>
<line>11444</line>
</frame>
<frame>
<ip>0x4BB4AE2</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>ShardedThreadPool::shardedthreadpool_worker(unsigned int, unsigned int)</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/src/common</dir>
<file>WorkQueue.cc</file>
<line>346</line>
</frame>
<frame>
<ip>0x4BB5596</ip>
<obj>/usr/bin/ceph-osd-classic</obj>
<fn>ShardedThreadPool::WorkThreadSharded::entry()</fn>
<dir>/usr/src/debug/ceph-21.3.0-1949.g6524572d.el10.x86_64/src/common</dir>
<file>WorkQueue.h</file>
<line>665</line>
</frame>
<frame>
<ip>0x6EB9127</ip>
<obj>/usr/lib64/libc.so.6</obj>
<fn>start_thread</fn>
</frame>
<frame>
<ip>0x6F29923</ip>
<obj>/usr/lib64/libc.so.6</obj>
<fn>clone</fn>
</frame>
</stack>
SZ Updated by Steven Zuraski 4 months ago
- Assignee set to Matty Williams
RZ Updated by Radoslaw Zarzynski 4 months ago
Bump up. CC: @Matty Williams.
MW Updated by Matty Williams 4 months ago ยท Edited
I've written a rough fix on this branch: https://github.com/MattyWilliams22/ceph/tree/wip-ec-sync-reads-memory-leak
I've attempted to run some teuthology tests with similar job descriptions to the original failure. I've run the same set of jobs on smithi and trial - both sets of jobs have been unsuccessful (in both cases 13/14 jobs died and the last job failed for unrelated reasons).
https://pulpito-ng.ceph.com/runs/mattywilliams-2026-08-18_08:20:11-rados:verify-main-distro-default-smithi
https://pulpito-ng.ceph.com/runs/mattywilliams-2026-08-19_10:38:27-rados:verify-main-distro-default-trial
I am going to wait until Teuthology tests seem to be passing consistently, then I will rerun the teuthology jobs.
MW Updated by Matty Williams 3 months ago
- Status changed from New to In Progress
MW Updated by Matty Williams 3 months ago
I have had a slightly more successful set of teuthology jobs: https://pulpito-ng.ceph.com/runs/mattywilliams-2026-08-21_10:36:38-rados:verify-main-distro-default-trial
All of these jobs fail, however, as far as I can tell, none of the failures are related to coroutines used for EC sync reads.
More testing is required to ensure that this fix is effective.
MW Updated by Matty Williams 3 months ago
- % Done changed from 0 to 50
- Pull request ID set to 71327
LF Updated by Laura Flores 3 months ago
/a/lflores-2026-08-24_14:33:54-rados-wip-lflores-testing-3-2026-08-21-2201-distro-default-smithi/509691
RZ Updated by Radoslaw Zarzynski 3 months ago
The PR is a draft but review is in progress.
MW Updated by Matty Williams 3 months ago
- % Done changed from 50 to 80
BS Updated by Bill Scales 3 months ago
- Status changed from In Progress to Fix Under Review
- Backport set to umbrella
MW Updated by Matty Williams 3 months ago
- % Done changed from 80 to 100
RZ Updated by Radoslaw Zarzynski 3 months ago
Sent to QA.
KS Updated by Kamoltat (Junior) Sirivadhna 3 months ago
scrub QA still being reviewed
UB Updated by Upkeep Bot 2 months ago
- Copied to Backport #80748: umbrella: osd: coroutine lifecycle bugs in PrimaryLogPG/ECBackend added
UB Updated by Upkeep Bot 2 months ago
- Status changed from Fix Under Review to Backporting
- Merge Commit set to 787a82eb5cfb96230a9af7758e72b33eac413dfb
- Fixed In set to v21.3.0-3523-g787a82eb5c
- Upkeep Timestamp set to 2026-09-22T15:04:13+00:00
MW Updated by Matty Williams about 2 months ago
- Parent task set to #75023
AA Updated by Alex Ainscow about 2 months ago
- Related to Bug #81777: osd: EC CALL op coroutine that completes without yielding leaks its stack added
UB Updated by Upkeep Bot 23 days ago
- Status changed from Backporting to Resolved
- Upkeep Timestamp changed from 2026-09-22T15:04:13+00:00 to 2026-11-10T01:18:41+00:00