Repository navigation
rgw/beast: use strand executor for timeout timer to prevent concurrent socket access - #67424
Conversation
…t socket access The timeout_timer in AsioFrontend::on_accept() was constructed with context.get_executor(), which dispatches the timeout handler to the raw io_context. Since each connection's coroutine runs on a strand (via make_strand(context)), this allowed the timeout handler to execute concurrently with the coroutine on a different thread. When the timeout fires, timeout_handler calls socket.cancel() and socket.shutdown() racing with the coroutine's async_read_header(), async_write(), or async_read_some() on the same socket and buffer. This can corrupt the flat_static_buffer's internal pointers, leading to SIGSEGV in the Beast HTTP parser. Fix by constructing the timeout_timer with yield.get_executor(), which returns the coroutine's strand executor. This ensures the timeout handler is serialized with the coroutine, preventing concurrent access to the socket and buffer. Fixes: https://tracker.ceph.com/issues/75031 Signed-off-by: Oguzhan Ozmen <[email protected]>
|
Since the boost version is newer than when the code was originally written, I wonder if |
thanks @jzhu116-bloomberg. i don't think that |
Thanks @cbodley for the explanation. It looks like I trusted the wrong AI tool. Here is what I've got:
I didn't test or dig into the source code myself, which I probably should have. I trust you more on this, and will do some tests to confirm it myself. |
|
given something like this: http::async_read_some(stream, buffer, parser, asio::cancel_after(10s, yield[ec]));under the hood, this creates a cancellation_signal and associates a cancellation_slot with the wrapped completion token a timeout from that executor (somewhere inside of in simple cases, the calling coroutine would see that error and return early. if the asio socket object is on the stack of that coroutine, which it usually is, its destructor would run and call rgw doesn't do a great job of differentiating between transport errors and others, so after a timeout reading from the socket we may still try to write a response. the call to |
I did some tests on asio::cancel_after as the following (with boost v1.87), and confirmed that we will indeed have to manually call the socket.cancel() etc. asio::spawn(yield, [&](asio::yield_context y) {
boost::system::error_code ec;
std::size_t n = socket.async_read_some(
asio::buffer(buf1),
asio::cancel_after(std::chrono::seconds(2),
asio::cancellation_type::terminal, // default
asio::bind_cancellation_slot(slot, y[ec]))
);
});
if (ec == asio::error::operation_aborted) {
socket->cancel();
socket->shutdown(...);
} else if (ec) {
...Using asio::cancel_after(...) with http::async_* still has a little bit benefit:
|
|
that looks reasonable. it may be simpler to just add a strand executor to what we have though? |
As squid uses 1.82, looking at https://www.boost.org/doc/libs/1_82_0/doc/html/boost_asio/reference/basic_stream_socket.html; though, 1.87 doc reads the same
I think I couldn't determine which one but (from the tracker), it looks like
Yes, this is surprising but memcheck is not good at detecting data races? If it were use-after-free or invalid write into memory it'd have detected I believe but here corrupted memory buffer is corrupted in-flgiht data but yeslet me think through a bit whether we can better pin point. I think TSAN is better suited for this job.
This is interesting but i think, just thinking about this change, in normal path timeout timer will be cancelled before it is fired so no strand dispatch happens. Since, this extra/new strand dispatch would happen in timeout, the overhead should be negligible i think. Moreover, the coroutime is already running on strand (make_strand(ctxt)) the new change will just re-use it.
I'd agree with that. The cancel_after approach is a good one but doesn't change/void the serialization requirement I believe. if the cancellation notification isn't serialized with the operation it's cancelling, the same race would exists. Additionally, cancel_after doesn't do shutdown(), which is intentional here to prevent half-open connections from continuing to send data I think? Let me take a look at TSAN option in the meantime. |
Agree, using the strand is simpler in our case here. I rethink the entire workflow if both the timeout handler and the http::async_* completion handler are dispatched to the strand, it shouldn't add much cost, since whichever gets executed first will cancel the other. The only added cost here is an extra cycle to check the error code of the later-dispatched handler of the two. And the scenarios of both handlers are dispatched should be rare. |
|
Added a TSAN test in commit 2da62b049eba937edb41184c8a0b2d2ae159deff that tries to simulate what we'd be seeing in real life. To sum up, Neither cancel() nor shutdown() is the issue here. Both are atomic syscalls. The race should be in the boost::beast HTTP parser's internal state. When timeout_handler is fired on a different thread (when context.get_executor() is used), it calls socket.cancel() which triggers the coroutine to wake and continue parsing. However, the timeout handler and coroutine now race on shared Connection state: e.g., in the crash analyzed in the tracker, we saw the parser's it/last pointers corrupted with ASCII data ("IDJRZtX-"). Using yield.get_executor() makes sure the timeout handler is dispatched through the coroutine's strand, serializing all access to the connection and parser state. So, the new test shows
So, timeout_handler and coroutine never overlap - they take turns on the strand. The test reproduces the issue shows:
Bottomline, with the fix, the timeout_handler waits in the strand queue until the coroutine yields at an async operation (like async_read), then executes cancel()/shutdown(). The coroutine is never interrupted mid-parse. BTW, running the test Line 114, e,g, Indeed what we expected to see race and tsan captured it. PerformanceAgain. timer handler is meant to run serialized with the coroutine, while this change ensures it and the strand should add no overhead when there's no contention (which is the common case as timeout rarely fires). |
|
@cbodley / @jzhu116-bloomberg - Following up on this PR. To summarize where we are: Added the TSAN test results in the previous comment demonstrating the race is in shared Connection/parser state, not in cancel()/shutdown() themselves. The timeout handler and coroutine access Connection concurrently when not serialized through the strand. In terms of the next steps:
Let me know if you'd like any additional analysis or if there are other approaches you'd prefer to explore. |
@mkogan1 could you please offer some advice on measuring the difference in cpu usage here? would we just look at small object upload latency at sufficient scale? do you know if there's still hardware available in the upstream lab for performance testing? |
|
@cbodley sure - can review (currently have one beefy machine in the IBM cloud, will slot a test run for this next week) will try to run a hsbench 4KB PUTs at close to the 1000 rgw_max_concurrent_requests concurrency and monitor the Operations per sec & all the hsbnch latency percentile breakdown and monitor the RGW process CPU usage during the run (on main before and after this PR commits cherry-pick) |
thanks @mkogan1! you had done similar investigation in #43761 (comment) - i think the cpu usage graphs were very helpful there |
|
This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved |
|
@BBoozmen the unit tests were obviously helpful in investigating this issue, but i don't see a ton of value in merging them. it looks to me like they're just setting up a data race to verify that tsan identifies it. and because they aren't testing the same data race we saw and fixed in the frontend, it won't help us catch regressions there in the future |
2da62b0 to
86ad0b8
Compare
Yeap, removed the testing commits. |
@cbodley performed a habench run of 500000 4KB objs PUT using 1000 threads there does not seem to be a pronounced difference in CPU utilization on main without the PR cherry-pick (left side of graph) and with the PR cherry-pick (right side of the graph) (the test system was not CPU/IO/MEM constrained during the run) |
|
https://pulpito.ceph.com/bcs-ceph-2026-03-24_17:03:56-rgw-wip-oozmen-testing-2026-03-23-2115-distro-default-trial/ is the teuthology rgw suite submission. I sent another submission later (https://pulpito.ceph.com/bcs-ceph-2026-03-25_01:00:31-rgw-wip-oozmen-testing-2026-03-23-2115-distro-default-trial/detail), for some reason my Comparing both runs, the high level result summary is very similar: Total Tests: 71 | Passed: 46 | Failed: 25 | Dead: 0 But, the tests-failed differ a bit which is good to eliminate some of the analysis I guess:
I think we can consider rgw/websitergw/website: failed on ubuntu and passed on rocky.
Ubuntu run failed miserably due to DNS issue I think as the failure is Doesn't look an issue with the PR but more of an infrastructure issue. rgw/d4nrgw/d4n: passed on ubuntu and failed on rocky again due to failing to find the redis pkg: rgw/upgrade & rgw/verifyAll 17 failures (4 upgrade + 13 verify) are exclusively on Rocky 10.1 and these suites pass for ubuntu. Rocky failures are due to missing dependencies: e.g. or This is a known issue: https://tracker.ceph.com/issues/72500 These leave notification and multisite tests. rgw/multisiteSo, we have 4 multisite runs: 2 submissions x each 2 runs (ubuntu+rocky)...Looking at the failed testcases across 4 runs:
I don't see any open tracker for and Maybe a timing issue - not waiting long enough to sync to complete but then there's only a few objects. E.g., looking at Time-window leading to fail is pretty small. rgw/notificationAgain, dependency issues are seen: 4 pytest failures (all same import error) and 1 erlang package failure (infrastructure) or There's also
If no real tests ran, why the valgrind failure, though? And, why only on Rocky? Didn't analyze in deep but more than likely during prepare/shutdown phase and why Rocky only - maybe due to Rocky using different compliler/toolchain? Next Steps
|
thanks, i was confused by that ubuntu failure and hadn't seen it before. the rgw/website suite uses dnsmasq to set up wildcard dns names for |
this failure for rgw/upgrade is because it's trying to install nonexistent rocky10 packages for squid. i opened #68001 to address this, but it's still expected to fail due to boto2 without ceph/ragweed#29 |
Hmm, let me also check (dig into logs a bit more) whether this warrants another tracker. |
may be related to recent changes from #66625 |
|
Re-tested this PR in the integration test branch https://github.com/ceph/ceph-ci/commits/wip-oozmen-testing-2026-04-16-0021/ which includes 5 PRs including this one:
Results are at https://pulpito.ceph.com/bcs-ceph-2026-04-16_11:07:58-rgw-wip-oozmen-testing-2026-04-16-0021-distro-default-trial/ which showed 5 failures and
Also took a look at another job from anrao19 as a baseline/comparison-point (which runs on ceph-ci branch https://github.com/ceph/ceph-ci/commits/wip-anrao10-testing-2026-04-14-1759/ which does not include the first 2 PRs but the other 3 of them: https://pulpito.ceph.com/anuchaithra-2026-04-15_04:52:32-rgw-wip-anrao10-testing-2026-04-14-1759-distro-default-trial/ At the high level:
Focusing on wip-oozmen-testing-2026-04-16-0021 5 failures:
multisitecentos -> two zones (passed): ubuntu -> two zonegroups: 1 test (test_copy_object_different_bucket) failed (in re-run this testcase PASSED) Only 1 test (test_copy_object_different_bucket) failed and it's a known test: https://tracker.ceph.com/issues/75716. Although above test passed in the re-run, 2 other tests failed: They are also known/flaky issues tracked at
verify (re-run passed)Only 1 of the 22 verify suites failed: https://pulpito.ceph.com/bcs-ceph-2026-04-16_11:07:58-rgw-wip-oozmen-testing-2026-04-16-0021-distro-default-trial/155891/ Post-teardown valgrind check failed otherwise all tests passed. Every test suite in this verify job completed cleanly. The job then went into teardown, and only then did valgrind fire the Leak_DefinitelyLost exception at 17:30 — purely a post-run memory check failure, not a functional test failure. I believe ProtocolV2 is the internal Ceph messenger protocol, not the HTTP/S3 frontend related so I don't think this failure is related to any of the included PRs. Moreover, the re-run passed. rgw/bucket-logging failure (passed in re-run)rgw/bucket-logging -> EC -> rocky -> PASSED For the replicated pool run, just 1 test fails: test_bucket_logging_cleanup_concurrent_updating_j Looks like the issue is related to Journal + concurrent config update path:
I think EC case passes because writes are slower? I think re-run shows this is due to race across these 5 threads and not-related to this PR. rgw/notifications2 of the 5 notifications suites show FAILED. (failed the same way in re-run) rgw/notifications -> test_kafka: actually tests passed and again clean up issue so can be ignored I think for the purposes of evaluating this PR: (passed in re-run) rgw/notifications -> test_ampq: task setup failed: ConclusionI believe the failures mentioned above are not directly relevant to this PR. @cbodley - would you mind taking a look at the test results real quick? I believe this is ready-to-merge. |
|
tyvm @BBoozmen @jzhu116-bloomberg @mkogan1 for all the hard work on this |


Fixes https://tracker.ceph.com/issues/75031.
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
xbetween the brackets:[x]. Spaces and capitalization matter when checking off items this way.Checklist
Show available Jenkins commands
jenkins test classic perfJenkins Job | Jenkins Job Definitionjenkins test crimson perfJenkins Job | Jenkins Job Definitionjenkins test signedJenkins Job | Jenkins Job Definitionjenkins test make checkJenkins Job | Jenkins Job Definitionjenkins test make check arm64Jenkins Job | Jenkins Job Definitionjenkins test submodulesJenkins Job | Jenkins Job Definitionjenkins test dashboardJenkins Job | Jenkins Job Definitionjenkins test dashboard cephadmJenkins Job | Jenkins Job Definitionjenkins test apiJenkins Job | Jenkins Job Definitionjenkins test docsReadTheDocs | Github Workflow Definitionjenkins test ceph-volume allJenkins Jobs | Jenkins Jobs Definitionjenkins test windowsJenkins Job | Jenkins Job Definitionjenkins test rook e2eJenkins Job | Jenkins Job DefinitionYou must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.