Repository navigation
Conversation
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
marked this pull request as draft
November 23, 2026 08:52
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three data-consistency and resource-lifetime fixes, with regression tests for each.
1. Durable Object requests were not serialized
forwardToSubStore.js/runUserCron.jsread the wholeuser_dataJSON, mutate it, then overwrite it wholesale. A DO is single-threaded, but asyncawaitlets requests interleave — two concurrent requests could clobber each other's updates.UserDO.fetchnow 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
runScriptOnceonly 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
finallyblock alongsidevm.dispose()andruntime.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
doneand returned 504;initSubStorekept running and could still write user data.The request context now has an
abortedflag andcancel(). On timeout (HTTP and cron),cancel()marks the context and detachesdone;$doneignores cancelled contexts andflushSubStoreRequestContextrefuses 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).
Regression coverage:
tests/user-do-serialization.test.js: concurrent PUTs stay serialized; a failed request does not block the queuetests/quickjs-resource-cleanup.test.js: compile error, sync throw, rejected promise, success, and failure-then-success;afterhook asserts every runtime hasalive === falsetests/substore-timeout-cancel.test.js: cancel semantics, flush gate, full 504 path with a late script write that must not be persistedtests/hooks/contains Node ESM loader stubs forcloudflare:workers,@cloudflare/actors/storageandmmdb-libso the Workers code can be tested in plain Node.