Skip to content

fix(js-exec): wait for in-flight filesystem changes after a timeout (fixes #780) - #793

Open
anhnt256 wants to merge 2 commits into
vercel-labs:mainfrom
anhnt256:fix/js-exec-timeout-fs-drain
Open

anhnt256 wants to merge 2 commits into
vercel-labs:mainfrom
anhnt256:fix/js-exec-timeout-fs-drain

Conversation

@anhnt256

@anhnt256 anhnt256 commented Dec 4, 2026

Copy link
Copy Markdown

Fixes #780.

Problem

js-exec returns after timing out while a filesystem change it started is still running, so the change is silently lost to the next command:

const bash = new Bash({
  fs,
  javascript: true,
  executionLimits: { maxJsTimeoutMs: 400 },
});
// SlowWriteFs whose writeFile completes 600ms after exec() starts
const result = await bash.exec(
  `js-exec -c "require('fs').writeFileSync('/out.txt', 'new\\n')"; echo "js-exec exit $?"; cat /out.txt`,
);
// before: exit 0, stdout "js-exec exit 124\nold\n", file is "old" when exec()
//   returns and becomes "new" afterwards
// after:  exit 0, stdout "js-exec exit 124\nnew\n", file is "new" when exec()
//   returns, while js-exec still reports exit 124 and its timeout message

On timeout, run terminates the worker without waiting for the host function the guest is blocked in, so the in-flight writeFile (and the same for appendFile, copyFile, rename, rm, mkdir, symlink, chmod) lands after exec() has already returned.

Fix

In run-runtime.ts, the eight mutating filesystem host functions now go through attemptMutation, which tracks the mutation currently in flight (at most one — the guest is single-threaded and its synchronous fs calls block it). When the run ends with a timeout or abort, js-exec waits for that mutation to land before returning, so the next command observes it. Reads are not tracked: a read in flight changes nothing the next command could observe.

The wait is bounded so js-exec can never hang on a stalled filesystem: at most one more maxJsTimeoutMs, and it stops immediately when the caller's abort signal fires. The timer is always cleared before returning, so no timer or callback outlives the command. This does not touch the dispatcher or the timeout system, and it does not cover changes made through child_process, whose own filesystem calls can still land late.

Tested

@vercel

vercel Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@anhnt256 is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@auto-maintain

auto-maintain Bot commented Dec 4, 2026

Copy link
Copy Markdown

🤖 auto-maintain review

Automated, advisory triage for @anhnt256's PR. Facts below are read from the GitHub API.

Check Result
Author's merged PRs (this repo) 0 — ⚠️ first-time contributor
Account established ✅ (age 3866d · 4 followers · 74 public repos)
Commits signed/verified ✅ 2/2
Changeset included ⚠️

Review panel: 🔴 high highest severity

just-bash maintainer code review: 🟢 low

No actionable correctness or maintainability issues found in the complete diff.

General code review: 🟢 low

No actionable correctness issues found in the complete diff.

Adversarial security: 🟢 low

No actionable adversarial security issues found in the complete diff.

Adversarial security (second opinion): 🔴 high

The fix itself grants the guest no new capability, but the drain observes the in-flight filesystem promise with the sandbox-patched `Promise.prototype.then`, which produces false-positive security violations and a potential unhandled-rejection crash exactly in the stalled-filesystem path this PR adds; switch to `_promiseThen`/native `await` before merge.

  • packages/just-bash/src/commands/js-exec/run-runtime.ts:841 — `drainPendingFsMutation` observes the in-flight mutation through `pending.then(...)` (and `Promise.race`, which also invokes `.then`). Inside a Bash execution the DefenseInDepthBox has patched `Promise.prototype.then`, so both callbacks are wrapped in `runGuardedCallback`. The whole point of this drain is the case where the mutation outlives the budget, so the promise typically settles after `bash.exec()` has returned and `defenseHandle.deactivate()` has run — at which point the guard records a spurious `promise_then_after_deactivate` violation and fires the embedder's `onViolation` callback on every late write. Worse, on the rejection path the guard does `throw args[0]` instead of calling the callback: that rethrow rejects the promise `Promise.race` creates from `settled.then(resolveElement, reject)` and discards, producing an unhandled rejection (fatal under Node's default `--unhandled-rejections=throw`) whenever a slow remote filesystem write eventually fails after the drain gave up. The repo already documents the fix for exactly this situation — `_promiseThen` from `src/security/trusted-globals.ts`, or settling via native `await` in an async IIFE as `src/commands/worker-bridge/bridge-handler.ts:91` does (native `await` bypasses the patched prototype method; the explicit `.then` does not). The adjacent comment claiming "no timer or callback outlives the command" is incorrect for these callbacks.
  • packages/just-bash/src/commands/js-exec/js-exec.timeout-fs-drain.test.ts:244 — The new test file has no trailing newline (`\ No newline at end of file`). The root `lint` script runs `biome check .` with the formatter enabled, which normalizes a final newline, so this fails CI despite the PR claiming lint is clean.

Standard Bash and host portability: 🟢 low

No Bash or ordinary-host portability issues found in the complete diff.

Posted by auto-maintain. This automated code review is advisory; a human maintainer makes the call.

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.

js-exec returns after timing out while a filesystem change it started is still running, so the change lands after the next command

1 participant