Skip to content

fix(cli): sweep stale Bun-extracted temp libraries at startup#568

Open
WakaTaira wants to merge 1 commit into
modem-dev:mainfrom
WakaTaira:fix/tmp-artifact-sweep
Open

fix(cli): sweep stale Bun-extracted temp libraries at startup#568
WakaTaira wants to merge 1 commit into
modem-dev:mainfrom
WakaTaira:fix/tmp-artifact-sweep

Conversation

@WakaTaira

Copy link
Copy Markdown

What

Bun single-file executables extract their embedded native libraries into the OS temp directory under a randomized hidden name (.{16hex}-{8hex}.so|dylib|dll) on every launch, and never reuse or remove them (oven-sh/bun#30962). For hunk this means every invocation leaks one copy of the OpenTUI native library — #556 documented 81,653 files (~190 GB) in three weeks via the lazygit pager integration; I hit a full 16 GB tmpfs overnight on Linux with an agent polling hunk session every 2 s.

This PR adds a best-effort startup sweep that reaps hunk's stale leaked artifacts until Bun's extraction dedupe (oven-sh/bun#29587) ships — at which point this module can be deleted outright.

How

  • New src/core/tmpArtifactSweep.ts (DI-friendly, colocated tests): removes hidden .{16hex}-{8hex}.(so|dylib|dll) regular files in the temp directory root, owned by the current user and older than one hour.
  • Rate-limited to one directory scan per hour via a stamp file, claimed before the scan so concurrent starts don't all readdir.
  • Hardened for shared /tmp: the stamp must be a regular file owned by the current user — a symlinked or foreign-owned stamp aborts the sweep; first creation is an exclusive write.
  • Wired in main(): the sweep starts before argument parsing, and short-lived commands await it right before process.exit (which would otherwise drop the pending work). The app and daemon paths never block on it.
  • All errors swallowed, no output. Opt out with HUNK_DISABLE_TMP_SWEEP=1.

Verification

  • typecheck / test / lint / format:check all pass (9 new unit tests).
  • End-to-end on Linux: a compiled hunk binary leaks exactly one new artifact per --help run (reproducing the bug); with this patch it removes planted stale artifacts, keeps fresh ones, honors the opt-out, and respects the rate-limit stamp.

Refs #556.

Bun single-file executables extract their embedded native libraries into
the OS temp directory under a randomized hidden name on every launch and
never remove or reuse them (oven-sh/bun#30962). Repeated invocations —
e.g. hunk as a git pager or agents polling `hunk session` — can leak
gigabytes per day (modem-dev#556).

Add a best-effort startup sweep that removes stale copies: hidden
`.{16hex}-{8hex}.(so|dylib|dll)` files in the temp directory root, owned
by the current user and older than one hour. The sweep is rate-limited to
one directory scan per hour via a stamp file, aborts on a suspicious
stamp (symlink or foreign owner) to stay safe on shared /tmp, swallows
all errors, and can be disabled with HUNK_DISABLE_TMP_SWEEP=1. Interim
mitigation until Bun's extraction dedupe (oven-sh/bun#29587) ships, at
which point this module can be deleted.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015eTSMVABBYeA6ona5khgKC
@greptile-apps

greptile-apps Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

PR author is not in the allowed authors list.

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