Skip to content

Fix Postgres acquire_lock TOCTOU race condition #19

@patrick-chinchill

Description

@patrick-chinchill

Problem

Two-step INSERT + UPDATE allows concurrent callers to both win an expired lock. TS uses a single atomic upsert with ON CONFLICT DO UPDATE WHERE.

Fix

Replace with: INSERT ... ON CONFLICT (key_prefix, thread_id) DO UPDATE SET ... WHERE chat_state_locks.expires_at <= now() RETURNING ...

File: src/chat_sdk/state/postgres.py lines 220-252

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions