Skip to content

fix: clean up stale .old binary at startup instead of via detached process#261

Merged
rhuanbarreto merged 1 commit into
mainfrom
fix/cleanup-stale-binary-on-startup
May 3, 2026
Merged

fix: clean up stale .old binary at startup instead of via detached process#261
rhuanbarreto merged 1 commit into
mainfrom
fix/cleanup-stale-binary-on-startup

Conversation

@rhuanbarreto

Copy link
Copy Markdown
Contributor

Summary

  • Replaces the unreliable detached cmd /c ping + del cleanup in replaceBinary() with cleanupStaleBinary(), a fire-and-forget async unlink() called at CLI startup
  • The .old file is guaranteed to be unlocked by the next CLI invocation, making cleanup reliable
  • Cleanup is platform-agnostic (resolves binary name via getArtifactInfo()), even though only Windows currently creates .old files
  • Keeps the sync unlinkSync in replaceBinary() as defense-in-depth for leftover .old from previous upgrades

Fixes: orphaned archgate.exe.old persisting in ~/.archgate/bin/ after upgrade on Windows.

Test plan

  • bun run validate passes (lint, typecheck, format, 690 tests, 23/23 ADR rules, build)
  • New test: replaceBinary creates .old file on Windows
  • New test: cleanupStaleBinary deletes .old when present
  • New test: cleanupStaleBinary resolves silently when no .old file exists

…ocess

On Windows, `replaceBinary()` renames the running exe to `.old` because
the OS file-locks it. The previous approach spawned a detached
`cmd /c ping -n 2 ... & del` process to clean up after exit, but this
was unreliable (process may not spawn, `del` may fail silently, timing
races), leaving orphaned `.old` files in ~/.archgate/bin/.

Replace the detached cleanup with `cleanupStaleBinary()`, called once
at CLI startup as a fire-and-forget async `unlink()`. The file is
guaranteed to be unlocked by then. The cleanup is platform-agnostic
(uses `getArtifactInfo()` to resolve the binary name) so it works on
any supported platform, even though only Windows currently creates
`.old` files.

The sync `unlinkSync` in `replaceBinary()` is kept as defense-in-depth
for leftover `.old` files from previous upgrades.

Signed-off-by: Rhuan Barreto <rhuan@barreto.work>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying archgate-cli with  Cloudflare Pages  Cloudflare Pages

Latest commit: ac4336e
Status: ✅  Deploy successful!
Preview URL: https://357baee7.archgate-cli.pages.dev
Branch Preview URL: https://fix-cleanup-stale-binary-on.archgate-cli.pages.dev

View logs

@rhuanbarreto rhuanbarreto merged commit 621bb6b into main May 3, 2026
10 checks passed
@rhuanbarreto rhuanbarreto deleted the fix/cleanup-stale-binary-on-startup branch May 3, 2026 23:13
@archgatebot archgatebot Bot mentioned this pull request May 3, 2026
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