Repository navigation
fix(studio-server): sub-composition previews build off the request thread within a deadline - #7857
Conversation
jrusso1020
left a comment
There was a problem hiding this comment.
Request changes at b4fb520d. Moving the sub-composition build onto a worker under a deadline is the right shape, and the deadline/replace logic reads correctly. But the head doesn't build, and the worker can't start from source, so the new tests and the existing sub-composition route tests fail.
Blocking
-
Typecheck/build fails.
src/helpers/subCompositionBuild.ts:73:started.on("error", (error) => replace(started, error)). Theerrorparameter isunknown, which givesTS2345: Argument of type 'unknown' is not assignable to parameter of type 'Error | undefined'. This failsBuild,Typecheck, and every job that builds the workspace first (Test (studio/cli/runtime/support), CLI smoke, Producer, SDK). Fix:(error) => replace(started, error instanceof Error ? error : new Error(String(error))). -
The worker can't load from source, so every sub-composition preview returns 500 there.
subCompositionWorker.tsimports./subComposition.js. When the.tsentry runs in a Node worker, that relative.jsdoesn't map to the.tsfile. Node 22.18+ strips types itself, so the worker file loads, but its import doesn't resolve. Adding--import tsx/esmto the worker'sexecArgvdoesn't fix it either. I tried it directly on Node 22.22.3 and gotERR_MODULE_NOT_FOUND …/src/helpers/subComposition.js. The precedent,shaderTransitionWorker, only works because it has no relative imports.- Locally at this head:
vitest run src/routes/preview.compDeadline.test.ts src/routes/preview.compPath.test.tsfails 4/4. Each request returns 500 with that error in stderr, including the two existingpreview.compPathtests. - The same path affects any source-mode host, e.g. Studio dev loading studio-server from
src. - Possible fixes: in source mode, start a small
.mjsbootstrap that callsregister()fromtsx/esm/apiand thenimport()s the.tsworker. Or keep the worker free of relative imports. Then re-run both test files.
- Locally at this head:
-
Commentscheck:createStudioApi.tscomment share rose (the new doc line onsubCompositionDeadlineMs). Cut it or move it, per the ratchet's message.
Non-blocking
- A deadline on a queued job kills the running one. When a job times out while another job is running (e.g. one request's
idsjob behind a different request's slowbuild),replace()ends the worker. The running build is resent and starts over from scratch. The comment "after a deadline the culprit is gone" doesn't hold in that case. It's bounded by each job's own deadline, so it isn't a correctness bug. Tracking the in-flight id, and only replacing when the timed-out job is the one running, would avoid it. - The remaining request-thread passes aren't bounded. After the worker returns, the route still runs
recordPreviewReferences(an element walk),transformPreviewHtml,injectStudioPreviewAugmentations(which does aparseHTMLatpreview.ts:218) and the codec-map scan. All of these run synchronously on the request thread, over a page the worker took up to 30 s to build.withinDeadlinecan't interrupt synchronous work. Either a test with a page that builds just inside the deadline, or a note on why those passes are cheap relative to the build, would close this. - The test plan's "fails without the fix" run and the existing
preview.compPathrun are still pending in the description. Point 2 above is what they would have shown.
Need: real (a project's own page could hold the request thread with no bound) · Size: right-sized · Reuse: follows the existing worker-entry pattern, but that pattern doesn't carry over to a worker with relative imports · Simpler: fine · Structure: good split (route / pool / worker)
Risk: as posted, every sub-composition preview 500s in source mode and the workspace doesn't build. Once 1–3 are fixed, the risk is low, with the packaged-CLI dist worker path still unexercised.
— Rames
6ccb0f5 to
2386bc8
Compare
|
Answers to the review at b4fb520, all at 2386bc8:
Non-blocking:
|
4aa3747 to
b497f5e
Compare
…used off the request thread
…read within a deadline
b497f5e to
1d189fd
Compare
jrusso1020
left a comment
There was a problem hiding this comment.
Approve at 1d189fd6. All three blockers from my request for changes at b4fb520d are fixed, and so are both non-blocking points.
The earlier blockers
- Typecheck. The
errorhandler now narrowsunknown(subCompositionBuild.ts). Build, Typecheck and the jobs that build first are green. - The worker from source under Node. The new
subCompositionWorker.dev.mjscallsregister()fromtsx/esm/api, then imports the.ts. Bun runs the.tsdirectly, anddistuses the built.js. Locally at this head,preview.compDeadline.test.ts+preview.compPath.test.tspass 5/5, through a real Node worker started fromsrc. Atb4fb520dboth files failed 4/4 withERR_MODULE_NOT_FOUND. - Comments check: green.
The earlier non-blocking points
- A queued job's deadline no longer kills the running build. Only a timeout on the first pending job (the running one) replaces the worker. A queued job past its deadline is just dropped.
- The passes after the build are off the request thread.
ensureHfIds, the studio augmentations (with theirparseHTML) and the preview variables now run as the worker'sfinishjob. The new "inside its deadline" test holds the request thread to under 200 ms of stall across a page of 10,000 cells, the host transform and the codec scan included.
Control. I ran the new deadline file against the merge base's route and helpers, with the test file at head. It did not finish within 500 s (timeout exit 124): the huge page built on the thread that also runs vitest's own timers, so even the test timeout couldn't fire. That is the bug this fixes.
Non-blocking
- One worker serves every project. A page that runs to its deadline delays every sub-composition preview queued behind it, and those can 503 when their own deadlines pass. That is bounded (30 s) and better than holding the request thread, but worth knowing for a server that previews several projects at once.
- A dropped queued job still runs. The worker runs it anyway and its reply is ignored. So "first pending = running" can be off while the worker works through dropped jobs, and a later deadline may then end the worker in the middle of one. That is harmless and also bounded.
- The codec scan (
scanHtmlOpeningTags) still runs on the request thread over the built page. It is a linear tag scan, and the large-page test covers it. - Packaged CLI from
distand Windows are still not run, as the body says. The tsup entries for both studio-server and the CLI emitsubCompositionWorker.jsbeside the chunk that looks for it.
Need: real (a project's own page could hold the request thread with no bound) · Size: right-sized · Reuse: follows the worker-entry pattern and now handles relative imports · Structure: good (route / pool / worker) · Risk: low.
— Rames
What
GET /projects/:id/preview/comp/*now builds the sub-composition page on a worker thread, within a deadline (30 s by default). A page still building at the deadline is answered503with " took too long to build", and its worker is ended and replaced.Why
A sub-composition page is the project's own HTML, and a shared or imported project can carry any page. The route built it on the server's request thread with no time limit, in three steps: the hf-id pin on the raw file,
buildSubCompositionHtml, then the hf-id pass on the transformed page. A page big enough to take seconds held every other request to the server for that long. One that never finished held the server for good. The media-codec probes for the page's media had no limit either.Related work
The root preview build already runs through the host's
adapter.bundle, which a host can run off its main thread. This gives the sub-composition build the same separation inside studio-server itself, for every host.How
helpers/subCompositionWorker.ts: the worker. Abuildjob pins hf-ids on the raw file, aspinSubCompHfIdsdid, and runsbuildSubCompositionHtml. Anidsjob pins hf-ids on the page the host transformed.helpers/subCompositionBuild.ts: one long-lived worker, so builds queue one at a time. Its entry is found beside the module that starts it: the built.jsindist, else the.tssource throughtsx, asshaderTransitionWorkerPooldoes.runOffThread(job, deadline, compPath)refuses withSubCompositionDeadlineErroronce the deadline passes. The worker is then ended, and the builds queued behind the refused one go to a fresh worker.withinDeadlineputs the same deadline on the media-codec probes. They are child processes, so nothing blocks; the probes themselves finish and fill the cache.createStudioApi(adapter, { subCompositionDeadlineMs })sets the deadline; tests use a short one.tsup.config.tsin studio-server and the CLI emitsubCompositionWorker.jsbeside the chunks that start it, as the CLI already does forshaderTransitionWorker.Test plan
src/routes/preview.compDeadline.test.tspreview.compDeadline.test.tsand the existingpreview.compPath.test.tspass (5 of 5); the deadline file passes 3 runs in a row, and 3 more beside one busy loop per CPU, as the flake gate runs it.Not exercised: a packaged CLI build starting the worker from
dist(the CI build compiles it; nohyperframes previewrun was made), and Windows.