Skip to content

upgrade: stop the background service before replacing the binary on Windows - #103

Merged
sagnik11 merged 2 commits into
mainfrom
autter/windows-upgrade-in-use
Nov 13, 2026
Merged

sagnik11 merged 2 commits into
mainfrom
autter/windows-upgrade-in-use

Conversation

@sagnik11

@sagnik11 sagnik11 commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

A tester reported that on Windows the npm upgrade fails quietly while the background service runs: EPERM, the old version stays, and it exits 0. Running autter bg shutdown first fixed it.

Cause

Windows won't replace or delete an .exe that a running process has open, and the background service runs ~/.autter/bin/autter.exe. Each upgrade path handled that differently:

  • npm (npm/install.js postinstall): renameSync(tmp, dest) failed with EPERM, the fallback rmSync(dest) failed the same way, and main() treated the error as transient. It printed a warning and returned, so npm exited 0 and the old binary stayed. The launcher (bin/autter.js) then tried again on every run, failed, and ran the old binary.
  • autter upgrade (Windows): it stopped the service, started install.ps1 in a detached PowerShell and exited 0 straight away. The PowerShell wrapper also caught the installer's error and exited 0, so a failure only showed up in ~/.autter/upgrade-logs. The service was not started again afterwards.
  • install.ps1: it stopped the service, waited up to 300 s for the file (killing every autter/git process from that folder after 20 s), and then ran Move-Item. It failed loudly on timeout, but it did not start the service again unless the service had started the upgrade itself.

What changes

All three paths now do the same thing on Windows:

  1. Stop the service the way autter bg shutdown does, and wait briefly for it to release the file.
  2. If the file is still held, rename the running .exe aside to autter.exe.old (Windows allows this; autter.exe.<pid>.old if an older one is still running) and move the new binary in. If that fails, move the old one back.
  3. Start the service again on the binary now in place, but only if it was running before.
  4. Delete .old files that nothing runs any more. This happens straight away when possible, otherwise on the next install/upgrade or when the service next starts (run_daemon).

If the swap still fails, the install exits non-zero. The message says the file is in use and gives the fix: run autter bg shutdown, then try again. The previous version stays installed and working. If the new binary doesn't run (--version check), the previous one is put back.

  • npm/install.js: replaceBinary / finishInstall / installDownloadedBinary, with the fs and process calls injectable. A SwapError now fails the postinstall (exit 1) instead of being deferred to the launcher. Other download errors are still deferred, as before.
  • src/commands/binary_swap.rs (new): run_installer_with_binary_aside over a SwapHost trait. On Windows, autter upgrade now runs install.ps1 attached (-File, no transcript wrapper), waits for it, exits with its result and refreshes agent hooks the way Unix already did. The service's own silent self-upgrade stays detached, but its wrapper now exits 1 on failure.
  • install.ps1: Stop-ServiceForReplace, Install-ExecutableFile (rename-aside with rollback, also used for the git.exe shim), Restore-PreviousExecutable and Remove-LeftoverExecutables. Start-DaemonIfRequested now also runs when the installer stopped a running service. It no longer force-kills arbitrary autter/git processes, since rename-aside makes that unnecessary.
  • macOS/Linux: no change, except that the npm postinstall now fails instead of passing when it cannot move the new binary into place.

Tests

  • Rust unit tests (binary_swap): the swap logic against a fake Windows file system. Covered: rename-aside then restart on the new binary; a service that wasn't running is not started; a file in use gives a non-zero error with the fix and leaves the old binary; a failed installer puts the old binary back; an installer that left no binary; a failure after the new binary is in place; a numbered aside name; leftover detection. A Windows-only test does the same on the real file system with a running copy of PING.EXE and a file held without delete sharing.
  • Node tests (npm/install.test.js): the same cases for the npm swap. The postinstall rejects on SwapError and still defers network errors. A real-filesystem ensureBinary run with a Windows-style EPERM injected.
  • scripts/windows-e2e.ps1, new upgrade section on real Windows: the npm swap over autter.exe while the service runs it (stopped, swapped, running again as a new process). A swap over a running exe (renamed aside, leftover removed after it exits). A swap over a held file (non-zero exit, names the cause and the fix, old file unchanged). install.ps1 from the local build over the running service (stops it, installs, starts it again).
  • tests/windows_install_script.rs (Windows-only, not run in CI): static checks updated.
  • Not covered on real Windows: autter upgrade end to end, because it needs a published release and the API. Its Windows logic is covered by the binary_swap unit tests, including the real-filesystem one.

🤖 Generated with Claude Code

sagnik11 and others added 2 commits November 14, 2026 00:42
…indows

Windows won't replace an .exe a running process has open, so upgrading
while the background service ran failed with EPERM, left the old version
in place and still exited 0. The npm install and autter upgrade now stop
the service first, swap the binary, restart the service if it was
running, and exit non-zero with a clear message if the swap still fails.

Pushed for CI without the full local test run (owner's request).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- binary_swap: a Windows-only unit test on the real file system renames a
  running exe (a copy of PING.EXE) aside, replaces it and removes the
  leftover once the process exits; a file held without delete sharing
  fails with the in-use message and stays in place.
- windows-e2e.ps1: new `upgrade` section. The npm bootstrapper's swap runs
  over autter.exe while the background service runs it (service stopped,
  swapped, started again as a new process); over a running exe (renamed
  aside); over a held file (non-zero exit naming the cause and the fix, old
  file kept); and install.ps1 runs over the running service.
- Messages suggest `autter bg restart`, the project's canonical command
  (fixes the lib test that forbids `autter bg start` in user-facing text).
- CHANGELOG [Unreleased] entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sagnik11
sagnik11 merged commit 5648ffe into main Nov 13, 2026
8 checks passed
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.

1 participant