Skip to content

fix: Drop track events with a NaN or infinite metric value - #599

Open
pcbeingused333 wants to merge 1 commit into
launchdarkly:mainfrom
pcbeingused333:fix/track-non-finite-metric
Open

pcbeingused333 wants to merge 1 commit into
launchdarkly:mainfrom
pcbeingused333:fix/track-non-finite-metric

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 27, 2026 •

Copy link
Copy Markdown

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. Same fix as launchdarkly/go-server-sdk#520 and launchdarkly/ruby-server-sdk#498.

Describe the solution you've provided

track() with a NaN or infinite metric_value puts the value straight into the custom event. The event processor serializes the whole batch with json.dumps, which by default writes the non-standard tokens NaN, Infinity and -Infinity, so the request body for that flush is not valid JSON and the other events in it are at risk too.

LDClient.track and AsyncLDClient.track now check for a non-finite float after the existing context check. In that case they log a warning that names the event and do not record it. Other values behave as before, and data is not inspected. The docstrings mention the new behavior.

New tests cover NaN, +Infinity and -Infinity on both clients. They fail without the change and pass with it. The client, async client and hook tests pass, and mypy, isort and pycodestyle are clean on the changed source files.

Describe alternatives you've considered

  • Sending the event without metricValue: this would silently report a different measurement.
  • Passing allow_nan=False to json.dumps: the whole batch would then fail to serialize.

Additional context

This change was prepared with the assistance of an AI coding agent (Claude Code).


Note

Overview
track() on LDClient and AsyncLDClient now rejects float **metric_value**s that are NaN or ±Infinity: it logs a warning naming the event and does not enqueue the custom event.

This matches other LaunchDarkly server SDKs and avoids event batches that json.dumps would emit with non-standard tokens, which can invalidate the flush payload and jeopardize co-batched events. Docstrings document the behavior; new parametrized tests cover sync and async clients.

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

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 27, 2026 06:54
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.

1 participant