Repository navigation
fix(core,server): harden workerd durability, hibernation, and machine framing - #67202
Conversation
There was a problem hiding this comment.
I reviewed the code and found no problems that should block a merge. The workerd behaviour can only be checked through unit tests here, so I ran the PR's new tests against the base: the COMMIT-propagation, rollback-throw semaphore, x25519 key-length, dead-socket wake and acceptWebSocket permit-leak tests fail there and pass with this change. The targeted core (171), server (29) and schema (97) suites all pass. tsc -b in packages/server passes; I didn't run the probe scripts.
Public API: one change. The Workspace.ID OpenAPI pattern goes from ^wrk to ^wrk_[A-Za-z0-9_-]+$. Generated IDs and Workspace.isID already followed the strict form, so normal callers are unaffected, but a client sending a custom ID like wrkfoo would now get a validation error. DurableObjectStorage.deleteAlarm is optional, and MachinesFlag / Config.parseInline are internal, so all of that is backwards compatible.
Harden Cloudflare Durable Object durability, WebSocket hibernation, workspace lifecycle locking, and machine framing/crypto boundaries.
Before:
sqlite.workerd.tsswallowed Durable ObjectCOMMITerrors via an already-spent single-shot callback and could deadlock its 1-permit DB semaphore iftxn.rollback()threw.ServerWorkerdDO with hibernated machine WebSockets booted the fullconfigLocationto checkexperimental.machines, arming 1-minuteLocationActivitysweeps and defeating hibernation; unguardedserializeAttachment(null)/close()calls could skipheld.endor crash wake, andacceptWebSocketthrows leakedMAX_HANDSHAKESpermits.Workspace.IDonly checkedstartsWith("wrk")(letting malformed IDs skip idle suspension),Workspace.createraceddestroy,scheduleNextIdlecould drop alarms or never clear stale DO alarms, failed suspends kept stale cached connections and backed off from a stale timestamp, andEnvironments.has/availabilityreported missing/destroyedwrk_*IDs asCold.fsmessages or invalid numeric fields tore down the entire machine link,Frames.readalways copied single-frame messages throughBuffer.concat,WorkerClient.fromWebSocketaccepted< 16Bframes into an unbounded queue,Noise.state()allowed an unrestorableMAX_NONCEsnapshot, andx25519.workerd.tssilently zero-extended/truncated non-32-byte keys.After:
packages/core/src/database/sqlite.workerd.ts: rewritemakeWithTransactionwithEffect.uninterruptibleMask+DeferredsoCOMMITfailures propagate asSqlErrorand throwingtxn.rollback()always settles and releases the DB semaphore.packages/server/src/workerd-machine-link.ts,handlers/machine.ts,routes.ts,workerd.ts,packages/core/src/config.ts,config/variable.ts: guard dead-socket calls, guarantee idempotentMAX_HANDSHAKESpermit release across all error/scope paths, and introduceMachinesFlagwithConfig.parseInline(including{env:...}substitution) so hibernated DO wakes boot 0 Locations and arm 0 timers.packages/schema/src/workspace-id.ts,packages/core/src/workspace.ts,environment/environments.ts: tightenWorkspace.IDto/^wrk_[A-Za-z0-9_-]+$/u(with OpenAPIpattern), serializeWorkspace.createunderlocksandscheduleNextIdleunderidleAlarmSemaphore, wireonIdleClearedtostorage.deleteAlarm, evict connections + use freshClock.currentTimeMillison suspend failure, and checkworkspaces.exists(id)inEnvironments.has/availability.packages/core/src/machine/{driver,frames,wire,worker-client,noise,x25519.workerd}.ts: pre-check outboundfsbyte length and read ranges before queueing, return single-piece frames directly, shareFrames.MIN_FRAME/MAX_FRAMEacrossworker-clientand server handlers, refinewire.tsnumeric schemas, reject spentMAX_NONCEsnapshots, and enforce exact 32-byte keys inx25519.workerd.ts.packages/schema,protocol,sdktestspackages/core&packages/servertargeted suitesprobe:workerd&probe:workerd-machine-linkbun run check,typecheck:tsc,check:generated, PrettierRead:
packages/core/src/database/sqlite.workerd.ts,packages/server/src/workerd-machine-link.ts,packages/core/src/workspace.ts,packages/core/src/machine/driver.ts.Added by the opencode bot (AI-generated).
Public API: tightens the Workspace.ID pattern from ^wrk to ^wrk_[A-Za-z0-9_-]+$ in the OpenAPI spec (rejects custom IDs without wrk_)