Skip to content

Commit a125348

Browse files
committed
fix: convert to BigInt before nanosecond multiplication to avoid precision 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 #3292
1 parent cf0fdde commit a125348

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

apps/webapp/app/v3/eventRepository/common.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function extractContextFromCarrier(carrier: Record<string, unknown>) {
2121
}
2222

2323
export function getNowInNanoseconds(): bigint {
24-
return BigInt(new Date().getTime() * 1_000_000);
24+
return BigInt(new Date().getTime()) * BigInt(1_000_000);
2525
}
2626

2727
export function getDateFromNanoseconds(nanoseconds: bigint): Date {
@@ -35,7 +35,7 @@ export function calculateDurationFromStart(
3535
) {
3636
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;
3737

38-
const duration = Number(BigInt($endtime.getTime() * 1_000_000) - startTime);
38+
const duration = Number(BigInt($endtime.getTime()) * BigInt(1_000_000) - startTime);
3939

4040
if (minimumDuration && duration < minimumDuration) {
4141
return minimumDuration;

apps/webapp/app/v3/eventRepository/index.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ async function recordRunEvent(
215215
runId: foundRun.friendlyId,
216216
...attributes,
217217
},
218-
startTime: BigInt((startTime?.getTime() ?? Date.now()) * 1_000_000),
218+
startTime: BigInt(startTime?.getTime() ?? Date.now()) * BigInt(1_000_000),
219219
...optionsRest,
220220
});
221221

apps/webapp/app/v3/runEngineHandlers.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export function registerRunEngineEventBusHandlers() {
428428
const eventRepository = resolveEventRepositoryForStore(run.taskEventStore);
429429

430430
await eventRepository.recordEvent(retryMessage, {
431-
startTime: BigInt(time.getTime() * 1000000),
431+
startTime: BigInt(time.getTime()) * BigInt(1_000_000),
432432
taskSlug: run.taskIdentifier,
433433
environment,
434434
attributes: {

0 commit comments

Comments
 (0)