Skip to content

fix(core,server): harden workerd durability, hibernation, and machine framing - #67202

Merged
kitlangton merged 1 commit into
devfrom
workerd-durability
Nov 30, 2026
Merged

kitlangton merged 1 commit into
devfrom
workerd-durability

Conversation

@kitlangton

@kitlangton kitlangton commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Harden Cloudflare Durable Object durability, WebSocket hibernation, workspace lifecycle locking, and machine framing/crypto boundaries.

Before:

  • sqlite.workerd.ts swallowed Durable Object COMMIT errors via an already-spent single-shot callback and could deadlock its 1-permit DB semaphore if txn.rollback() threw.
  • Waking a ServerWorkerd DO with hibernated machine WebSockets booted the full configLocation to check experimental.machines, arming 1-minute LocationActivity sweeps and defeating hibernation; unguarded serializeAttachment(null)/close() calls could skip held.end or crash wake, and acceptWebSocket throws leaked MAX_HANDSHAKES permits.
  • Workspace.ID only checked startsWith("wrk") (letting malformed IDs skip idle suspension), Workspace.create raced destroy, scheduleNextIdle could drop alarms or never clear stale DO alarms, failed suspends kept stale cached connections and backed off from a stale timestamp, and Environments.has/availability reported missing/destroyed wrk_* IDs as Cold.
  • Oversized outbound fs messages or invalid numeric fields tore down the entire machine link, Frames.read always copied single-frame messages through Buffer.concat, WorkerClient.fromWebSocket accepted < 16B frames into an unbounded queue, Noise.state() allowed an unrestorable MAX_NONCE snapshot, and x25519.workerd.ts silently zero-extended/truncated non-32-byte keys.

After:

  • packages/core/src/database/sqlite.workerd.ts: rewrite makeWithTransaction with Effect.uninterruptibleMask + Deferred so COMMIT failures propagate as SqlError and throwing txn.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 idempotent MAX_HANDSHAKES permit release across all error/scope paths, and introduce MachinesFlag with Config.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: tighten Workspace.ID to /^wrk_[A-Za-z0-9_-]+$/u (with OpenAPI pattern), serialize Workspace.create under locks and scheduleNextIdle under idleAlarmSemaphore, wire onIdleCleared to storage.deleteAlarm, evict connections + use fresh Clock.currentTimeMillis on suspend failure, and check workspaces.exists(id) in Environments.has/availability.
  • packages/core/src/machine/{driver,frames,wire,worker-client,noise,x25519.workerd}.ts: pre-check outbound fs byte length and read ranges before queueing, return single-piece frames directly, share Frames.MIN_FRAME/MAX_FRAME across worker-client and server handlers, refine wire.ts numeric schemas, reject spent MAX_NONCE snapshots, and enforce exact 32-byte keys in x25519.workerd.ts.
Check Result
packages/schema, protocol, sdk tests 97 / 17 / 7 pass
packages/core & packages/server targeted suites 353 / 31 pass
probe:workerd & probe:workerd-machine-link pass
bun run check, typecheck:tsc, check:generated, Prettier pass

Read: 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_)

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant