Skip to content

TT-031: Concurrent POST /ingest/session creates duplicate session rows (init() + immediate identify() inflates session counts) #886

Closed

Description

@unjica

TT ID: TT-031 · Severity: P2 · Area: Ingest/API · Found by: QA (weekly deep test, React Native SDK) · Production: API v1.22.19 · SDK: @telemetry-tracker/react-native 1.3.1 / @telemetry-tracker/core 1.5.2 (npm)

Summary

POST /ingest/session can store two rows for one session_id when two requests for the same session arrive close together. The SDK does exactly that whenever identify() is called right after init() (for example, restoring a signed-in user at app start): init() posts the session start, and identify() immediately re-posts the same session with user_id while the first request is still in flight.

The duplicate row never receives ended_at (the later endSession() updates only one of them), so it stays open. Session KPIs count both rows.

Steps to reproduce

  1. Fresh project, published @telemetry-tracker/[email protected] (the same applies to core/browser, because identify() lives in core).
  2. init({ ingestUrl: "https://api.telemetry-tracker.com", app, apiKey, platform: "android", environment, release }), then immediately identify("some-user"). Wait about 2.5 s, then repeat with a new init() (8 iterations).
  3. GET /api/sessions?range=24h&environment=<env> and group the rows by session_id.

Expected

One row per session_id (the second request updates the first, as it already does when requests arrive one after another).

Actual

  • SDK loop (init + identify): 6/8 and 5/8 session_ids have 2 rows (two separate runs). The first deep-test harness hit it in 1 of 3 runs.
  • Raw API, no SDK: two concurrent POST /ingest/session with the same body and session_id gave 2 rows in 1/5 tries. The same two requests sent sequentially gave 1 row in 5/5.
  • Effect: Sessions page and /api/sessions/summary for that environment report 38 total sessions for 26 real sessions (+46%), and the bounce rate is skewed. Each duplicate row is left open with ended_at = null.

Likely cause

apps/api/src/routes/ingest.ts (/session) does findIngestSession(...) and then prisma.session.create(...) when nothing is found: check-then-insert with no transaction. model Session in schema.prisma has no unique constraint on (project_id, app, session_id), so concurrent requests both insert. A unique index plus upsert (or catch-and-update on conflict) would close the race. The SDK could also serialize session posts, but the server should be idempotent regardless.

Notes

  • Not security-sensitive.
  • Evidence (QA only): QA project environment qa-deep-rn-sessrace, runs a/b/raw1 on 2026-10-28 09:57–10:00 CET.

Activity

  1. added
    bug
    api
    qa
    on Oct 28, 2026
  2. unjica commented on Oct 28, 2026

    @unjica
    CollaboratorAuthor

    QA retest on v1.22.20 (PR #887), 2026-10-28 ~11:42–11:47 CET: partly fixed, keeping open.

    Setup: production /health 1.22.20; published @telemetry-tracker/react-native 1.3.1 (+ core 1.5.2); QA project only.

    Check Result
    Duplicate rows, init()+identify(), 3 runs × 8 sessions PASS: 0/24 duplicates, 0 missing, 1 row per session
    Burst: 6 concurrent POST /ingest/session per session_id × 6 PASS: 36/36 204, no 500, no Prisma text, 1 row each
    Extra variants (pairs ×5, 10-post burst, start/end/re-post, partial re-post, later user wins) PASS. Missing app gives 400 (expected)
    Morning duplicates (env qa-deep-rn-sessrace) PASS: totalSessions 26 (was 38)
    ended_at PASS: set on 21/24. The last session of each run has no end because the process exits (expected)
    user_id from identify() kept FAIL, 3/3 runs: the last session of runs 1 and 2 has user …-6 instead of …-7. In run 3, session 5 has user …-3 instead of …-4

    Cause, from logging fetch in run 3: RN 1.3.1 sends a late extra POST for the new session carrying the previous user_id. It arrives about 1 s after the correct identify post, e.g. c11fe082 user=post3-7 at 10:46:08.272Z, then c11fe082 user=post3-6 at 10:46:08.960Z. The new ON CONFLICT upsert is last-write-wins on user_id, so when that stale post lands last it overwrites the identified user. Before the fix, this stale post probably created the second duplicate row. Possible fixes: the SDK should stop sending the stale-user post after init()/identify(), and/or the server should not let a later post replace an existing non-null user_id with an older one (e.g. compare by client timestamp).

    Evidence: post-sdk-p1/p2/p3*.json, post-sdk-p3-dbg.log, post-burst*.json, post-variants*.json.

  3. unjica commented on Oct 28, 2026

    @unjica
    CollaboratorAuthor

    QA retest on production v1.22.21 (live 12:31 CET, 2026-10-28): the server-side identity_updated_at handling works. 12 raw /ingest/session cases × 2 runs passed 24/24 (stale older timestamp ignored, newer wins, equal keeps first, future and pre-start timestamps clamped, null user keeps stored user, email follows the same rule, 6-way concurrent burst keeps the newest, invalid timestamp 400), one row per session, no 500s. Published RN 1.3.1 still shows the stale-user overwrite on the last session (2/2 runs), which is expected because it sends no identity_updated_at. Keeping this open until core 1.5.3 / RN 1.3.2 are on npm and the init()+identify() repro passes with them.

  4. unjica commented on Oct 30, 2026

    @unjica
    CollaboratorAuthor

    QA (2026-10-30, CET): client part of TT-031 is verified in the published @telemetry-tracker/[email protected] (npm, gitHead e5ece31). I ran the upstream session-lifecycle and index tests against the installed dist twice: 9 files and 78 tests passed each time. That covers identity_updated_at on session payloads, identify() before init() being kept, and stale identity being cleared on re-init. @telemetry-tracker/[email protected] resolves core 1.5.3 in a fresh install. Leaving the issue open until @telemetry-tracker/react-native 1.3.2 is on npm and the end-to-end retest passes. Refs #886

  5. unjica commented on Oct 30, 2026

    @unjica
    CollaboratorAuthor

    @telemetry-tracker/react-native 1.3.2 is published on npm (latest, depends on @telemetry-tracker/core ^1.5.3, built from e5ece31, tag sdk-react-native-v1.3.2). Core 1.5.3 was already published. Awaiting QA retest; QA closes this after verification.

  6. unjica commented on Oct 30, 2026

    @unjica
    CollaboratorAuthor

    QA verification on published packages, 2026-10-30 (CET): PASS 2/2

    Packages (registry.npmjs.org, clean temp dir, empty cache):

    Server: production v1.22.25. /health returned 200 both before and after the runs.

    Method: Node harness using the published RN package, with RN globals mocked as in the earlier repro. The harness logs every /ingest/session post. QA project only, existing app my-rn-app, env qa-tt031-rn132. Payloads were at most 401 bytes. Read-back went through the dashboard sessions API. Each run covers 11 sessions:

    • A: identify() before init(), so the identity should be kept
    • B: the original loop of init() + immediate identify() with a different user, 8 times
    • C: re-init without identify(), so the stale identity should be cleared
    • D: re-init and then a different user

    Results:

    • 22/22 sessions store the expected user_id. Every session has exactly 1 row and sdk_version 1.5.3.
    • In case C the user_id is null, so the stale identity was cleared.
    • Every post that carries a user includes identity_updated_at.
    • No post carried the previous session's user, which was the stale late post seen with 1.3.1.
    • An initial post with no user sometimes arrived after the identified post (for example B-3 and B-4). The server still kept the identified user.
    • 60 posts in total, all 204, no 5xx.

    Older SDKs (RN ≤1.3.1 / core ≤1.5.2) remain affected as documented; upgrading to RN 1.3.2 / core 1.5.3 resolves it. Closing as completed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    apibugqa

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions