Skip to content

fix: null-safe getTime() calls in replication services (#3519)#3974

Closed
deepshekhardas wants to merge 1 commit into
triggerdotdev:mainfrom
deepshekhardas:fix/3519-replication-nullsafety
Closed

fix: null-safe getTime() calls in replication services (#3519)#3974
deepshekhardas wants to merge 1 commit into
triggerdotdev:mainfrom
deepshekhardas:fix/3519-replication-nullsafety

Conversation

@deepshekhardas

Copy link
Copy Markdown

Add optional chaining to getTime() calls on createdAt, updatedAt
fields in runs and sessions replication services. When CDC sends rows
before timestamps are fully populated, calling .getTime() on undefined
crashes the replication service with a TypeError.

  • runsReplicationService.server.ts: null-safe updatedAt/createdAt in
    #prepareTaskRunInsert and #preparePayloadInsert
  • sessionsReplicationService.server.ts: null-safe createdAt/updatedAt in
    toSessionInsertArray

…#3519)

Add optional chaining to getTime() calls on createdAt, updatedAt
fields in runs and sessions replication services. When CDC sends rows
before timestamps are fully populated, calling .getTime() on undefined
crashes the replication service with a TypeError.

- runsReplicationService.server.ts: null-safe updatedAt/createdAt in
  #prepareTaskRunInsert and #preparePayloadInsert
- sessionsReplicationService.server.ts: null-safe createdAt/updatedAt in
  toSessionInsertArray
@changeset-bot

changeset-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 6858123

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 @deepshekhardas, 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 Jun 17, 2026

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

Open in Devin Review

Comment on lines +1083 to +1084
run.updatedAt?.getTime() ?? Date.now(), // updated_at
run.createdAt?.getTime() ?? Date.now(), // created_at

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚩 Date.now() fallback for created_at could produce non-deduplicatable rows in ClickHouse

The ClickHouse task_runs_v2 table uses created_at in both the sort key (ORDER BY (organization_id, project_id, environment_id, created_at, run_id)) and the partition key (PARTITION BY toYYYYMM(created_at)). ReplacingMergeTree deduplicates rows based on the sort key. If createdAt is null for a replication event and Date.now() is used as a fallback, the fabricated created_at value would differ from the real timestamp on any subsequent replication event (e.g., an update) for the same run. This means the two rows would have different sort keys and ClickHouse would not deduplicate them, even with FINAL. They could also land in different partitions. This is an inherent trade-off of the Date.now() approach — the alternative (crashing) would halt the entire replication pipeline. A possible improvement would be to skip the row entirely (similar to the !run.environmentType || !run.organizationId guard at runsReplicationService.server.ts:1032) and log a warning, rather than inserting potentially unreplaceable data. The same concern applies to sessionsReplicationService.server.ts if the sessions table has a similar schema.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

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: 9db63eb7-0b4c-4dd5-8a89-efb23d124be0

📥 Commits

Reviewing files that changed from the base of the PR and between 4e919e7 and 6858123.

📒 Files selected for processing (2)
  • apps/webapp/app/services/runsReplicationService.server.ts
  • apps/webapp/app/services/sessionsReplicationService.server.ts

Walkthrough

Two replication service files are updated to handle missing timestamp fields defensively. In runsReplicationService.server.ts, the #prepareTaskRunInsert method changes updated_at and created_at to use optional chaining with Date.now() as a fallback, and #preparePayloadInsert applies the same pattern to its created_at field. In sessionsReplicationService.server.ts, toSessionInsertArray replaces direct .getTime() calls on session.createdAt and session.updatedAt with optional chaining and Date.now() fallbacks. No public API signatures are changed.

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

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


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.

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