Skip to content

fix: Prevent a NaN or infinite metric value from discarding all events in a flush - #498

Merged
keelerm84 merged 1 commit into
launchdarkly:mainfrom
pcbeingused33:fix/track-nonfinite-metric
Nov 20, 2026
Merged

keelerm84 merged 1 commit into
launchdarkly:mainfrom
pcbeingused33:fix/track-nonfinite-metric

Conversation

@pcbeingused33

@pcbeingused33 pcbeingused33 commented Nov 13, 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 (specs run on Ruby 3.3 only)

Related issues

No issue in this repository. The same failure mode is addressed for the Go SDK in launchdarkly/go-sdk-events#69.

Describe the solution you've provided

LDClient#track with a NaN or ±Infinity metric value makes the whole event batch fail to serialize. CustomEvent#metric_value goes straight into events_out.to_json, which raises JSON::GeneratorError for non-finite floats. The error is rescued and logged inside the flush worker, but trigger_flush has already handed the payload over and cleared the outbox, so every other event in that flush (and the summary) is lost.

EventProcessor#record_custom_event now logs a warning and drops only that custom event when metric_value is a non-finite Float. Other events are untouched; data is not inspected.

Three specs in spec/events_spec.rb (NaN, +Infinity, -Infinity) record a healthy custom event plus a bad one and assert that only the healthy one is delivered. They fail without the change and pass with it.

Reproduction:

bundle exec rspec spec/events_spec.rb -e "non-finite metric value"

Describe alternatives you've considered

  • Sending the event without metricValue: keeps the event but silently reports a different measurement.
  • Rescuing the serialization error per event in the formatter: a larger change in the hot path, for a value that is never valid.

Additional context

This change was prepared with the assistance of an AI coding agent (Claude) and has not been reviewed line by line by a human prior to submission.


Note

Overview
Custom events with a NaN or ±Infinity metricValue no longer break an entire flush batch. EventProcessor#record_custom_event now rejects non-finite Float metrics with a warning and skips enqueueing that event only.

Specs cover NaN, +Infinity, and -Infinity: a valid custom event in the same flush still ships.

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

…ing the batch

NaN and +/-Infinity metric values made JSON serialization raise, losing every event in the same flush.

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@pcbeingused33
pcbeingused33 requested a review from a team as a code owner November 13, 2026 08:11
@keelerm84 keelerm84 changed the title fix: drop custom events with non-finite metric values instead of failing the batch fix: Prevent a NaN or infinite metric value from discarding all events in a flush Nov 20, 2026
@keelerm84
keelerm84 merged commit 2b0f4ef into launchdarkly:main Nov 20, 2026
12 checks passed
@keelerm84

Copy link
Copy Markdown
Member

@pcbeingused333 thank you for contributing this fix!

keelerm84 pushed a commit that referenced this pull request Nov 20, 2026
🤖 I have created a release *beep* *boop*
---


##
[8.20.1](8.20.0...8.20.1)
(2026-11-20)


### Bug Fixes

