From dbc9569775f71af2e1250b3cbf83ba4503078325 Mon Sep 17 00:00:00 2001 From: Hayodea Hakol Date: Sun, 21 Sep 2025 14:17:23 -0400 Subject: [PATCH] Async: Add exception bubbling CONT_SET_EXC: Set exception on the continuation, to be rethrown by the caller. CONT_SET_EXC_AND_RET: Convenience which returns immediately after setting the exception. --- include/asynchronousContinuation.h | 32 ++++++++++++++++++++ include/serializedAsynchronousContinuation.h | 1 - 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/asynchronousContinuation.h b/include/asynchronousContinuation.h index 95f2793..407318d 100644 --- a/include/asynchronousContinuation.h +++ b/include/asynchronousContinuation.h @@ -3,9 +3,11 @@ #include #include +#include #include #include + namespace smo { /** @@ -33,8 +35,38 @@ public: std::shared_ptr> lifetimePreservingConveyance); + + /** EXPLANATION: + * When an exception is thrown in a an async callee, which pertains to an + * error in the data given by the caller, we ought not to throw the + * exception within the callee. Instead, we should store the exception + * in the continuation object and return it to the caller. + * + * The caller should then call checkException() to rethrow it on its + * own stack. + * + * This macro should be used by the caller to bubble the exception to the + * caller. + */ + #define CONT_SET_EXC(continuation, type, exc_obj) \ + (continuation)->exception = std::make_exception_ptr(exc_obj) + + #define CONT_SET_EXC_AND_RET(continuation, type, exc_obj) \ + do { \ + (continuation)->exception = std::make_exception_ptr(exc_obj); \ + return; \ + } while(0) + + // Call this in the caller to rethrow the exception. + void checkException() + { + if (exception) + { std::rethrow_exception(exception); } + } + public: OriginalCbFnT originalCbFn; + std::exception_ptr exception; }; /** diff --git a/include/serializedAsynchronousContinuation.h b/include/serializedAsynchronousContinuation.h index e632ebf..a8cded8 100644 --- a/include/serializedAsynchronousContinuation.h +++ b/include/serializedAsynchronousContinuation.h @@ -11,7 +11,6 @@ namespace smo { - template class SerializedAsynchronousContinuation : public PostedAsynchronousContinuation