Repository navigation
release: v1.22.20 - #888
Merged
Merged
Conversation
Sync main into develop after v1.22.19
* Fix duplicate session race condition (TT-031, Refs #886) Server-side race condition: when init() and identify() are called in quick succession (common with React Native SDK 1.3.1), both POST /ingest/session requests could arrive concurrently. The endpoint checked for an existing session, then created one if not found - a classic check-then-insert race that created duplicate rows. Root cause: - No unique constraint on (project_id, session_id) - Check-then-create pattern in POST /ingest/session endpoint Fix: 1. Added unique constraint on Session(project_id, session_id) 2. Changed endpoint to use upsert instead of check-then-create 3. Safe migration that dedupes existing duplicate rows first: - Merges ended_at from duplicates into earliest session - Deletes all but the earliest (smallest id) session per key 4. Added regression tests for concurrent session creation The SDK behavior is correct - identify() should be able to update user info immediately after init(). Server now handles this atomically. * Update CHANGELOG for TT-031 fix * Address PR review: fix app dimension, deterministic selection, field merging, and tests Fixes per review of PR #887 (TT-031, Refs #886): 1. **App dimension in unique key**: Changed from (project_id, session_id) to (project_id, session_id, app) to match findIngestSession's filtering logic. Updated upsert where clause to include app. 2. **Deterministic row selection**: Use ROW_NUMBER() OVER (PARTITION BY ... ORDER BY started_at ASC, id ASC) instead of MIN(id) to ensure earliest session by started_at, then id is kept. 3. **Complete field merging**: Before DELETE, UPDATE merges ALL nullable fields (user_id, user_email, anonymous_id, country, device_browser, device_os, sdk_version, platform, environment, release) using latest non-null values; ended_at uses GREATEST (MAX) of all non-null values. 4. **Transaction**: Wrapped entire migration in BEGIN/COMMIT. 5. **Migration regression test**: Added test that seeds duplicates where identify() row has larger UUID than init() row, verifies kept row has earliest started_at/id with all fields merged, and other apps untouched. Includes second test for multiple duplicates with same started_at. 6. **Bug fix**: Added ended_at to createData in upsert so concurrent requests that win the create race preserve ended_at from the request body. All tests pass (migration test + existing ingest tests). * Fix ended_at handling in createData to avoid undefined in Prisma create Only include ended_at and started_at fields in createData when they are provided in the request body, rather than passing undefined which might cause issues in some environments. * Replace Prisma upsert with raw INSERT ... ON CONFLICT for atomic session creation Prisma's upsert uses SELECT-then-INSERT (not ON CONFLICT) when conditions aren't met, causing P2002 unique violations on concurrent requests in CI. Fix: Native INSERT ... ON CONFLICT (project_id, session_id, app) DO UPDATE with merge semantics matching the migration: - COALESCE to preserve existing non-null fields - GREATEST for ended_at to keep the latest value This ensures truly atomic upsert behavior that handles concurrent init() + identify() calls without race conditions. * Fix merge semantics: prioritize latest non-null values and update tests - Migration: change ORDER BY to DESC to pick latest non-null values (not oldest) - Migration: flip COALESCE to COALESCE(latest, existing) so latest wins - Ingest ON CONFLICT: use COALESCE(EXCLUDED.x, Session.x) so new non-null wins - Tests: execute actual migration.sql statements (not hand-copied logic) - Tests: verify latest non-null values win, seed later rows with different data - Tests: add upsert test to verify ON CONFLICT preserves existing non-null fields Addresses review feedback on PR #887 (TT-031)
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.
Release v1.22.20
Fixed
/ingest/sessionuses an atomicINSERT ... ON CONFLICTon a new unique key(project_id, session_id, app); migration20261028090000_dedupe_and_constrain_sessionsmerges existing duplicates (latest non-null fields, latestended_at) and removes extra rows (#887, Refs #886)Checklist