Skip to content

fix: prevent IEEE 754 precision loss in BigInt nanosecond conversions#3555

Closed
brone1323 wants to merge 1 commit into
triggerdotdev:mainfrom
brone1323:fix/bigint-nanosecond-precision
Closed

fix: prevent IEEE 754 precision loss in BigInt nanosecond conversions#3555
brone1323 wants to merge 1 commit into
triggerdotdev:mainfrom
brone1323:fix/bigint-nanosecond-precision

Conversation

@brone1323
Copy link
Copy Markdown

Problem

Several places multiply epoch milliseconds by 1_000_000 before converting to BigInt, causing IEEE 754 float precision loss (~±256 ns errors):

// Bug: multiplication happens in float-land; result ~1.7e18 > Number.MAX_SAFE_INTEGER
BigInt(new Date().getTime() * 1_000_000)

The product (~1.7×10¹⁸) exceeds Number.MAX_SAFE_INTEGER (~9×10¹⁵), so the float loses the last few bits before BigInt sees it.

Fix

Convert to BigInt first, then multiply — matching the already-correct pattern in convertDateToNanoseconds() in the same file:

// Correct: BigInt arithmetic, no precision loss
BigInt(new Date().getTime()) * BigInt(1_000_000)

Affected locations (4 sites across 3 files)

File Function
apps/webapp/app/v3/eventRepository/common.server.ts getNowInNanoseconds()
apps/webapp/app/v3/eventRepository/common.server.ts calculateDurationFromStart()
apps/webapp/app/v3/eventRepository/index.server.ts recordRunEvent()
apps/webapp/app/v3/runEngineHandlers.server.ts retry event recording

Fixes #3292

Multiplying epoch milliseconds by 1_000_000 before converting to BigInt
causes IEEE 754 float precision loss (~256ns errors) because the result
(~1.7e18) exceeds Number.MAX_SAFE_INTEGER (~9e15).

Fix all four affected sites to convert to BigInt first, then multiply:
  BigInt(ms) * BigInt(1_000_000)

This matches the existing correct pattern in convertDateToNanoseconds().

Fixes triggerdotdev#3292
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 11, 2026

⚠️ No Changeset found

Latest commit: 4add189

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

Hi @brone1323, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 11, 2026

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f6c59ad3-0ccf-4289-bd5e-a6ecf7b40573

📥 Commits

Reviewing files that changed from the base of the PR and between 2b84545 and 4add189.

📒 Files selected for processing (3)
  • apps/webapp/app/v3/eventRepository/common.server.ts
  • apps/webapp/app/v3/eventRepository/index.server.ts
  • apps/webapp/app/v3/runEngineHandlers.server.ts

Walkthrough

This change updates millisecond-to-nanosecond timestamp conversions across the event repository and run engine to use safer BigInt arithmetic. Instead of multiplying a JS number by 1,000,000 and then converting to BigInt, the code now converts to BigInt first and then multiplies. This pattern is applied to utility functions (getNowInNanoseconds() and calculateDurationFromStart()) and to event recording callsites (recordRunEvent() and runRetryScheduled()), ensuring consistent and precision-safe nanosecond conversions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot closed this May 11, 2026
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

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.

OTLP nanosecond timestamp overflow in webapp event repository

1 participant