Skip to content

Bound every backend client with a request deadline - #198

Merged
neuromechanist merged 8 commits into
mainfrom
173-admin-api-clients-pass-an-undefaulted-abortsignal-hung-upstream-stalls-ssr
Jul 29, 2026
Merged

neuromechanist merged 8 commits into
mainfrom
173-admin-api-clients-pass-an-undefaulted-abortsignal-hung-upstream-stalls-ssr

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #173.

Problem

src/lib/admin-api.ts, dashboard-api.ts and collaborators-api.ts passed signal: init.signal on every fetch, where signal is optional and no caller supplied one. The existing try/catch covers a request that fails (refused connection, DNS, TLS). It does not cover one that hangs: a connection that opens and never writes a response never settles, so there is nothing to catch. These run during SSR at the edge, so the page render hangs with them.

upload-client.ts had the same gap on its two JSON calls, which took no signal at all.

Changes

New src/lib/request-deadline.ts holds the single resolveSignal(init, fallbackMs) (an AbortSignal.timeout combined with the caller's signal via AbortSignal.any) plus the two shared constants. Four clients had already hand-copied that function, each carrying a "mirrors the other one deliberately" comment; they all import it now, so identical behaviour under a hung upstream is enforced by construction rather than by comment.

Deadlines are per-operation, exported as a table per client so the differences are a pinned contract:

Client Operation Deadline Why
admin-api list 5s primary SSR content
admin-api decide (approve/deny) 15s hands off to the publish orchestrator; user-initiated click
dashboard-api list 5s primary SSR content
dashboard-api status 2.5s decorative badge, fanned out per dataset, already degrades to "draft"
dashboard-api mutate 15s orchestrator handoff / cascade delete
collaborators-api list 5s primary SSR content
collaborators-api invite 15s carries a GitHub round-trip
upload-client create 30s cost scales with the dataset's file count
upload-client finalize 60s runs after every byte is in S3; aborting early orphans the draft

The byte-transfer path (runUploadQueue / putToPresignedUrl) deliberately gets no deadline. Its signal is the user's cancel button and a multi-gigabyte PUT legitimately runs for minutes; a timeout there would abort healthy uploads. That is now stated in the code rather than being an accident.

createDraftDataset and finalizeDataset gained an optional init argument (fetch override + timeout), matching every other client, which is also what makes them testable.

Tests

bun run test: 929 passing, up from 906.

New suites drive the real abort path with a fetch that only settles by rejecting on its signal, per the convention in imports-admin-api.test.ts — no fake timers. Each client covers: every call site aborts when hung, a caller-supplied signal still wins alongside the deadline, and the deadline ordering holds (decorative < primary < mutation).

AbortSignal.timeout() does not expose its duration, so these pin the values, not the wiring — the same caveat already documented on IMPORT_TIMEOUTS_MS.

bun run typecheck 0 errors, bun run lint clean.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Both review findings addressed.

1. approve and deny no longer share a deadline

Verified the claim against the backend before changing anything: POST /admin/publish/:id/approve (nemar-cli backend/src/routes/admin/publish.ts) does await runPublicationApproval(...) with only waitForPublicPropagation deferred to waitUntil. So the sixteen-step orchestrator genuinely blocks the response, and 15s was too short for a healthy run.

ADMIN_TIMEOUTS_MS now has three entries instead of two:

before after
list 5s 5s
deny 15s (as decide) 15s
approve 15s (as decide) 120s

120s is still a bound, which is the point — #173 was about an unbounded wait, not a short one. The real fix is making approve non-blocking (kick off the job, poll progress); that needs a backend change and is now tracked in #200, referenced from the constant's comment so it doesn't become a dangling note.

2. The tests now prove the wiring, not just the values

Reproduced the reported gap first: swapped approve's fallback to ADMIN_TIMEOUTS_MS.list and the whole suite stayed green, confirming the explicit timeoutMs: 10 in every case masks which constant the call site references.

Added a deadline wiring suite to all four test files that spies on AbortSignal.timeout and runs with no timeoutMs override, so the fallback becomes observable:

const spy = vi.spyOn(AbortSignal, "timeout");
await approvePublicationRequest("nm-xyz", { fetch: okFetch(...) });
expect(spy).toHaveBeenCalledWith(ADMIN_TIMEOUTS_MS.approve);

Re-ran the same mutation against the new suite: passes the approve deadline when approving fails as it should. Mutation reverted.

Covers every call site across admin-api, dashboard-api, collaborators-api, and upload-client.

3. Non-blocking observation filed

The upload.astro finding — a failed finalize re-runs createDraftDataset from scratch instead of retrying finalize against the existing draft.id — is filed as #201. Agreed it's pre-existing and out of scope here; the PR makes it reachable via a hang rather than only via a hard failure, which is worth the follow-up.

Full suite green, typecheck 0 errors, lint clean.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review re-verified. Both findings closed out, no new ones.

On the 120s approve deadline: independently traced through the orchestrator again and confirmed there is no upper bound derivable from the code — worst case is retry-count x (GitHub API latency + backoff) summed across roughly ten sequential steps, which depends on the architecture rather than on anything readable in the source. So the number is arbitrary; the comment says as much rather than implying precision, and #200 is where it actually gets fixed instead of being tuned further.

On the wiring tests: mutation-tested all four files, not just the one I had checked:

mutation test that caught it
getPublishStatus fallback .status -> .list dashboard-api / passes the decorative deadline for the publish-status fan-out (expected 2500, got 5000)
inviteCollaborator fallback .invite -> .list collaborators-api / passes the invite deadline when inviting (expected 15000, got 5000)
finalizeDataset fallback .finalize -> .create upload-client / passes the finalize deadline when finalizing (expected 60000, got 30000)
approve fallback .approve -> .list admin-api / passes the approve deadline when approving

Each mutation was caught by exactly the test that should catch it and by nothing else. All reverted.

On spy hygiene: vi.restoreAllMocks() is scoped inside each deadline wiring describe. dashboard-api.test.ts now has two afterEach blocks in different describes, which vitest scopes independently — full suite run with the spies live shows no cross-test contamination from a leaked AbortSignal.timeout.

On regressions: git diff --stat d4a9287..23c29e6 touches only admin-api.ts plus the four test files. The four previously-migrated clients and the byte-transfer path are byte-for-byte unchanged by the review commits, so the first pass's clearances still hold.

CI green across build, lint, test, typecheck, and Cloudflare Pages.

@neuromechanist
neuromechanist merged commit 22dd214 into main Jul 29, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the 173-admin-api-clients-pass-an-undefaulted-abortsignal-hung-upstream-stalls-ssr branch July 29, 2026 14:00
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.

Admin API clients pass an undefaulted AbortSignal (hung upstream stalls SSR)

1 participant