fix(logs): guard null session timestamps in initializeLogs#3927
Closed
posthog[bot] wants to merge 2 commits into
Closed
fix(logs): guard null session timestamps in initializeLogs#3927posthog[bot] wants to merge 2 commits into
posthog[bot] wants to merge 2 commits into
Conversation
Session timestamps from checkAndGetSessionAndWindowId can legitimately be null (e.g. right after a session reset), and the unguarded .toString() calls in initializeLogs threw TypeError, aborting console-log capture setup for that session. Use optional chaining and only add each attribute when its value is present. Generated-By: PostHog Code Task-Id: 79af8c32-9f4c-46e5-8cdb-0d26b28d276b
Generated-By: PostHog Code Task-Id: 79af8c32-9f4c-46e5-8cdb-0d26b28d276b
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Contributor
|
Size Change: +84 B (0%) Total Size: 16.8 MB
ℹ️ View Unchanged
|
Contributor
|
This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the |
Contributor
|
Superseded by #3942, which moved session-timestamp attribute assembly into the shared core pipeline with proper isNullish guards. The logs.ts code path this patched no longer exists. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Sites running the posthog-js logs extension can silently lose console-log capture when the extension crashes during initialization.
initializeLogs(packages/browser/src/entrypoints/logs.ts) destructuredsessionStartTimestampandlastActivityTimestampfromsessionManager.checkAndGetSessionAndWindowId(true)and called.toString()on both with no null guard. Those session timestamps can legitimately benull(e.g. right after a session reset), and when they are, the unguarded call throwsTypeError: Cannot read properties of null (reading 'toString'). The throw aborts logs-extension setup, so console-log capture never installs for that session — a silent feature failure. This was surfaced by a production error tracking issue.The unguarded calls were introduced when the extension was first added (#2784) and carried forward verbatim in the recent
@posthog/coremigration (#3895).Changes
initializeLogsusing optional chaining, adding each attribute only when its value is present.initializeLogsdoes not throw and omits the timestamp attributes when the session timestamps arenull.Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Fully autonomous
Authored by PostHog Code (Claude Opus 4.8) from an inbox error-tracking report. Confirmed the throw site at
logs.ts:313(column 57 lands onsessionStartTimestamp.toString()), tracednullsession timestamps to the documented post-reset behavior, applied the optional-chaining guard, and added a unit test. Verifiedsrc/__tests__/entrypoints/logs.test.ts(31 tests) andlogs.golden.test.ts(8 tests) pass locally.Created with PostHog Code from an inbox report.