Skip to content

Commit

Permalink
semaphore: expiry_handler: tunnel exception_ptr to entry
Browse files Browse the repository at this point in the history
Accept an optional exception_ptr in the expiry_handler
and pass it on to the entry promise, so that the original
abort exception can be passed to the user.

This way we can distinguish between, e.g. timed_out_error
and abort_requested_error.

Signed-off-by: Benny Halevy <[email protected]>
  • Loading branch information
bhalevy committed Jan 9, 2025
1 parent d415e57 commit f8c36db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/seastar/core/semaphore.hh
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ private:
}
struct expiry_handler {
basic_semaphore& sem;
void operator()(entry& e) noexcept {
void operator()(entry& e, const std::optional<std::exception_ptr>& ex) noexcept {
if (e.timer) {
e.pr.set_exception(sem.get_timeout_exception());
} else if (ex) {
e.pr.set_exception(*ex);
} else if (sem._ex) {
e.pr.set_exception(sem._ex);
} else {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/semaphore_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <seastar/core/sleep.hh>
#include <seastar/core/shared_mutex.hh>
#include <ranges>
#include <stdexcept>

#include "expected_exception.hh"

Expand Down Expand Up @@ -428,6 +429,19 @@ SEASTAR_THREAD_TEST_CASE(test_semaphore_abort_after_wait) {
BOOST_REQUIRE_EQUAL(x, 0);
}

SEASTAR_THREAD_TEST_CASE(test_semaphore_abort_with_exception_after_wait) {
auto sem = semaphore(0);
abort_source as;
int x = 0;
auto fut1 = sem.wait(as).then([&x] {
x++;
});
as.request_abort_ex(expected_exception());
sem.signal();
BOOST_CHECK_THROW(fut1.get(), expected_exception);
BOOST_REQUIRE_EQUAL(x, 0);
}

SEASTAR_THREAD_TEST_CASE(test_semaphore_abort_before_wait) {
auto sem = semaphore(0);
abort_source as;
Expand Down

0 comments on commit f8c36db

Please sign in to comment.