Skip to content

Commit ae2b0bd

Browse files
committed
fix: log feature usage callback errors in the sync client
_feature_usage swallowed exceptions with a bare pass, so a failing sink set via set_feature_usage_callback (or on_feature_usage) left no trace, while the README says callback errors are logged. Log them the way the async client already does.
1 parent cf02da8 commit ae2b0bd

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎growthbook/growthbook.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,8 @@ def _feature_usage(self, key: str, result: FeatureResult[Any], user_context: Use
16201620
# for the evaluation, even if the caller mutates them afterwards.
16211621
self._featureUsageCallback(key, result, tracking_user_context(user_context))
16221622
except Exception:
1623-
pass
1623+
# Logged like the async client does, so a failing sink is visible.
1624+
logger.exception("Error in feature usage callback")
16241625

16251626
@deprecated("getAllResults is deprecated, use get_all_results instead")
16261627
def getAllResults(self) -> Dict[str, Dict[str, Any]]:

‎tests/test_growthbook.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,7 @@ def test_setting_tracking_callback_does_not_flush_deferred_buffer():
22002200
gb.destroy()
22012201

22022202

2203-
def test_throwing_runtime_callback_cannot_break_evaluation():
2203+
def test_throwing_runtime_callback_cannot_break_evaluation(caplog):
22042204
gb = GrowthBook(attributes={"id": "1"}, features={"flag": {"defaultValue": 7}})
22052205

22062206
def boom(*args, **kwargs):
@@ -2209,8 +2209,14 @@ def boom(*args, **kwargs):
22092209
gb.set_tracking_callback(boom)
22102210
gb.set_feature_usage_callback(boom)
22112211

2212-
assert gb.run(Experiment(key="safe", variations=[0, 1])).value in (0, 1)
2213-
assert gb.get_feature_value("flag", 0) == 7
2212+
with caplog.at_level("ERROR", logger="growthbook"):
2213+
assert gb.run(Experiment(key="safe", variations=[0, 1])).value in (0, 1)
2214+
assert gb.get_feature_value("flag", 0) == 7
2215+
2216+
# Both failures are logged, as the README promises.
2217+
failures = [r for r in caplog.records if r.exc_info and "sink exploded" in str(r.exc_info[1])]
2218+
assert len(failures) == 2
2219+
assert any(r.getMessage() == "Error in feature usage callback" for r in failures)
22142220

22152221
gb.destroy()
22162222

0 commit comments

Comments
 (0)