Skip to content

shell_exec: preflight direct spawn failures - #3015

Merged
max-sixty merged 1 commit into
max-sixty:mainfrom
Xeonacid:fix-qemu-user-posix-spawn-preflight
Jun 8, 2026
Merged

max-sixty merged 1 commit into
max-sixty:mainfrom
Xeonacid:fix-qemu-user-posix-spawn-preflight

Conversation

@Xeonacid

@Xeonacid Xeonacid commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Rust's Command API normally reports execve and current_dir setup failures synchronously when spawn/output fails. Under qemu-user, glibc's posix_spawn path can use CLONE_VM|CLONE_VFORK and expect the child to write those errors back to the parent, but qemu-user does not preserve that shared-stack error channel. The parent can then observe a successful spawn and only see the child exit with status 127.

This fixes the riscv64 qemu-user failures in shell_exec::tests::test_cmd_stream_spawn_failure_is_errored and git::repository::tests::repo_path_error_when_is_bare_fails, both of which expect missing executable or invalid current_dir failures to surface as spawn errors instead of ordinary child exit status 127.

This is a portability issue in the child-side exec failure reporting path, not just a qemu-user quirk. Not all posix_spawn() implementations use errno to report execv() failures from the child process, such as qemu-user, OpenBSD and HPPA.

Check the pieces that worktrunk already knows before spawning: explicit current_dir values must name directories, and direct commands with absolute program paths must already name executable files. stream() keeps the existing 'Failed to execute command' wrapping and trace behavior, while run() and pipe_into() continue to return io::Error directly.

Add regression tests for direct run() failures and update the existing stream() test to describe the externally visible behavior instead of depending on cmd.spawn() being the failing call.

Ref:
https://gitlab.com/qemu-project/qemu/-/work_items/140
https://lists.nongnu.org/archive/html/qemu-devel/2025-03/msg04706.html

@worktrunk-bot worktrunk-bot 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.

Nice portability fix — moving the missing-binary / bad-current-dir detection ahead of spawn() is the right call for the platforms (qemu-user, OpenBSD, HPPA) where the child-side exec-failure errno channel is lossy, and the conservative mode & 0o111 check correctly only rejects when no exec bit is set rather than guessing at the effective-uid permission outcome. Tests pass locally, including the two qemu-affected ones.

One asymmetry worth tightening: stream() records the preflight failure to the [wt-trace] stream via trace_log.errored(&e), but run() and pipe_into() return through ? without emitting a trace record. Before this change a missing-binary / bad-dir failure in run() flowed through cmd.output() → trace_log.record_result() → errored, so it appeared in -vv output (and, for external() commands, the command log). With the preflight catching it first, that command now vanishes from the trace — and the bad-current_dir case is exactly the failure shape wt -vv diagnostics exist to surface (it's the same path repo_path_error_when_is_bare_fails exercises). Suggestions inline mirror the stream() handling.

Comment thread src/shell_exec.rs Resolved
Comment thread src/shell_exec.rs Resolved
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Heads up: codecov/patch is failing (64.91% on this diff). It's marked non-required in GitHub but worktrunk treats it as a merge gate, so it's worth closing before this lands.

The uncovered lines are all the "exists but wrong type / not executable" branches. Both new tests use /no/such/... paths, which fail at the initial std::fs::metadata(...)? (NotFound) and return before the type/permission checks ever run:

  • ensure_executable_path — the !metadata.is_file() branch, the !is_executable(...) branch, and is_executable() itself (never called, since no test reaches it with an existing file)
  • check_spawn_preconditions — the NotADirectory branch (current_dir exists but isn't a directory)

Three small regression tests would close the gap and, more usefully, pin the behavior this PR is really about (turning these into spawn-time errors rather than exit-127):

  • current_dir pointing at a regular file → error
  • an absolute program path that resolves to a directory → error
  • an absolute program path that exists but lacks the exec bit (tempfile::NamedTempFile + set_permissions(PermissionsExt::from_mode(0o644)), unix-only) → error

Want me to push those tests to your branch?

Rust's Command API normally reports execve and current_dir setup failures synchronously when spawn/output fails. Under qemu-user, glibc's posix_spawn path can use CLONE_VM|CLONE_VFORK and expect the child to write those errors back to the parent, but qemu-user does not preserve that shared-stack error channel. The parent can then observe a successful spawn and only see the child exit with status 127.

This fixes the riscv64 qemu-user failures in shell_exec::tests::test_cmd_stream_spawn_failure_is_errored and git::repository::tests::repo_path_error_when_is_bare_fails, both of which expect missing executable or invalid current_dir failures to surface as spawn errors instead of ordinary child exit status 127.

This is a portability issue in the child-side exec failure reporting path, not just a qemu-user quirk. Not all posix_spawn() implementations use errno to report execv() failures from the child process, such as qemu-user, OpenBSD and HPPA.

Check the pieces that worktrunk already knows before spawning: explicit current_dir values must name directories, and direct commands with absolute program paths must already name executable files. All entry points keep their trace behavior for preflight failures: stream() keeps the existing 'Failed to execute command' wrapping, run() records the failed command and external log entry before returning the io::Error, and pipe_into() records the failing side before returning the io::Error.

Add regression tests for missing paths, existing non-directory current_dir values, directory program paths, non-executable program paths, and update the existing stream() test to describe the externally visible behavior instead of depending on cmd.spawn() being the failing call.
@max-sixty
max-sixty merged commit b1f660f into max-sixty:main Jun 8, 2026
34 of 35 checks passed
@Xeonacid
Xeonacid deleted the fix-qemu-user-posix-spawn-preflight branch June 8, 2026 07:03
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.

3 participants