From fd668655a8042f2e88e78087c285f89bc1ede6f4 Mon Sep 17 00:00:00 2001 From: Aditya Gupta <84338325+MaiAditya@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:25:11 +0530 Subject: [PATCH] fix: mark an exception taken from sys.exc_info() as captured capture_exception() falls back to sys.exc_info() when no exception is passed, but only marked a passed exception as captured. A later capture of the same exception (a new_context exit, a framework integration, the excepthook, or a second call) then sent a duplicate $exception event. Mark the resolved exc_info, and skip a sys.exc_info() exception that is already marked. Client and AsyncClient both had the bug. --- .../capture-exception-no-arg-dedupe.md | 5 ++ posthog/async_client.py | 10 +++- posthog/client.py | 11 +++-- posthog/test/test_exception_capture.py | 46 +++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 .sampo/changesets/capture-exception-no-arg-dedupe.md diff --git a/.sampo/changesets/capture-exception-no-arg-dedupe.md b/.sampo/changesets/capture-exception-no-arg-dedupe.md new file mode 100644 index 000000000..c0fb9c7ad --- /dev/null +++ b/.sampo/changesets/capture-exception-no-arg-dedupe.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +`capture_exception()` called without an exception now marks the exception it takes from `sys.exc_info()` as captured, so a later automatic capture of the same exception no longer sends a second `$exception` event. diff --git a/posthog/async_client.py b/posthog/async_client.py index ae63d0853..8572921d3 100644 --- a/posthog/async_client.py +++ b/posthog/async_client.py @@ -928,8 +928,10 @@ def capture_exception( flags=kwargs.get("flags"), disable_geoip=kwargs.get("disable_geoip"), ) - if exception is not None and result is not None: - mark_exception_as_captured(exception, result) + # Mark the exception as captured to prevent duplicate captures, + # including one taken from sys.exc_info() + if result is not None: + mark_exception_as_captured(exc_info, result) return result except Exception as error: self.log.exception("Failed to capture exception: %s", error) @@ -983,6 +985,10 @@ def _exception_info(self, exception: Optional[ExceptionArg]): self.log.warning("No exception information available") return None + if exception is None and exception_is_already_captured(exc_info): + self.log.debug("Exception already captured, skipping") + return None + return exc_info def _get_feature_flag_evaluations_host(self) -> _FeatureFlagEvaluationsHost: diff --git a/posthog/client.py b/posthog/client.py index f4b6591fc..84251f16a 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -2932,9 +2932,10 @@ def capture_exception( disable_geoip=disable_geoip, ) - # Mark the exception as captured to prevent duplicate captures - if exception is not None and res is not None: - mark_exception_as_captured(exception, res) + # Mark the exception as captured to prevent duplicate captures, + # including one taken from sys.exc_info() + if res is not None: + mark_exception_as_captured(exc_info, res) return res except Exception as e: @@ -2956,6 +2957,10 @@ def _exception_info(self, exception): self.log.warning("No exception information available") return None + if exception is None and exception_is_already_captured(exc_info): + self.log.debug("Exception already captured, skipping") + return None + return exc_info def _exception_properties(self, exc_info, properties, kwargs): diff --git a/posthog/test/test_exception_capture.py b/posthog/test/test_exception_capture.py index e491b606e..1485ffff9 100644 --- a/posthog/test/test_exception_capture.py +++ b/posthog/test/test_exception_capture.py @@ -743,3 +743,49 @@ def test_never_raised_exception_stack_omits_sdk_frames_in_renamed_package(): functions = [frame["function"] for frame in exception["stacktrace"]["frames"]] assert functions[-1] == "handler" assert exception["mechanism"]["synthetic"] is True + + +def _captured_events(client_class, capture_handled, pass_exception): + from posthog.async_client import AsyncClient + from posthog.client import Client + + clients = {"Client": Client, "AsyncClient": AsyncClient} + client = clients[client_class]("phc_test", send=False) + with patch.object(client, "capture", wraps=client.capture) as capture: + try: + capture_handled(client, pass_exception) + except ZeroDivisionError: + pass + return [call.args[0] for call in capture.call_args_list] + + +def _handled_then_reraised_in_context(client, pass_exception): + from posthog.contexts import new_context + + with new_context(capture_exceptions=True, client=client): + try: + 1 / 0 + except ZeroDivisionError as error: + client.capture_exception(error if pass_exception else None) + raise + + +def _handled_twice(client, pass_exception): + try: + 1 / 0 + except ZeroDivisionError as error: + client.capture_exception(error if pass_exception else None) + client.capture_exception(error if pass_exception else None) + + +@pytest.mark.parametrize("client_class", ["Client", "AsyncClient"]) +@pytest.mark.parametrize("pass_exception", [True, False]) +@pytest.mark.parametrize( + "capture_handled", [_handled_then_reraised_in_context, _handled_twice] +) +def test_exception_from_sys_exc_info_is_captured_once( + client_class, capture_handled, pass_exception +): + assert _captured_events(client_class, capture_handled, pass_exception) == [ + "$exception" + ]