Repository navigation
Conversation
There was a problem hiding this comment.
This doesn't fix #63747 yet. I made the race happen every time by having the timeout branch wait for the job to settle before calling jobs.background. A foreground call (sleep 0.3; echo hi; exit 3 with timeout: 10) still returns "Command moved to the background… did not finish within its timeout" with status: "running", and the real result still arrives later as a notice. That happens on base and on this PR alike. On this PR the finished job is also left in the job registry afterwards; on base it is cleaned up.
The fix is in the background: true branch, but the issue is about the foreground timeout branch. There, jobs.background(job.id) still maps any returned info to { type: "backgrounded", timedOut: true }. The new settled: true flag never reaches callers, because Job.background returns only result.info. The new job-background-settled.test.ts passes without exercising that path. A regression test should drive the shell tool through its timeout with the job settling first, and check that the call returns the command's own output and exit code with no notice. It would replace the Job-level test, which also relies on a 50 ms sleep.
The tests touching job, tool-shell and session-execution pass (163/0), and typecheck:tsc is clean.
Public API: unchanged (only internal Job behaviour and the shell tool's result in this race).
| if (job.isBackgrounded) return [{ info: snapshot(job) }, jobs] | ||
| // Work that already settled has nothing left to move. Marking it would hand the caller a background result | ||
| // whose completion notice was already written, so the call would report a running command that never notifies. | ||
| if (job.info.status !== "running") return [{ info: snapshot(job), settled: true }, jobs] |
There was a problem hiding this comment.
settled: true is never visible outside this function: background returns only result.info, which still has a snapshot for a settled job. So the foreground timeout caller in shell.ts (jobs.background(job.id).pipe(Effect.map((info) => info ? { type: "backgrounded", ... } : undefined))) still reports a settled command as moved to the background. That is the race in #63747, and it is unchanged. That branch needs to treat a non-running result as finished (e.g. { type: "finished", info }) and go on to Deferred.await(settled), or background needs to return undefined or a tagged result for settled work.
Also, this returns early without marking or consuming the job. Since wait doesn't consume recoverable jobs either, a settled shell job reached this way stays in the registry: I saw jobs.get(id) still return it after the call, while on base it is removed. The early-return paths should consume it (as block does for a finished job).
|
|
||
| if (input.background === true) { | ||
| yield* jobs.background(job.id) | ||
| const moved = yield* jobs.background(job.id) |
There was a problem hiding this comment.
This is the explicit background: true path, not the timeout path from the issue. Here it changes behaviour: a fast background: true command now returns inline instead of as a background handoff. That may be fine, but it's a separate change. The timeout branch further down (jobs.background(job.id) inside Effect.timeoutOption) is untouched, and it is where the reported race happens.
|
you're right on both counts: the timeout branch mapped every returned info to fixed in 620032b.
on the test: checked on a clean checkout of the branch: the branch is behind |
There was a problem hiding this comment.
The new commit fixes the race on the foreground timeout path. I reran the same probe as last time: the timeout branch was patched to wait for the job to settle before jobs.background, then a foreground sleep 0.3; echo hi; exit 3 ran with timeout: 10. The call now returns the command's own output and exit: 3 with status: "completed". No notice is posted, nothing is left pending, and the settled job is consumed. On base the same call said "moved to the background" and the result arrived later as a notice.
The tests touching job, tool-shell, session-execution and shell-restart pass (167/0), and typecheck:tsc is clean.
Public API: unchanged.
The earlier point about the timeout branch and consuming the settled job is addressed. The background: true branch still carries its separate inline-result change (open thread).
There was a problem hiding this comment.
This push only merges dev; the PR's own changes are the same as at 620032b. I reran the same probe on the merged head: the timeout branch was forced to wait for the job to settle before the move, then a foreground sleep 0.3; echo hi; exit 3 ran with timeout: 10. It still returns the command's own output and exit: 3 with status: "completed". No notice is posted, nothing is left pending, and the job is consumed.
The tests touching job, tool-shell, session-execution and shell-restart pass (167/0), and typecheck:tsc is clean.
Public API: unchanged.
Nothing new. The background: true branch still has its separate inline-result change (open thread).
A foreground shell call that hits its timeout moves the command to the background. If the command exits between the timeout firing and the move, Job.background still marked the settled job as backgrounded, because the guard only skipped jobs without a recovery. Shell jobs always carry one. The call then returned "moved to the background" and registered a completion notice, but the settlement had already written its own row, so the notice never arrived and the output was lost. background now takes reportSettled for callers that read the settlement themselves. The foreground timeout path passes it, so a job that finished in that window comes back as the command's own result, and its generation is consumed instead of being left pending. Callers that did not ask keep the old behaviour, because explicitly backgrounded work still needs its notification.
d245632 to
f0c8bdc
Compare
|
rebased onto dev at 85951cb. the job registry changed under this (listing/update, consume), so the fix moved with it: background now takes reportSettled, and the foreground timeout path passes it. jobs that finish before the move come back as the command result and nothing is left pending. the default call still marks already-finished work, since its notification is what an explicit background call needs. 3 files, +90/-8. job and shell tests pass, typecheck clean. |
There was a problem hiding this comment.
The race is fixed. I reran the same probe on the new head: the timeout branch was forced to wait for the job to settle before the move, then a foreground sleep 0.3; echo hi; exit 3 ran with timeout: 10. It returns the command's own output and exit: 3 with status: "completed". No notice is posted, nothing is left pending, and the job is consumed. On base the same call reported "moved to the background".
The tests touching job, tool-shell, session-execution and shell-restart pass (171/0), and typecheck:tsc is clean.
Public API: unchanged. Job.background gets an optional { reportSettled } argument, which is internal to core.
The opt-in keeps the background: true path and other callers on their existing behaviour, so the earlier point about the background: true branch is addressed.
Not for this PR: Code Mode's timeout path (jobs.block(...).pipe(Effect.timeoutOption(BLOCK_TIMEOUT)), then jobs.background(id) in packages/core/src/codemode/job.ts) follows the same pattern and probably has the same race. I didn't run it.
Issue for this PR
Closes #63747
Type of change
What does this PR do?
A foreground shell call that reaches its timeout moves the command to the background. If the command exits between the timeout firing and the move, the call still reported "moved to the background" and the real result was lost.
The cause is in
Job.background: it skips a settled job only when the job has norecovery. Shell jobs are always started withrecovery, so a command that settled in that window was still marked as backgrounded. The call then registered a completion notice, but the settlement had already written its own row, so no notice ever arrived.The fix is to stop moving work that has already settled.
backgroundnow returns the settled snapshot withsettled: trueinstead of marking it, and the shell tool reads that as a finished command: it returns the command's own output and exit code, and leaves nothing pending.I took the fix in
Job.backgroundrather than only in the shell tool so the race is closed for every caller, andbackgroundAllalready skipped non-running jobs.How did you verify your code works?
I added
packages/core/test/job-background-settled.test.ts, which starts a shell job, settles it, then callsbackgroundand asserts the call seesstatus: "completed"and thatpendingBackgroundstays empty. It fails ondevand passes with this change.Three existing tests seeded a settled job through
background, which only worked because of the old behaviour. I updated them to background while the job is still running, which is the real sequence.bun run typecheckinpackages/core: clean.bun testfor job, tool-shell, session-execution, shell-restart and the new test: 167 pass, 0 fail.packages/coresuite: no new failures againstdev. The 9 that still fail there (config home discovery, flock on unwritable roots) fail the same way ondevin this environment.Screenshots / recordings
Not a UI change.
Checklist