Skip to content

release: v1.22.20 - #888

Merged
unjica merged 3 commits into
mainfrom
release/v1.22.20
Oct 28, 2026
Merged

unjica merged 3 commits into
mainfrom
release/v1.22.20

Conversation

@unjica

@unjica unjica commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Release v1.22.20

Fixed

  • Duplicate session race condition (TT-031): POST /ingest/session uses an atomic INSERT ... ON CONFLICT on a new unique key (project_id, session_id, app); migration 20261028090000_dedupe_and_constrain_sessions merges existing duplicates (latest non-null fields, latest ended_at) and removes extra rows (#887, Refs #886)

Checklist

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)
@unjica
unjica merged commit d0f5ad5 into main Oct 28, 2026
6 checks passed
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