* Prevent a NaN or infinite metric value from discarding all events in a
flush
([#498](#498))
([2b0f4ef](2b0f4ef))
* Restart the stream when a delete event has a version that is not an
integer
([#501](#501))
([d060827](d060827))
* Signal readiness when selectorless initializers are exhausted
([#499](#499))
([22fda02](22fda02))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
tanderson-ld pushed a commit to launchdarkly/dotnet-core that referenced this pull request Nov 30, 2026
**Requirements**

- [x] I have added test coverage for new or changed functionality
- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [ ] I have validated my changes against all supported platform
versions (tests run on net8.0 only)

**Related issues**

No issue in this repository. Same failure mode as
launchdarkly/go-server-sdk#520 and launchdarkly/ruby-server-sdk#498.

**Describe the solution you've provided**

`Track` with a `double.NaN` or infinite `metricValue` queues a custom
event that cannot be serialized: `EventOutputFormatter` passes the value
to `Utf8JsonWriter.WriteNumber`, which throws for non-finite numbers.
The flush worker catches the exception, logs "Error preparing events,
will not send" and returns, so every other event and summary in that
batch is lost.

The server-side and client-side `Track` methods now check the value, log
a warning that names the event, and do not record it. Finite values and
calls without a metric value are unchanged.

Tests cover NaN, +Infinity and -Infinity in both SDKs (no custom event
recorded, warning logged). They fail without the change and pass with
it.

**Describe alternatives you've considered**

- Checking in the shared event formatter: a change in the internal
package, and the SDKs would only pick it up after a new
`LaunchDarkly.InternalSdk` release.
- Sending the event without `metricValue`: keeps the event but silently
reports a different measurement.

**Additional context**

This change was prepared with AI assistance (Claude Code).

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> **Client and server `Track`** now reject **NaN and ±Infinity** metric
values: they **log a warning** (including the event name) and **do not
enqueue** the custom event. Finite metrics and tracks without a metric
are unchanged.
> 
> This avoids a serialization failure when events are flushed
(`Utf8JsonWriter.WriteNumber` throws on non-finite doubles), which
previously could abort an entire event batch.
> 
> **Tests** add theory coverage for NaN and both infinities in the
client and server SDKs (no custom event, warning logged).
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
459a8ef. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
tanderson-ld added a commit to launchdarkly/java-core that referenced this pull request Nov 30, 2026
**Requirements**

- [x] I have added test coverage for new or changed functionality
- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [ ] I have validated my changes against all supported platform
versions (`LDClientEventTest` run on JDK 17 only)

**Related issues**

No issue in this repository. Same failure mode as
launchdarkly/go-server-sdk#520 and launchdarkly/ruby-server-sdk#498.

**Describe the solution you've provided**

`LDClient#trackMetric` passes `metricValue` to the event processor
without checking it. For `NaN` or `±Infinity`, Gson throws in
`EventOutputFormatter` when the value is written. The exception is
caught around the whole flush in `DefaultEventProcessor`, so the other
events and the summaries in that batch are not delivered either.

`trackMetric` now checks the value after the existing context checks.
For a non-finite value it logs a warning with the event name and does
not record the event. Finite values are unchanged.

The added test `trackMetricWithNonFiniteValueDoesNotSendEvent` covers
all three values. It fails without the change and passes with it.

**Describe alternatives you've considered**

Checking in `EventOutputFormatter` (shared internal library). That would
also protect the payload, but the Go SDK fix put the check in the SDK,
where it can warn at the call site.

**Additional context**

This change was prepared with AI assistance (Claude Code).

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> **`LDClient#trackMetric`** now treats **NaN** and **±Infinity** like
invalid input: after the existing context checks it logs a warning
(including the event name) and **does not** call the event processor, so
bad metrics never reach JSON serialization.
> 
> This avoids Gson failing during event flush and taking down the **rest
of the batch** (other custom events and summaries). Valid finite metrics
behave as before.
> 
> **`LDClientEventTest`** adds
**`trackMetricWithNonFiniteValueDoesNotSendEvent`**, asserting NaN and
both infinities produce no events.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
fcc9bb0. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Todd Anderson <127344469+tanderson-ld@users.noreply.github.com>
tanderson-ld pushed a commit to launchdarkly/python-server-sdk that referenced this pull request Nov 30, 2026
**Requirements**

- [x] I have added test coverage for new or changed functionality
- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [ ] I have validated my changes against all supported platform
versions (tested on one Python version only)
- [x] 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).

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> **`LDClient.track` and `AsyncLDClient.track`** now reject custom
events when `metric_value` is a non-finite `float` (`NaN`, `±Infinity`):
they log a warning naming the event and skip sending, instead of letting
those values reach batch JSON serialization (which can emit invalid JSON
and jeopardize other events in the same flush). Docstrings describe this
behavior.
> 
> Parametric tests were added on both sync and async clients to assert
no event is recorded and the warning appears for each non-finite metric
value.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
6298882. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
tanderson-ld pushed a commit to launchdarkly/flutter-client-sdk that referenced this pull request Nov 30, 2026
**Requirements**

- [x] I have added test coverage for new or changed functionality
- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [ ] I have validated my changes against all supported platform
versions (tests run on the Dart VM with the current stable SDK only)

**Related issues**

No issue in this repository. Same failure mode as
launchdarkly/go-server-sdk#520 and launchdarkly/ruby-server-sdk#498.

**Describe the solution you've provided**

`track` accepts any `num` as `metricValue`. A `NaN` or `±Infinity` value
is copied into the event JSON, and `jsonEncode` in
`DefaultEventProcessor` throws for it. The events have already been
taken off the buffer at that point, and the retry encodes the same
payload, so every event in that flush (including summaries) is dropped.

`LDCommonClient.track` now logs a warning naming the event and returns
without recording it when `metricValue` is not finite. As in the Go
server SDK, `afterTrack` hooks are not run for such a call. Finite and
absent values are unchanged, and `data` is not inspected.

The new tests cover `NaN`, `Infinity` and `-Infinity`, plus a finite
value. The three non-finite cases fail without the change and pass with
it. `dart test` and `dart analyze --fatal-infos` pass in
`packages/common_client`.

Reproduction: record a custom event with `metricValue: 1` and another
with `double.nan` in a `DefaultEventProcessor`, then call `flush()`. No
request is made and the log says `Converting object to an encodable
object failed: NaN ... some events were dropped`.

**Describe alternatives you've considered**

Sending the event without `metricValue` keeps the event but reports
something other than what was measured.

**Additional context**

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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> **`track()` no longer queues custom events when `metricValue` is NaN
or ±Infinity.** Non-finite numbers cannot be JSON-encoded, so a single
bad metric could cause `DefaultEventProcessor` flush encoding to fail
and drop an entire batch of analytics events.
> 
> On a non-finite `metricValue`, the client logs a warning (including
the event name) and returns early without calling the event processor or
**`afterTrack` hooks**. Finite metrics and omitted `metricValue` behave
as before.
> 
> Tests verify finite metrics are recorded and that `NaN`, `Infinity`,
and `-Infinity` produce no custom events.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
d92f73a. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
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