Fix command cleanup and first-use races in `BubblewrapWorkspace`

Merged
MP
1mo ago
+258-17

Why we make these changes

Foreground descendants can survive timeout or cancellation, and concurrent first calls can race during login-path setup. Remove --new-session so the backend's process-group cleanup reaches the command's children, and make the login-path setup script accept a path another workspace created between its check and its creation (each run of BubblewrapSandbox builds its own workspace, so a per-instance lock alone cannot serialize them; the lock stays as an optimisation within one workspace). Without --new-session, the wrapped workspace must run commands without a controlling terminal; the class docstring, README, and docs page now state this requirement and the TIOCSTI risk it avoids. The docs also say which backends stop a command's descendants.

This PR now contains everything from #10763 and #10778, so those two are redundant.

New public surface

none

User-visible behavior

  • Before: sleep 3600 & wait can leave its child running after timeout or cancellation. After: with LocalWorkspaceBackend or SSHWorkspaceBackend, foreground descendants are stopped; E2B and Modal stop only the leader when the image lacks setsid. Deliberately detached jobs still survive.
  • Before: concurrent first calls, on one workspace or on separate runs against the same home, can fail with WorkspaceUnavailableError while creating missing login files. After: both proceed; a path that turns out to be a symlink after a failed creation is still refused.

Verification

  • Login paths created by another workspace meanwhile and a parent symlinked meanwhile: both fail with the script change reverted (cannot overwrite existing file or File exists); files are created with printf because dash exits on a failed : redirection, which CI caught; the second also fails without the post-creation symlink check.
  • Concurrent first calls on one workspace run setup once.
  • --new-session removal is guarded in CI by the fake-bwrap argv tests, including an explicit '--new-session' not in assertion in test_commands_run_in_bwrap_on_the_wrapped_host.
  • The real process-group test, TestRealBubblewrap::test_stopping_a_command_kills_its_descendants, is skipped in CI because CI hosts have no bwrap. For commit 1cbe7b3 it was run on a Linux host with bwrap (passes; fails with --new-session restored), and timeouts were checked over real SSH there; it was not rerun for the later commits, which do not touch the launcher.
  • Locally (macOS): tests/harness/bubblewrap_sandbox 100 passed, 51 skipped; Ruff and Pyright clean; docs parity 8 passed.

What changes for existing users

Foreground commands respect backend timeout/cancellation cleanup, and concurrent first calls no longer race to create login files. Custom wrapped backends must not attach a controlling terminal.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. N/A: no public API or supported compatibility change.
  • PR title is fit for the release changelog.