fix(streaming): race condition on concurrent stream starts #28

Open
opened 2026-08-26 20:50:00 +01:00 by dylan · 0 comments
Owner

startStream() uses a find-then-insert check for an existing active
stream, which isn't atomic — two concurrent start requests for the
same user can both pass the check before either commits, resulting
in two active stream rows.

Proposed fix: add a partial unique index (user_id WHERE end_time IS
NULL) so Postgres enforces "one active stream per user" at the DB
level. startStream() would then rely on the constraint instead of
the pre-check, translating a DataIntegrityViolationException into
the existing AlreadyStreaming exception.

startStream() uses a find-then-insert check for an existing active stream, which isn't atomic — two concurrent start requests for the same user can both pass the check before either commits, resulting in two active stream rows. Proposed fix: add a partial unique index (user_id WHERE end_time IS NULL) so Postgres enforces "one active stream per user" at the DB level. startStream() would then rely on the constraint instead of the pre-check, translating a DataIntegrityViolationException into the existing AlreadyStreaming exception.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dylan/vod-system#28