From dc03ea718cc644ff8403460876bf378f6cee2d47 Mon Sep 17 00:00:00 2001 From: Nicholas Tindle Date: Tue, 9 Sep 2025 06:56:51 +0200 Subject: [PATCH] fix(backend): Report process errors to Sentry before cleanup (#10873) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds error reporting to Sentry for exceptions (excluding KeyboardInterrupt and SystemExit) before executing process cleanup. Silently ignores failures if Sentry is unavailable. ### Changes 🏗️ Adds cleanup for sentry Adds disabling for sentry ### Checklist 📋 #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - [x] Test all services with manual exception raising - [x] Remove those excptions - [x] Make sure they show up in sentry --- autogpt_platform/backend/backend/util/metrics.py | 2 +- autogpt_platform/backend/backend/util/process.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/autogpt_platform/backend/backend/util/metrics.py b/autogpt_platform/backend/backend/util/metrics.py index ff92537d4e..2fb0d53e7a 100644 --- a/autogpt_platform/backend/backend/util/metrics.py +++ b/autogpt_platform/backend/backend/util/metrics.py @@ -33,7 +33,7 @@ def sentry_init(): ) -def sentry_capture_error(error: Exception): +def sentry_capture_error(error: BaseException): sentry_sdk.capture_exception(error) sentry_sdk.flush() diff --git a/autogpt_platform/backend/backend/util/process.py b/autogpt_platform/backend/backend/util/process.py index 4e37f960d8..d345d2d0f4 100644 --- a/autogpt_platform/backend/backend/util/process.py +++ b/autogpt_platform/backend/backend/util/process.py @@ -76,6 +76,14 @@ class AppProcess(ABC): logger.warning( f"[{self.service_name}] Termination request: {type(e).__name__}; {e} executing cleanup." ) + # Send error to Sentry before cleanup + if not isinstance(e, (KeyboardInterrupt, SystemExit)): + try: + from backend.util.metrics import sentry_capture_error + + sentry_capture_error(e) + except Exception: + pass # Silently ignore if Sentry isn't available finally: self.cleanup() logger.info(f"[{self.service_name}] Terminated.")