Skip to content

fix: Enforce a 24-hour maximum session duration#470

Draft
marandaneto wants to merge 9 commits intomainfrom
fix/use-date-provider-in-session-manager
Draft

fix: Enforce a 24-hour maximum session duration#470
marandaneto wants to merge 9 commits intomainfrom
fix/use-date-provider-in-session-manager

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented Mar 26, 2026

💡 Motivation and Context

Enforce a 24-hour maximum session duration. When a session exceeds 24 hours, it is automatically rotated to a new one. This prevents excessively long sessions from accumulating unbounded data.

Closes #220

💚 How did you test it?

  • Added unit tests for rotateSession(), getSessionStartedAt(), and sessionStartedAt lifecycle in PostHogSessionManagerTest.
  • Added integration tests in PostHogLifecycleObserverIntegrationTest verifying:
    • Session is rotated when it exceeds 24 hours.
    • Session is not rotated when under 24 hours.
    • Session rotation is skipped for React Native (RN manages its own sessions).
  • Used PostHogDateProvider for time mocking to make tests deterministic.
  • Ran make checkFormat — all formatting checks pass.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file
  • Added the "release" label to the PR to indicate we're publishing new versions for the affected packages

@marandaneto marandaneto requested a review from a team as a code owner March 26, 2026 16:42
@marandaneto marandaneto marked this pull request as draft March 26, 2026 16:43
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 26, 2026

posthog-android Compliance Report

Date: 2026-03-27 15:36:37 UTC
Duration: 173ms

✅ All Tests Passed!

0/0 tests passed


@marandaneto marandaneto changed the title fix: use PostHogDateProvider in PostHogSessionManager for testability fix: Enforce a 24-hour maximum session duration Mar 26, 2026
Comment on lines +79 to +83
if (postHog?.isSessionReplayActive() == true) {
postHog?.stopSessionReplay()

// startSessionReplay will rotate the session id internally
postHog?.startSessionReplay(resumeCurrent = false)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i need to do this here so all the snapshots and caching are cleared up from the previous sessions and wont leak to the new one

if (PostHogSessionManager.isSessionExceedingMaxDuration(currentTimeMillis)) {
cancelTask()
postHog?.endSession()
postHog?.stopSessionReplay()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i have to check if i need to call postHog?.startSessionReplay during onStart now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enforce max session duration

1 participant