POST /v1/users/people get-or-create isn't atomic, so two requests for a new name create two people
Product impact (see PRODUCT.md and ISSUE_TRIAGE_GUIDE.MD)
- Primary loop layer: Understand
- [ ] Involves data loss, failed capture, or broken sync/trust. Nothing is lost, but one contact can end up as two people with the same name.
Describe the bug
POST /v1/users/people (get_or_create_person in backend/routers/users.py) is documented as idempotent by name: "Create a new person or return existing one with same name". It runs get_person_by_name, a plain Firestore name == query. If that misses, it writes a new person under a fresh uuid4 id with create_person (document(id).set(...)). The lookup and the write are separate operations, and nothing ties them together.
Two requests for the same new name that overlap both run the lookup before either write lands. That happens with a double submit, a client retry sent while the first request is still in flight, or two devices. Both miss, both create, and the user gets two people with the same name and different ids. Later name lookups return whichever one comes first, so labels and voice samples can end up split across the two.
To Reproduce
- Pick a name the user doesn't have yet, e.g. "Alice".
- Send two
POST /v1/users/peoplerequests with{"name": "Alice"}at the same time, or retry the first before it returns. - Both return 200 with different
ids, andGET /v1/users/peoplelists two "Alice"s.
Deterministic check: on current main, if another request's "Alice" lands between this request's get_person_by_name miss and its create_person, the endpoint still writes and returns a second person.
Current behavior Overlapping requests for a new name each create a person.
Expected behavior One person per name. A request that loses the race returns the person the other request created, as the endpoint's docstring promises.
Evidence (main 70f9bace79)
- Lookup, then a separate create: https://github.com/BasedHardware/omi/blob/70f9bace79ace710d231dcdfe40a9e53df2518f6/backend/routers/users.py#L615-L635
create_personwrites a new random-id document: https://github.com/BasedHardware/omi/blob/70f9bace79ace710d231dcdfe40a9e53df2518f6/backend/database/users.py#L964-L967- The lookup query runs outside any transaction: https://github.com/BasedHardware/omi/blob/70f9bace79ace710d231dcdfe40a9e53df2518f6/backend/database/users.py#L1005-L1009 and https://github.com/BasedHardware/omi/blob/70f9bace79ace710d231dcdfe40a9e53df2518f6/backend/database/person_aliases.py#L274-L285
- The pattern the backend already uses to dedupe a create, a query inside the creating transaction: https://github.com/BasedHardware/omi/blob/70f9bace79ace710d231dcdfe40a9e53df2518f6/backend/database/action_items.py#L277-L318
Additional context
This is separate from #22381, which is about the lookup being case-sensitive. This one happens with identical names: the lookup and the create aren't atomic. The "Who is this?" tag card (utils/speaker_tag_prompts/service.py) and live explicit introductions (routers/listen/transcripts.py) use the same lookup-then-create pattern. A fix and regression tests follow in a PR.
I have resolved this issue in PR #23324 (https://github.com/BasedHardware/omi/pull/23324).
Summary of Fix:
- Replaced the vulnerable check-then-insert pattern in
POST /v1/users/peoplewith an atomic Firestore@transactionalhelperusers_db.get_or_create_person. - Updated
find_person_by_nameandlist_peopleto support transactional reads viatransaction: Any = None. - Added unit tests in
backend/tests/unit/test_atomic_person_get_or_create.pycovering new creation, idempotency, case-insensitivity, contention retry under concurrent write conflict, and router endpoint handling (6 passed, 100% pass rate).
Payout Wallet: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC)