Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/resolute-queen-loviatar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Treat NaN exception rate-limit settings as invalid numbers so autocapture keeps working and bucket limits remain finite.
7 changes: 6 additions & 1 deletion posthog/bucketed_rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Kept behaviorally identical so rate limiting is consistent across SDKs.

import logging
import math
import threading
import time
from typing import Callable, Dict, Hashable, Optional, Union
Expand All @@ -15,7 +16,11 @@


def _clamp_to_range(value, min_value: Number, max_value: Number, label: str) -> Number:
if isinstance(value, bool) or not isinstance(value, (int, float)):
if (
isinstance(value, bool)
or not isinstance(value, (int, float))
or (isinstance(value, float) and math.isnan(value))
):
log.warning(f"{label} must be a number. Using max value {max_value}.")
return max_value
if value > max_value:
Expand Down
32 changes: 32 additions & 0 deletions posthog/test/test_bucketed_rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,38 @@ def test_clamps_out_of_range_options():
assert limiter._refill_interval == 86400.0


@pytest.mark.parametrize(
"option, attribute, fallback",
[
("bucket_size", "_bucket_size", 100),
("refill_rate", "_refill_rate", 10),
("refill_interval_seconds", "_refill_interval", 86400.0),
],
)
def test_nan_options_use_invalid_number_fallback(option, attribute, fallback, caplog):
limiter = make_limiter(FakeClock(), **{option: float("nan")})

assert getattr(limiter, attribute) == fallback
assert f"{option} must be a number" in caplog.text


@pytest.mark.parametrize(
"value, expected",
[
(0, 0),
(10**1000, 100),
(float("inf"), 100),
(float("-inf"), 0),
(True, 100),
(False, 100),
],
)
def test_bucket_size_preserves_existing_clamping(value, expected):
limiter = make_limiter(FakeClock(), bucket_size=value)

assert limiter._bucket_size == expected


def test_thread_safety_allows_exactly_bucket_size_minus_one():
limiter = make_limiter(FakeClock(), bucket_size=50)
allowed = []
Expand Down
40 changes: 40 additions & 0 deletions posthog/test/test_exception_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,46 @@ def test_client_passes_rate_limiter_configuration_through():
client.shutdown()


@pytest.mark.parametrize(
"option, attempts, expected_uploads",
[
("exception_autocapture_refill_interval_seconds", 6, 2),
("exception_autocapture_bucket_size", 105, 99),
],
)
def test_nan_limiter_configuration_keeps_exception_hook_working(
monkeypatch, caplog, option, attempts, expected_uploads
):
from posthog import Posthog

previous_hook = MagicMock()
upload = MagicMock()
monkeypatch.setattr(sys, "excepthook", previous_hook)
monkeypatch.setattr("posthog.client.batch_post", upload)
options = {
"exception_autocapture_bucket_size": 3,
"exception_autocapture_refill_interval_seconds": 60,
option: float("nan"),
}
client = Posthog(
"phc_test",
sync_mode=True,
enable_exception_autocapture=True,
enable_exception_autocapture_rate_limiting=True,
**options,
)
try:
for _ in range(attempts):
error = ValueError("boom")
sys.excepthook(type(error), error, None)

assert upload.call_count == expected_uploads
assert previous_hook.call_count == attempts
assert "Failed to capture exception" not in caplog.text
finally:
client.shutdown()


def test_rate_limits_per_exception_type():
from posthog.exception_capture import ExceptionCapture

Expand Down
Loading