Skip to content

fix(user-do): serialize requests to prevent read-modify-write races - #43

Open
takomitkm wants to merge 2 commits into
SaintWe:multi-tenantfrom
takomitkm:fix/user-do-serialization
Open

takomitkm wants to merge 2 commits into
SaintWe:multi-tenantfrom
takomitkm:fix/user-do-serialization

Conversation

@takomitkm

Copy link
Copy Markdown

Summary

A Durable Object is single-threaded, but await lets requests interleave. The
user data flow reads the whole JSON blob, mutates it, then overwrites it. Two
concurrent requests could clobber each other's updates.

UserDO.fetch now queues each request through a promise chain, so the whole
operation (including Sub-Store HTTP requests and cron refreshes) runs to
completion before the next one starts. A failed request does not jam the queue.

Tests

  • Two concurrent Sub-Store download requests each read user_data, await,
    then write back. Both updates survive with the queue in place.
  • A control test bypasses the queue via _handleFetch and confirms the test
    catches the lost-update case.
  • A failed request does not block later requests.
  • The storage mock joins all tagged-template parts; the old version only read
    the first part and never matched the real multi-line SQL.

The Workers CI job now runs these Node tests.

Part of the split from the original combined PR.

A Durable Object is single-threaded, but async awaits let different
requests interleave. The user_data flow reads the full JSON, mutates it,
then overwrites it wholesale; two concurrent requests could clobber each
other's updates.

Queue each UserDO.fetch through a promise chain so the entire business
operation (including Sub-Store HTTP requests and cron refreshes) runs to
completion before the next one starts. A failed request does not jam the
queue.

Add regression tests that assert the operation sequence stays strictly
serialized and that a failed request does not block later ones.
Add a real read-modify-write regression: two concurrent Sub-Store download
requests each read the whole user_data blob, await script execution, then
write it back. With the queue both updates survive; a control test via
_handleFetch bypasses the queue and demonstrates the test catches the loss.

Also fix the storage mock to join all tagged-template parts: the real SQL
atom uses multi-line templates, so the old mock matched nothing.

Wire the regression suite into CI (Workers job).
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