You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(js-exec): wait for in-flight filesystem changes after a timeout (fixes #780) - #793
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:
constbash=newBash({
fs,javascript: true,executionLimits: {maxJsTimeoutMs: 400},});// SlowWriteFs whose writeFile completes 600ms after exec() startsconstresult=awaitbash.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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #780.
Problem
js-execreturns after timing out while a filesystem change it started is still running, so the change is silently lost to the next command:On timeout,
runterminates the worker without waiting for the host function the guest is blocked in, so the in-flightwriteFile(and the same forappendFile,copyFile,rename,rm,mkdir,symlink,chmod) lands afterexec()has already returned.Fix
In
run-runtime.ts, the eight mutating filesystem host functions now go throughattemptMutation, 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 throughchild_process, whose own filesystem calls can still land late.Tested
src/commands/js-exec/js-exec.timeout-fs-drain.test.ts: the exact js-exec returns after timing out while a filesystem change it started is still running, so the change lands after the next command #780 repro, all eight mutations from the issue, plus a never-completing write that must not hang js-exec — 10/10 pass (9 failed before the fix).