Skip to content

fix: serialize UserDO requests, release QuickJS resources, cancel timed-out writes - #42

Closed
takomitkm wants to merge 3 commits into
SaintWe:multi-tenantfrom
takomitkm:fix/concurrency-and-resource-leaks
Closed

takomitkm wants to merge 3 commits into
SaintWe:multi-tenantfrom
takomitkm:fix/concurrency-and-resource-leaks

Conversation

@takomitkm

Copy link
Copy Markdown

Summary

Three data-consistency and resource-lifetime fixes, with regression tests for each.

1. Durable Object requests were not serialized

forwardToSubStore.js / runUserCron.js read the whole user_data JSON, mutate it, then overwrite it wholesale. A DO is single-threaded, but async await lets requests interleave — two concurrent requests could clobber each other's updates.

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

2. QuickJS runtime/context leaked on failure paths

runScriptOnce only disposed the VM and WASM runtime on the success path. Compile errors, synchronous throws, rejected promises, pending timeouts and pendingJobs failures all threw without cleanup.

All handles are now tracked and disposed in a finally block alongside vm.dispose() and runtime.dispose(). Once aborted, pending host-call callbacks no longer write into a VM that is about to be disposed.

3. Request timeout did not cancel side effects

The timeout handler only cleared done and returned 504; initSubStore kept running and could still write user data.

The request context now has an aborted flag and cancel(). On timeout (HTTP and cron), cancel() marks the context and detaches done; $done ignores cancelled contexts and flushSubStoreRequestContext refuses to persist late writes. The HTTP timeout is injectable for tests (default unchanged at 25000 ms).

Note: JavaScript cannot forcibly kill a script already handed to the global dispatch. This fix gates all side-effect exits, so work that continues after timeout cannot produce any persisted effect.

Tests

11 tests added, all passing. Each fix was verified by reverting it and confirming the corresponding tests fail (leak probe: 23/25 runtimes leaked before fix, 0 after).

QJS_DEPS_DIR=<dir with quickjs-emscripten@0.31> \
  node --import ./tests/hooks/register.mjs --test tests/*.test.js
# 11 tests, 11 pass, 0 fail

Regression coverage:

  • tests/user-do-serialization.test.js: concurrent PUTs stay serialized; a failed request does not block the queue
  • tests/quickjs-resource-cleanup.test.js: compile error, sync throw, rejected promise, success, and failure-then-success; after hook asserts every runtime has alive === false
  • tests/substore-timeout-cancel.test.js: cancel semantics, flush gate, full 504 path with a late script write that must not be persisted

tests/hooks/ contains Node ESM loader stubs for cloudflare:workers, @cloudflare/actors/storage and mmdb-lib so the Workers code can be tested in plain Node.

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.
runScriptOnce only disposed the VM and WASM runtime on the success path.
Compile errors, synchronous throws, rejected promises, pending timeouts
and pendingJobs failures all threw without cleanup, leaking WASM runtimes
on repeated failures.

Track every handle and dispose all of them plus vm/runtime in a finally
block that runs on success, error and timeout alike. Once aborted, pending
host-call callbacks no longer write into a VM that is about to be disposed.

Add regression tests with a leak probe: after 25 script runs, assert every
created runtime has alive === false (23/25 leaked before the fix, 0 after).
The timeout handler only cleared done and returned 504; initSubStore kept
running and could still write user data after the caller had given up.

Add an aborted flag and cancel() to the request context. On timeout (HTTP
and cron), cancel() marks the context and detaches done;  ignores
cancelled contexts and flushSubStoreRequestContext refuses to persist late
writes. The HTTP timeout is now injectable for tests (default unchanged).

Add regression tests covering cancel semantics, the flush gate, and a full
504 path where a late script write must not be persisted.
@takomitkm
takomitkm marked this pull request as draft November 23, 2026 08:52
@takomitkm

Copy link
Copy Markdown
Author

Superseded by #43 (UserDO serialization), #44 (QuickJS cleanup) and #45 (timeout cancellation). Each fix is now an independent PR with expanded regression tests; the CI workflow also runs the Node test suite.

@takomitkm takomitkm closed this Nov 25, 2026
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