Skip to content

fix: convert to BigInt before nanosecond multiplication to avoid precision loss#3325

Closed
claygeo wants to merge 1 commit intotriggerdotdev:mainfrom
claygeo:fix/nanosecond-timestamp-overflow
Closed

fix: convert to BigInt before nanosecond multiplication to avoid precision loss#3325
claygeo wants to merge 1 commit intotriggerdotdev:mainfrom
claygeo:fix/nanosecond-timestamp-overflow

Conversation

@claygeo
Copy link
Copy Markdown

@claygeo claygeo commented Apr 3, 2026

Summary

Closes #3292

Multiplying epoch milliseconds by 1,000,000 as a Number exceeds Number.MAX_SAFE_INTEGER (~9e15), causing IEEE 754 precision loss of ~256ns in ~0.2% of cases. The fix converts to BigInt first, then multiplies.

Changes

4 locations fixed across 3 files:

File Function Line
common.server.ts getNowInNanoseconds() 24
common.server.ts calculateDurationFromStart() 38
index.server.ts recordRunDebugLog() 218
runEngineHandlers.server.ts retry event recording 431
// Before: multiplication in float-land (exceeds MAX_SAFE_INTEGER)
BigInt(new Date().getTime() * 1_000_000)

// After: multiplication in BigInt-land (exact)
BigInt(new Date().getTime()) * BigInt(1_000_000)

The correct pattern already existed in convertDateToNanoseconds() in the same file — these locations just weren't using it.

Test plan

  • All 4 locations use BigInt multiplication instead of Number multiplication
  • No behavioral change for correctly-rounded timestamps (~99.8% of cases)
  • Eliminates ~256ns errors for the ~0.2% of timestamps that previously lost precision

…ision loss

Multiplying epoch milliseconds by 1,000,000 as a Number exceeds
Number.MAX_SAFE_INTEGER (~9e15), causing IEEE 754 precision loss
of ~256ns in ~0.2% of cases. Convert to BigInt first, then multiply.

Fixed in 4 locations:
- getNowInNanoseconds() in common.server.ts
- calculateDurationFromStart() in common.server.ts
- recordRunDebugLog() in index.server.ts
- retry event recording in runEngineHandlers.server.ts

The correct pattern (BigInt(ms) * BigInt(1_000_000)) already existed
in convertDateToNanoseconds() in the same file.

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

changeset-bot bot commented Apr 3, 2026

⚠️ No Changeset found

Latest commit: a125348

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

github-actions bot commented Apr 3, 2026

Hi @claygeo, 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.

@github-actions github-actions bot closed this Apr 3, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 3, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9d942296-0e95-40c4-b200-8729f5641d0a

📥 Commits

Reviewing files that changed from the base of the PR and between cf0fdde and a125348.

📒 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

Three files in the event repository and run engine handlers were updated to standardize nanoseconds conversion arithmetic. The changes reorder arithmetic operations to convert millisecond timestamps to BigInt before multiplication rather than multiplying as numbers first. Specifically, getNowInNanoseconds() and calculateDurationFromStart() in the common module, the startTime calculation in the event repository index, and the retry event handling in the run engine handlers all follow this pattern. No function signatures were altered.

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.

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 1 additional finding.

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