Skip to content

fix: isolate invalid custom analytics event data - #603

Closed
pcbeingused33 wants to merge 1 commit into
launchdarkly:mainfrom
pcbeingused33:fix/c8322-nonfinite-track-data
Closed

pcbeingused33 wants to merge 1 commit into
launchdarkly:mainfrom
pcbeingused33:fix/c8322-nonfinite-track-data

Conversation

@pcbeingused33

@pcbeingused33 pcbeingused33 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions (tested on one Python version only)
  • Sync/async parity: matching changes made to async_* siblings (or N/A)

Related issues

No issue in this repository. Follow-up to #599, which rejects non-finite metric_value in track() but does not inspect data.

Describe the solution you've provided

Python's JSON encoder writes NaN, Infinity and -Infinity by default. A non-finite number nested in the data passed to track() is copied into the custom event as-is, and the event processor serializes the whole batch with json.dumps, so that one value makes the request body for the entire flush invalid JSON. A value that cannot be encoded at all (for example a datetime) makes json.dumps raise, and the flush is dropped.

The shared event formatter (EventOutputFormatter, used by both the sync and async processors) now checks custom event data with json.dumps(..., allow_nan=False). If that fails, it logs a warning naming the event and leaves that event out, so the rest of the batch is still sent. Dropping the event rather than its data matches what #599 does for non-finite metric values.

New sync and async tests flush a batch containing one custom event with nested NaN, +Infinity or -Infinity and one valid custom event over mock HTTP. They check that the payload encodes as strict JSON and still contains the valid event with its data. They fail without the change and pass with it; the event processor test suites pass.

Describe alternatives you've considered

  • Checking data in track(), as fix: Drop track events with a NaN or infinite metric value #599 does for metric_value: this would serialize the data on the caller's thread for every call, while the formatter already runs on the flush path and covers both clients in one place.
  • Passing allow_nan=False to the batch json.dumps: the whole batch would then fail to serialize.

Additional context

This change was prepared with the assistance of an AI coding agent.


Note

Overview
Custom events whose data cannot be encoded as strict JSON (e.g. nested NaN, Infinity, or non-JSON-serializable values) were previously included in the flush payload and could invalidate or drop the entire batch when the processor serializes it.

EventOutputFormatter (shared by sync and async processors) now pre-validates custom data with json.dumps(..., allow_nan=False). Failed events are omitted with a warning keyed by event name; make_output_events skips None results so the rest of the batch still ships.

New sync and async tests assert that a batch mixing one invalid and one valid custom event produces strict JSON and only delivers the valid event.

Reviewed by Cursor Bugbot for commit fba31a2. Bugbot is set up for automated code reviews on this repo. Configure here.

@pcbeingused33
pcbeingused33 requested a review from a team as a code owner December 2, 2026 13:07
@keelerm84

Copy link
Copy Markdown
Member

Thanks for the detailed write-up and the sync and async tests.

We discussed this as a team and have decided not to take this change. We do want to drop only the bad events instead of the whole payload. Doing that well needs a broader change to how the flush path builds the payload, and we would rather design that properly than add a second encoding pass. Re-encoding also has its own failure modes. For example, an event can pass the per-event check and still fail when the batch is encoded again, and then the whole batch is lost anyway.

For now, we plan to keep the current behavior along with the metric_value check from #599. We also don't want to validate data on every track call, because walking nested values on the caller's thread costs too much for a rare case.

We'll close this for now. Thanks again for raising it.

@keelerm84 keelerm84 closed this Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants