Practical technical guides / Recent development: Claude Code 2.1.330 (20 November 2026)

Make Agent Output Failures Reach the Process Exit Status

Claude Code 2.1.330 fixed claude -p exiting 0 on a full disk. Write-flush-close-rename before the done marker, and test the exit status with injected failures.

By Sandeep Belgavi · Published 2026-11-21

Claude Code 2.1.330 fixed claude -p exiting 0 on a full disk. Write-flush-close-rename before the done marker, and test the exit status with injected failures.

One line in a release note

Claude Code 2.1.330, released on 20 November 2026 (UTC), includes this fix in its notes: claude -p exited 0 with no message when its output went to a file on a full disk, and it now prints an error and exits 1, though a full NFS export may go unnoticed. The same release also fixes incomplete backups of ~/.claude.json being kept, and displacing good ones, when the disk was full.

Those are two small fixes in a long list, but they describe a failure that every agent command can have. Agents increasingly run unattended: in continuous integration, in cron jobs, behind schedulers that poll for a result file. In those settings the exit status and the files left behind are the whole interface. A command that loses its output and still exits 0 tells the scheduler the work is done. This guide shows how that happens with ordinary buffered I/O, measures three ways of writing output against a simulated full disk, and gives a procedure that makes failures reach the exit status. It does not test Claude Code itself. The release note is the prompt for the topic, and the experiment is original.

Where the error goes

Buffered writes are the reason output failures hide. Python's io documentation describes the layering: a text stream on top of a buffered stream on top of a raw stream. A call to write on the text layer usually only copies bytes into a buffer. The bytes reach the operating system when the buffer fills, when you call flush, or when the file is closed. A full disk is reported to whichever call actually hands bytes to the operating system. With a large write that can be write itself. With a small final write it is often flush or close, which run in a finally block or at interpreter exit, where errors are easy to swallow.

Standard output has the same property, with one more twist. Python's sys.exit documentation says that if an error occurs in the cleanup after the interpreter has caught SystemExit, such as an error flushing buffered data in the standard streams, the exit status is changed to 120. The companion example measures this on Windows: a child process writes 100 characters to standard output, the parent closes its end of the pipe, and the child's exit flush fails with OSError: [Errno 22] Invalid argument. The exit status is 120. That is the interpreter doing the right thing, but only if your process reaches interpreter exit. A handler that catches the error earlier and calls sys.exit(0) overrides it.

The experiment

The example writes a JSON summary of 200 synthetic refund cases for the fictional Belgavi's Book Store, 16,846 bytes, and then a DONE marker that a scheduler would poll. A full disk is simulated by a raw stream that accepts a byte budget and then raises OSError(ENOSPC). The standard io.BufferedWriter and io.TextIOWrapper sit on top, as with a real file, so the error surfaces wherever it would in practice. Each writer runs as a real child process, so the exit status is the operating system's, not a return value.

Three writers:

  • swallow catches every exception around the write and close, writes the marker, and exits 0.
  • marker_first writes the summary, writes the marker, then closes the summary file.
  • checked writes to a temporary name, flushes, closes, renames the file into place, and only then writes the marker. Any OSError removes the temporary file, prints an error to standard error and exits 1.
def checked(out_dir, budget):
    tmp = out_dir / 'summary.json.tmp'
    try:
        f = open_output(tmp, budget)
        try:
            f.write(payload())
            f.flush()
        finally:
            f.close()
        os.replace(tmp, out_dir / 'summary.json')
    except OSError as e:
        tmp.unlink(missing_ok=True)
        print(f'error: summary not written: {e}', file=sys.stderr)
        return 1
    (out_dir / 'DONE').write_text('ok\n')
    return 0

Measured results

With plenty of space, all three writers exit 0, write the marker and produce a valid summary. With a budget of 4,096 bytes, the large write fails immediately:

  • swallow: exit 0, marker present, summary not valid JSON.
  • marker_first: exit 1, no marker, because the failure happened at write, before the marker.
  • checked: exit 1, no marker, error message on standard error.

The interesting case is a budget of 16,746 bytes, 100 short of the payload. Here the large write succeeds, because the buffered layer accepts the remaining 100 bytes into its buffer. The failure arrives at close:

  • swallow: exit 0, marker present, summary truncated.
  • marker_first: exit 1, but the marker is already present. A scheduler that polls for the marker sees success. One that checks the exit status sees failure. Two monitors disagree about the same run.
  • checked: exit 1, no marker, no file at the final name.

marker_first is the pattern to look for in review, because it looks correct. It does not swallow anything. It just announces completion before the last step that can fail.

A procedure for agent commands

  1. Write each output to a temporary name in the destination directory.
  2. Flush and close it, and treat any exception from either call as a failure of the command.
  3. If the output must survive power loss, call os.fsync on the file before closing, and on the directory after the rename where the platform supports it. Without that, a rename can be visible before the data is durable.
  4. Rename into place with os.replace, which replaces an existing file in one step.
  5. Write the completion marker last, using the same procedure if the marker carries data.
  6. Map every output failure to a nonzero exit status and a one-line error on standard error that names the file. Reserve exit 0 for "every promised artifact exists and is complete".
  7. Flush standard output yourself before exit if your output contract includes it, so the failure is reported by your code with a clear message rather than by interpreter cleanup with status 120.

The release note's caveat about NFS deserves attention too. On some network filesystems, a write and even a close can succeed locally while the server later fails to store the data. An exit status cannot report a failure that the operating system has not reported yet. If correctness matters, verify the output after writing it: read it back, check its size and parse it, as the example's test harness does.

What stays deterministic

The model's contribution is the content. Whether the content was stored is a property of the filesystem calls, and the command should decide its exit status from those calls alone. Do not let an agent decide whether a write "probably worked", and do not ask a model to summarise a log to decide success. The scheduler should see the exit status, the marker and the files, and those should agree.

Failure modes to test

  • A full disk at write, at flush and at close, using a fake raw stream like the example's. Assert exit status, marker and file state for each.
  • A read-only destination directory, which fails at open or rename rather than at write.
  • Standard output closed by the consumer. Assert a nonzero status, and decide whether your command should treat a closed pipe as an error or as a request to stop.
  • A crash between rename and marker. Assert that a rerun is safe, because the output exists without its marker.
  • An existing marker from a previous run. Remove it at start, or include a run identifier in it, so a stale marker cannot vouch for a failed run.

When you can skip this

A command whose only output is a stream that a person reads interactively does not need temporary files and markers. If the terminal closes, nobody is waiting for the result. The procedure matters when something else acts on your exit status or polls for your files, which is the normal case for an agent running unattended.

Verification checklist

  • Every output file goes through write, flush, close and rename before any completion signal.
  • Exit 0 means every promised output exists and parses.
  • Injected failures at write, flush and close each produce a nonzero exit status and no marker, as in the example's 16,746-byte case.
  • Monitors read the exit status and the marker together, and alert when they disagree.

Run the example yourself

The simulated full disk, the three writers run as child processes and the exit-status probe, with five tests. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Primary references and authorship

Sources read 21 November 2026. Original explanation, measurements and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.

Related on this site: