From 5b7a91a31f167090565c2934afa6c99a33207898 Mon Sep 17 00:00:00 2001 From: Aditya Gupta <84338325+MaiAditya@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:51:15 +0530 Subject: [PATCH 1/5] fix: mark an exception taken from sys.exc_info() as captured (#1267) 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" + ] From e401aeed00cf747ff84f17a11aaa2860de3163cb Mon Sep 17 00:00:00 2001 From: "releaser-posthog-python[bot]" <262414804+releaser-posthog-python[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:24:59 +0000 Subject: [PATCH 2/5] chore: Release v7.91.3 [skip ci] --- .sampo/changesets/capture-exception-no-arg-dedupe.md | 5 ----- openfeature-provider/CHANGELOG.md | 6 ++++++ openfeature-provider/pyproject.toml | 2 +- posthog/CHANGELOG.md | 6 ++++++ posthog/version.py | 2 +- pyproject.toml | 2 +- uv.lock | 4 ++-- 7 files changed, 17 insertions(+), 10 deletions(-) delete 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 deleted file mode 100644 index c0fb9c7ad..000000000 --- a/.sampo/changesets/capture-exception-no-arg-dedupe.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -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/openfeature-provider/CHANGELOG.md b/openfeature-provider/CHANGELOG.md index dd1423d8f..51b52a682 100644 --- a/openfeature-provider/CHANGELOG.md +++ b/openfeature-provider/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to `openfeature-provider-posthog` are documented here. This file is maintained by [Sampo](https://github.com/bruits/sampo) from changesets in `.sampo/changesets/` that target `pypi/openfeature-provider-posthog`. +## 0.1.166 — 2026-11-17 + +### Patch changes + +- Updated dependencies: posthog@7.91.3 + ## 0.1.165 — 2026-11-16 ### Patch changes diff --git a/openfeature-provider/pyproject.toml b/openfeature-provider/pyproject.toml index 02e1dcadb..e2d864055 100644 --- a/openfeature-provider/pyproject.toml +++ b/openfeature-provider/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "openfeature-provider-posthog" -version = "0.1.165" +version = "0.1.166" description = "Official PostHog provider for the OpenFeature Python SDK." readme = "README.md" authors = [{ name = "PostHog", email = "engineering@posthog.com" }] diff --git a/posthog/CHANGELOG.md b/posthog/CHANGELOG.md index 9d9cd52c4..ac47182b4 100644 --- a/posthog/CHANGELOG.md +++ b/posthog/CHANGELOG.md @@ -1,5 +1,11 @@ # posthog +## 7.91.3 — 2026-11-17 + +### Patch changes + +- [5b7a91a3](https://github.com/posthog/posthog-python/commit/5b7a91a31f167090565c2934afa6c99a33207898) `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. — Thanks @MaiAditya for your first contribution 🎉! + ## 7.91.2 — 2026-11-16 ### Patch changes diff --git a/posthog/version.py b/posthog/version.py index d29da7e01..368b9572d 100644 --- a/posthog/version.py +++ b/posthog/version.py @@ -1 +1 @@ -VERSION = "7.91.2" +VERSION = "7.91.3" diff --git a/pyproject.toml b/pyproject.toml index 99b3f6286..14089638b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "posthog" -version = "7.91.2" +version = "7.91.3" description = "Integrate PostHog into any python application." authors = [{ name = "PostHog", email = "engineering@posthog.com" }] maintainers = [{ name = "PostHog", email = "engineering@posthog.com" }] diff --git a/uv.lock b/uv.lock index e6ced52b1..dc834c449 100644 --- a/uv.lock +++ b/uv.lock @@ -2907,7 +2907,7 @@ wheels = [ [[package]] name = "openfeature-provider-posthog" -version = "0.1.165" +version = "0.1.166" source = { editable = "openfeature-provider" } dependencies = [ { name = "openfeature-sdk" }, @@ -3464,7 +3464,7 @@ wheels = [ [[package]] name = "posthog" -version = "7.91.2" +version = "7.91.3" source = { editable = "." } dependencies = [ { name = "backoff" }, From 8a88a289fe530d9983ddc5531480e3c84490d1ae Mon Sep 17 00:00:00 2001 From: TORANOSUKE TANIGAKI <155611269+Toraastrta@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:27:28 +0900 Subject: [PATCH 3/5] fix(exceptions): send truncated source lines instead of null (#1265) get_lines_from_file passed each line through strip_string, which wraps a truncated line in an AnnotatedValue. Nothing unwrapped it, so clean() turned it into None and lines over 1024 bytes reached the event as null in context_line, pre_context and post_context. Keep only the truncated string. --- .../exception-long-context-lines.md | 5 +++++ posthog/exception_utils.py | 22 ++++++++++++------- posthog/test/test_exception_utils.py | 17 ++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 .sampo/changesets/exception-long-context-lines.md diff --git a/.sampo/changesets/exception-long-context-lines.md b/.sampo/changesets/exception-long-context-lines.md new file mode 100644 index 000000000..ea740a8a0 --- /dev/null +++ b/.sampo/changesets/exception-long-context-lines.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Exception autocapture now sends a truncated string for source lines over 1024 bytes in a frame's `context_line`, `pre_context` and `post_context`. Before, those lines were sent as `null`. diff --git a/posthog/exception_utils.py b/posthog/exception_utils.py index d462d23cc..107973953 100644 --- a/posthog/exception_utils.py +++ b/posthog/exception_utils.py @@ -413,7 +413,7 @@ def get_lines_from_file( loader=None, # type: Optional[Any] module=None, # type: Optional[str] ): - # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]] + # type: (...) -> Tuple[List[str], Optional[str], List[str]] context_lines = 5 source = _get_source_lines(filename, loader, module) @@ -423,14 +423,20 @@ def get_lines_from_file( lower_bound = max(0, lineno - context_lines) upper_bound = min(lineno + 1 + context_lines, len(source)) + def strip_line(line): + # type: (str) -> str + # strip_string wraps a truncated line in an AnnotatedValue, which the + # event payload can't serialize, so keep only the truncated text. + stripped = strip_string(line.strip("\r\n"), max_length=max_length) + if isinstance(stripped, AnnotatedValue): + return cast(str, stripped.value) + return stripped + try: - pre_context = [ - strip_string(line.strip("\r\n"), max_length=max_length) - for line in source[lower_bound:lineno] - ] - context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length) + pre_context = [strip_line(line) for line in source[lower_bound:lineno]] + context_line = strip_line(source[lineno]) post_context = [ - strip_string(line.strip("\r\n"), max_length=max_length) + strip_line(line) for line in source[(lineno + 1) : upper_bound] # noqa: E203 ] return pre_context, context_line, post_context @@ -464,7 +470,7 @@ def get_source_context( tb_lineno, # type: int max_value_length=None, # type: Optional[int] ): - # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]] + # type: (...) -> Tuple[List[str], Optional[str], List[str]] try: abs_path = frame.f_code.co_filename # type: Optional[str] except Exception: diff --git a/posthog/test/test_exception_utils.py b/posthog/test/test_exception_utils.py index e719c8c31..063194618 100644 --- a/posthog/test/test_exception_utils.py +++ b/posthog/test/test_exception_utils.py @@ -61,6 +61,23 @@ def unavailable(module): ) == ([], "current", ["after"]) +def test_source_context_truncates_long_lines_to_strings(tmp_path): + long_line = "x" * 1100 + source = tmp_path / "application.py" + source.write_text("\n".join([long_line, long_line, long_line])) + + pre_context, context_line, post_context = exception_utils.get_lines_from_file( + str(source), 1 + ) + + truncated = "x" * 1021 + "..." + assert (pre_context, context_line, post_context) == ( + [truncated], + truncated, + [truncated], + ) + + def test_source_context_handles_changed_files(tmp_path): source = tmp_path / "application.py" source.write_text("current\n") From 36027e3c99d5bf3e2876b2f18284ec6291c4765f Mon Sep 17 00:00:00 2001 From: "releaser-posthog-python[bot]" <262414804+releaser-posthog-python[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:38:41 +0000 Subject: [PATCH 4/5] chore: Release v7.91.4 [skip ci] --- .sampo/changesets/exception-long-context-lines.md | 5 ----- openfeature-provider/CHANGELOG.md | 6 ++++++ openfeature-provider/pyproject.toml | 2 +- posthog/CHANGELOG.md | 6 ++++++ posthog/version.py | 2 +- pyproject.toml | 2 +- uv.lock | 4 ++-- 7 files changed, 17 insertions(+), 10 deletions(-) delete mode 100644 .sampo/changesets/exception-long-context-lines.md diff --git a/.sampo/changesets/exception-long-context-lines.md b/.sampo/changesets/exception-long-context-lines.md deleted file mode 100644 index ea740a8a0..000000000 --- a/.sampo/changesets/exception-long-context-lines.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -pypi/posthog: patch ---- - -Exception autocapture now sends a truncated string for source lines over 1024 bytes in a frame's `context_line`, `pre_context` and `post_context`. Before, those lines were sent as `null`. diff --git a/openfeature-provider/CHANGELOG.md b/openfeature-provider/CHANGELOG.md index 51b52a682..355c92386 100644 --- a/openfeature-provider/CHANGELOG.md +++ b/openfeature-provider/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to `openfeature-provider-posthog` are documented here. This file is maintained by [Sampo](https://github.com/bruits/sampo) from changesets in `.sampo/changesets/` that target `pypi/openfeature-provider-posthog`. +## 0.1.167 — 2026-11-17 + +### Patch changes + +- Updated dependencies: posthog@7.91.4 + ## 0.1.166 — 2026-11-17 ### Patch changes diff --git a/openfeature-provider/pyproject.toml b/openfeature-provider/pyproject.toml index e2d864055..2e2885b3d 100644 --- a/openfeature-provider/pyproject.toml +++ b/openfeature-provider/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "openfeature-provider-posthog" -version = "0.1.166" +version = "0.1.167" description = "Official PostHog provider for the OpenFeature Python SDK." readme = "README.md" authors = [{ name = "PostHog", email = "engineering@posthog.com" }] diff --git a/posthog/CHANGELOG.md b/posthog/CHANGELOG.md index ac47182b4..c79e2cac6 100644 --- a/posthog/CHANGELOG.md +++ b/posthog/CHANGELOG.md @@ -1,5 +1,11 @@ # posthog +## 7.91.4 — 2026-11-17 + +### Patch changes + +- [8a88a289](https://github.com/posthog/posthog-python/commit/8a88a289fe530d9983ddc5531480e3c84490d1ae) Exception autocapture now sends a truncated string for source lines over 1024 bytes in a frame's `context_line`, `pre_context` and `post_context`. Before, those lines were sent as `null`. — Thanks @Toraastrta for your first contribution 🎉! + ## 7.91.3 — 2026-11-17 ### Patch changes diff --git a/posthog/version.py b/posthog/version.py index 368b9572d..04490f216 100644 --- a/posthog/version.py +++ b/posthog/version.py @@ -1 +1 @@ -VERSION = "7.91.3" +VERSION = "7.91.4" diff --git a/pyproject.toml b/pyproject.toml index 14089638b..732c394ba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "posthog" -version = "7.91.3" +version = "7.91.4" description = "Integrate PostHog into any python application." authors = [{ name = "PostHog", email = "engineering@posthog.com" }] maintainers = [{ name = "PostHog", email = "engineering@posthog.com" }] diff --git a/uv.lock b/uv.lock index dc834c449..04425987e 100644 --- a/uv.lock +++ b/uv.lock @@ -2907,7 +2907,7 @@ wheels = [ [[package]] name = "openfeature-provider-posthog" -version = "0.1.166" +version = "0.1.167" source = { editable = "openfeature-provider" } dependencies = [ { name = "openfeature-sdk" }, @@ -3464,7 +3464,7 @@ wheels = [ [[package]] name = "posthog" -version = "7.91.3" +version = "7.91.4" source = { editable = "." } dependencies = [ { name = "backoff" }, From e2b12f80e1f0cd3c4b6356df8420050ef22d54a6 Mon Sep 17 00:00:00 2001 From: Eli Reisman Date: Tue, 17 Nov 2026 08:55:30 -0800 Subject: [PATCH 5/5] fix: skip an already-captured sys.exc_info() exception in capture_exception Ports #1267 to v1's inline capture_exception, which has no _exception_info helper. Client and AsyncClient both skip it, as on main. Co-Authored-By: Claude Opus 5.5 --- posthog/async_client.py | 4 ++++ posthog/client.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/posthog/async_client.py b/posthog/async_client.py index 65bc57f28..7401e3740 100644 --- a/posthog/async_client.py +++ b/posthog/async_client.py @@ -1022,6 +1022,10 @@ def capture_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 + exceptions = exceptions_from_error_tuple(exc_info) event = handle_in_app( {"exception": {"values": exceptions}}, diff --git a/posthog/client.py b/posthog/client.py index 3762b5481..d71aa13d7 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -2925,6 +2925,10 @@ def capture_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 + # Format stack trace for cymbal capture_metadata_input = dict(kwargs).get("_capture_metadata") capture_metadata = (