yiguolei commented on code in PR #57275:
URL: https://github.com/apache/doris/pull/57275#discussion_r2458733094


##########
be/src/util/defer_op.h:
##########
@@ -29,12 +32,44 @@ namespace doris {
 // for C++11
 // auto op = [] {};
 // Defer<decltype<op>> (op);
+// Defer runs a closure in its destructor. By default destructors must not
+// let exceptions escape during stack unwinding (that would call
+// std::terminate). This implementation tries to strike a balance:
+// - If the destructor is running while there is an active exception
+//   (std::uncaught_exceptions() > 0), we call the closure but catch and
+//   swallow any exception to avoid terminate.
+// - If there is no active exception, we invoke the closure directly and
+//   allow any exception to propagate to the caller. To permit propagation
+//   we declare the destructor noexcept(false).
 template <class T>
 class Defer {
 public:
     Defer(T& closure) : _closure(closure) {}
     Defer(T&& closure) : _closure(std::move(closure)) {}
-    ~Defer() { _closure(); }
+    // Allow throwing when there is no active exception. If we are currently
+    // unwinding (std::uncaught_exceptions() > 0), swallow exceptions from
+    // the closure to prevent std::terminate.
+    ~Defer() noexcept(false) {
+        if (std::uncaught_exceptions() > 0) {
+            try {
+                _closure();
+            } catch (...) {
+                // swallow: cannot safely rethrow during stack unwind
+                // Log the exception for debugging. Try to get 
std::exception::what()
+                try {
+                    throw;
+                } catch (const std::exception& e) {
+                    LOG(WARNING) << "Exception swallowed in Defer destructor 
during unwind: "
+                                 << e.what();

Review Comment:
   In this case, should print stack here, we should know where the exception 
happens.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to