Skip to content

fix(studio-server): opening a project no longer waits on its drive to answer a write - #8833

Open
miguel-heygen wants to merge 6 commits into
mainfrom
fix/project-writes-off-main
Open

miguel-heygen wants to merge 6 commits into
mainfrom
fix/project-writes-off-main

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

Opening a project no longer holds the host's thread when the project's drive answers reads but not writes. The hf-id stamp pass (stampProjectHfIdsAsync) and a project's first history id (projectHistoryId) now write the project only through fs/promises. A drive that stays silent holds a libuv pool thread, not the host's event loop.

Why

Both wrote the project with blocking calls on the caller's thread. replaceFileAtomically runs accessSync(W_OK), then writeFileSync, renameSync and their siblings; mkdirWithinProject runs mkdirSync. A host that embeds Studio's server in its own main process (a desktop app) runs these during a project open. Its main thread then waited for as long as a network or cloud folder left a write unanswered, while the window stayed frozen. The stamp pass rewrites .hyperframes/hf-ids-stamped.json on every run, so every open reached this, not only the first.

stampProjectHfIdsAsync already yielded to the event loop between files, but each file's open, read and write ran synchronously, so a silent drive still froze the host inside one file.

Related work

None open on this path. A host that calls the synchronous stampProjectHfIds on its main thread needs to move to stampProjectHfIdsAsync to get this.

How

  • helpers/hfIdPersist.ts: the stamping rules are written once, as a pass that yields the file work it needs (read, open for stamping, replace if unchanged, write the record, pause). A synchronous and an asynchronous driver answer those steps. stampProjectHfIds and stampFileHfIds keep the synchronous driver and behave as before. stampProjectHfIdsAsync uses the asynchronous one, so every file call it makes is on fs/promises.
  • core/atomicFile.ts: replaceFileAtomicallyAsync (already there) takes its read-only refusal from the same rule as the synchronous writer (readOnlyOrMissing), not from a copy.
  • core/safePath.ts: mkdirWithinProjectAsync, the fs/promises twin of mkdirWithinProject. Both get their folder list and error rule from one place (foldersToMake, madeOrThrow).
  • studio-server/helpers/safePath.ts: mkdirOwnedWithinProjectAsync; the link check stays synchronous (lstat, a read).
  • history/historyId.ts: projectHistoryId makes .hyperframes and writes history-id with the async twins.

Not changed: the directory walk (walkDir) and reads stay as they were; lstat and readdir are reads, which the drive in question still answers. History records under the history root (the tool's own cache folder, not the project) keep their synchronous writes.

One order to know about: a pass that is waiting on the drive can see a check (file unchanged) and its write separated by other work of the same process. The pass runs before a host serves or watches the project, as its doc says, so no in-process writer edits that project's files meanwhile. Writers in other processes could interleave before this change too.

Test plan

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

helpers/projectDriveWrites.test.ts stands in for a drive under the project that answers reads but never a write. It is patched on node:fs itself and synced to every module's import. A blocking write call holds the calling thread for 1.5 s. An fs/promises write never settles. A 20 ms timer is set, then the call starts:

  • "holds no thread of the host's while the stamp pass waits on it": stampProjectHfIdsAsync on two compositions without ids.
  • "holds no thread of the host's while a project's first history id waits on it": projectHistoryId on a project with no .hyperframes.
    Each expects the timer on time (under 500 ms late) and the call still waiting on the drive.

fails without the fix: with the five changed source files as on main, both fail. The timer fired 11,987 ms late during the stamp pass and 4,487 ms late during the first history id. With the change, both pass (45 ms for the file).

hfIdPersist.yield.test.ts "lets the event loop run between files" asserted that at least one file was stamped by the first setImmediate. That held only while each file was stamped synchronously. It now reads "lets the event loop run before the pass is done": the timer must fire before all 40 files are stamped. A pass that ran synchronously still fails it.

  • studio-server: the stamp, safe-path, owned-folder and history tests, 461 passed, 3 runs in a row; the full suite, 2536 passed (152 files).
  • core: atomicFile, atomicFile.win32Paths and the four safePath test files, 260 passed, 3 runs in a row.
  • tsc --noEmit clean for core and studio-server; oxlint and oxfmt clean on the changed files.
  • Linux only. Not run against a real network or cloud drive; the stand-in covers the call shape.

… answer a write

The hf-id stamp pass and a project's first history id wrote the project with
blocking calls on the host's own thread, so a drive that answered reads but
not writes held that thread for as long as it stayed silent. The stamp pass
now has one owner of its rules and two drivers; stampProjectHfIdsAsync makes
every file call on fs/promises, and projectHistoryId makes its folder and id
file on fs/promises.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

On the pool-thread note: right, each write the drive never answers keeps one libuv pool thread (4 by default) for as long as the drive stays silent. A timeout on our side would only stop the caller waiting, which the async path already doesn't do; the thread stays inside the system call until the drive answers, so it would not free the pool. What would cap it is one stamp pass per project at a time, so reopening a project on a stuck drive joins the pass already waiting instead of starting another stuck write. I'll make that a follow-up if a stack shows other file work queueing behind stuck writes; this PR keeps the host's window answering, which is the freeze people hit.

Head is now e6cd27f: the only change since fa6efcb removes two doc comments, because the comment-share check refused the added lines.

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 273 (base branch 271), smooth 55 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Quarantined, measured but not gated (0)

Unstable (1)

  • seqab-none-pct-r0-root-z100: tracking 0.05, pressJump 0, drop 0, reload 0, render 0.03, renderKey -, undo true, teleport true / tracking 0.05, pressJump 0, drop 0, reload 0, render 0.03, renderKey -, undo true, teleport true / tracking 0.05, pressJump 0, drop 0, reload 0, render 0.03, renderKey -, undo true, teleport true

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Size budget (6c09256): @hyperframes/inspector bundles core's safePath, so mkdirWithinProjectAsync grows its tarball. Packed locally it is 64 bytes larger than main (938,267 against 938,203). On CI it measured 942,944, more than 1% over the 933,584 budget, because main already sat within about 40 bytes of that line. scripts/package-size-budgets.json raises the inspector budget to 952,400, about 1% above the CI measurement, as in #8790. The growth is code, not something to leave out of the tarball.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review at 6c092565: APPROVE. Since fa6efcb1 there are two changes and no code changes. Two doc-comment lines are removed. The @hyperframes/inspector packed budget goes from 933,584 to 952,400 bytes: Build measured 942,944 at e6cd27f0, because the inspector bundles core and now carries the async folder-write helpers. The reason is in the commit message ("fit the inspector size budget to core's async project folder writes"). A line in the PR body would satisfy the check's wording too. What follows was reviewed at fa6efcb1.

  • Fails without the fix: I put the five changed source files back as they are on main, and both tests in projectDriveWrites.test.ts fail. The timer fired 12,013 ms late during the stamp pass and 4,509 ms late during the first history id, which matches the body. At head, the stamp, history and safe-path tests pass: 452 tests, 3 runs in a row.
  • One rule owner: the stamp rules now live once in generator passes (stampPass, hashAfterStamp, stampFile). The sync and async drivers only answer file steps. A driver error goes back in through pass.throw, so each existing try/catch still decides the outcome: a bad record parses everything, an unreadable file returns null, and a failed replace warns and returns null. That matches main's stampFileHfIds. stampProjectHfIds and stampFileHfIds keep their sync behavior.
  • Async path: every project write in stampProjectHfIdsAsync and projectHistoryId now goes through fs/promises: the no-follow open, the stat and read, the unchanged check, the temp-and-rename replace, and the mkdir. The read-only refusal, foldersToMake and madeOrThrow are shared with the sync versions, not copied.
  • historyId: this function already awaited its reads, so moving its two writes to async adds no new interleave. The history-root records stay synchronous, as the body says.

Not blockers:

  1. A drive that never answers a write now holds a libuv pool thread for each stuck call (4 threads by default). The stamp pass makes one call at a time, but after a few opens of projects on such a drive, the host's other fs/promises and crypto work would queue behind the stuck calls. The window still stays responsive, which is the fix. A later timeout or abort on these writes would bound it.
  2. walkDir and isLinkFreeWithinProject stay synchronous. They only read, and this drive still answers reads, as the body says.

Need · Size · Reuse · Simpler · Structure: stops a project open from freezing the host's window · +396/−97, of which 120 are a new test · one set of rules with two drivers instead of duplicated sync and async stampers · the generator is the smallest way to keep one copy of the rules · fits the existing helpers.
Risk: low to medium. The sync path is unchanged in behavior. The async path is new and has only been tested against the stand-in drive, not a real network drive.

— Rames

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Merged main and shortened one comment:

  • packages/core/src/safePath.ts: the mkdirWithinProjectAsync doc comment is now 120 characters ("a write the drive never answers" in place of "a drive that never answers a write"). Same meaning.
  • scripts/package-size-budgets.json conflicted with main, which raised @hyperframes/inspector packed to 945,000 in the meantime. The merge keeps this PR's 952,400, which also covers this PR's growth on top of main's. Every other budget is main's.

Against main, the diff is still the same eight files.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review at a6c05362: request changes. The code is unchanged and still approve-ready. After the main merge, one required check is red because of the combined size.

  • Build fails verify:packed-manifests: "@hyperframes/cli unpacked size 26091227 bytes is over its budget of 25830000 by more than 1%." Main passes Build (it is green at 47e6307e, the main this merge brought in), and so did 6c092565 before the merge. The CLI bundles core and studio-server, so this PR's async folder-write code plus main's growth since the last base takes it over. The fix is the same as for the inspector: raise @hyperframes/cli.unpacked in scripts/package-size-budgets.json with the reason in the body.
  • Test (runtime) also failed, on an unhandled ReferenceError: window is not defined from webAudioTransport.test.ts (mediaProxy.ts:406 isRenderMode) after teardown. This PR does not touch that path, and it is the same late-callback teardown problem as hf#8834. I'm treating it as a flake: a rerun should clear it, and it does not block.

I'll approve the next head once required CI is green.

Need · Size · Reuse · Simpler · Structure: unchanged.
Risk: low. Build budget only.

— Rames

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

@hyperframes/cli unpacked budget: 25,830,000 → 27,395,789.

Why: the CLI bundles studio-server, so this PR's async stamp pass and folder writes grow the CLI's chunks by about 3.7 KB (the largest, 453.40 → 456.07 KB). Main already sat just under the 1% line: built on main (22f268e), the CLI unpacks to 26,087,457 bytes against a 26,088,300 limit, 843 bytes of room. At this PR's head, CI measured 26,091,227, past it.

The new value follows verify-packed-manifests.mjs's own rule (lines 31-34): the measured size × 1.05, ceil(26,091,227 × 1.05). Producer and every other budget are unchanged; producer measures 3,741,650 on main, within its 1%.

The size check on this branch, run locally: red ("@hyperframes/cli unpacked size 26091227 bytes is over its budget of 25830000 by more than 1%") with main's budget, green ("Verified 15 packages are within their size budgets.") with this one.

Test (runtime) failed on an unhandled window is not defined rejection from webAudioTransport.test.ts after its environment was torn down (all 7,369 core tests passed). That file is not part of this PR.

This branch has not been deployed

No deployments
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.

2 participants