codex icon indicating copy to clipboard operation
codex copied to clipboard

Plugin refresh error leaves orphaned marketplace-plugin-source clones and .tmp/git-* dirs (second unawaited refresh wave)

Open Kenmege opened this issue 1 month ago • 5 comments

What version of Codex is running?

codex-cli 0.160.1 (also reproduced on 0.163.0), macOS (arm64)

What happened?

$CODEX_HOME/plugins/.marketplace-plugin-source-staging/ fills with complete git clones of plugin sources (marketplace-plugin-source-XXXXXX) that are never deleted, each paired with an orphaned $CODEX_HOME/.tmp/git-XXXXXX directory. On one machine this reached ~860 directories / ~3 GB in four days, growing by roughly 100 directories per hour.

Root cause

  1. plugin/list with forceRefetch: true runs the non-curated plugin cache refresh synchronously and cleans up its own staging clones.
  2. If any configured plugin fails to refresh (in our case a local-marketplace plugin whose source path was a dangling symlink), refreshed is false and state.last_refreshed is never set (codex-rs/core-plugins/src/manager.rs, the if refreshed { state.last_refreshed = Some(request.clone()); } guard, ~L3555 on current main).
  3. Just before returning, plugin/list calls maybe_start_plugin_list_background_tasks_for_config (app-server/src/request_processors/plugins.rs). Because last_refreshed is still unset, it starts a second, unawaited refresh on a detached thread that clones every git-sourced plugin again.
  4. A client that exits the app-server shortly afterwards (for example a one-shot initialize -> plugin/list -> skills/list -> exit caller) orphans the in-flight git clone (it is reparented to PID 1). The clone completes into the staging directory, and neither it nor the GIT_DIR temp directory is ever removed, because the detached thread's TempDir guards never drop.

Related: the non-curated refresh also clones plugins configured with enabled = false. configured_non_curated_plugin_ids filters only by marketplace (core-plugins/src/loader.rs, ~L583), so disabled git plugins are cloned on every refresh.

Steps to reproduce

  1. Configure one local-marketplace plugin whose source path does not exist (dangling symlink), plus a few git-sourced plugins.
  2. Repeatedly start codex app-server --stdio, send initialize, plugin/list with {"forceRefetch": true}, then skills/list, and terminate the process as soon as skills/list returns.
  3. Watch $CODEX_HOME/plugins/.marketplace-plugin-source-staging/ and $CODEX_HOME/.tmp/git-*: clones started after plugin/list returns are orphaned and accumulate.

Evidence

  • A/B with only the broken plugin entry changed (3 runs per arm): 13 clones per refresh with the failing entry, 6 without it. The second wave disappears.
  • 10-minute live observation: 149 clones, 18 left behind before the change; 36 clones, 0 left behind after removing the failing entry.

Expected behavior

One failing plugin should not cause every refresh to re-run in the background, and no clone or temp directory should outlive the app-server process.

Suggested fixes

  • Record refresh completion per plugin (or set last_refreshed together with the error set) so a single failing plugin does not trigger a duplicate background wave.
  • Join or cancel background refresh threads on shutdown, or sweep stale marketplace-plugin-source-* and .tmp/git-* directories older than a few minutes, as is already done for curated clones (CURATED_PLUGINS_STALE_TEMP_DIR_MAX_AGE).
  • Skip plugins with enabled = false in the non-curated refresh.

Kenmege avatar Oct 18 '26 14:10 Kenmege

A regression test could queue two forceRefetch calls while one plugin is broken, then wait for both refresh workers to settle before checking that staging and .tmp contain no orphan clones. I would also assert that the healthy plugin is not cloned again in a duplicate background wave and that the original failure remains visible to the caller. I work with BOMTOBE/Orke; I have not reproduced this locally, so this is a test suggestion based on the report.

bomtobe-hgkim avatar Oct 18 '26 14:10 bomtobe-hgkim

Can confirm the .tmp/git-* half of this leak on Linux/WSL2 at an extreme scale: on our machine, ~/.codex/.tmp/ had accumulated 352,950 orphaned git-XXXXXX directories (each an empty bare-git stub with HEAD, objects/info, objects/pack, refs/heads, refs/tags).

Two operational consequences worth highlighting:

  1. Inode exhaustion and directory-inode bloat: 352,950 git-* directories consumed over 1.4 million inodes and bloated the ~/.codex/.tmp directory inode itself to 12 MiB (12,054,528 bytes). Once an ext4 directory inode grows that large, every readdir() / ls / stat walk inside ~/.codex/.tmp slows to a crawl, and even after unlinking the 352k subdirectories, ext4 does not shrink the 12 MiB directory entry table unless .tmp itself is atomically recreated.
  2. Safe automated cleanup + directory compaction: Until the upstream TempDir lifecycle is fixed, we built an open-source guard that sweeps empty ~/.codex/.tmp/git-* stubs older than 15 minutes (after verifying no process has an open FD or cwd under /proc/*/fd and /proc/*/cwd and no packfiles/loose objects exist inside objects/) and then atomically compacts ~/.codex/.tmp under flock when its directory inode exceeds 1 MiB: https://github.com/simfor99/codex-hdd-guard (MIT, Python 3.10+ stdlib only, zero dependencies).

On our live run it cleared 352,902 stale git-* directories and shrank the .tmp directory inode from 12 MiB back down to 1008 KiB.

simfor99 avatar Nov 03 '26 15:11 simfor99

I reproduced the duplicate refresh wave on tagged rust-v0.166.0 in an offline Linux container using only synthetic local repositories.

The focused codex-core-plugins test has two arms. Both configure a healthy Git-backed plugin. The second arm also configures a local plugin whose source is a dangling symlink. For each arm, it calls refresh_non_curated_plugin_cache_for_context, then maybe_start_non_curated_plugin_cache_refresh, waits for any scheduled background worker to settle, and counts git clone invocations with a PATH wrapper that immediately executes real Git.

Fixture Git clones
Healthy Git plugin only 1
Git plugin plus broken local plugin 2

The broken arm leaves last_refreshed unset after the explicit refresh, so the subsequent background check clones the healthy Git source again. The focused test passed (1 passed; 0 failed). This confirms the duplicate-wave mechanism on a newer release than the original report.

This component test did not start or stop codex app-server; it waited for the manager's refresh workers to finish. It does not independently reproduce the orphaned child or directory accumulation after app-server exit. A minimal test patch and sanitized log are available if useful.

TheOutlandsDev avatar Nov 04 '26 11:11 TheOutlandsDev

Follow-up to my earlier release test: I also reproduced the app-server exit portion on tagged rust-v0.166.0 in an offline Linux container with a disposable Codex home and local Git repository.

The fixture configured one healthy Git-backed plugin and one local plugin with a dangling-symlink source. I started the tagged codex-app-server binary with plugin startup tasks disabled, sent initialize and plugin/list with forceRefetch: true, and used an executable PATH wrapper to record Git commands and pause only the second git clone. The wrapper recorded two clones: the first completed during the forced refresh, and the second began in the background after the broken plugin caused a refresh error.

While the second clone was paused, I closed app-server stdin. App-server exited with code 0 within six seconds. Immediately afterward, the second Git wrapper PID was still alive, and both a plugins/.marketplace-plugin-source-staging/marketplace-plugin-source-* directory and a .tmp/git-* directory remained in the temporary Codex home. I then stopped the synthetic process group and removed the fixture.

This confirms the process and directory state at app-server exit on this release. The bounded test did not let the orphan finish its clone or measure accumulation across repeated launches. No live service traffic was sent. I can provide the small Python harness and sanitized output if useful.

TheOutlandsDev avatar Nov 04 '26 12:11 TheOutlandsDev

Still happening on codex-cli 0.172.0 (Linux x86_64, Ubuntu 26.04).

~/.codex/plugins/.marketplace-plugin-source-staging/ held 241 marketplace-plugin-source-XXXXXX clones, about 270 MB each (40 GB in total), created over 8 days at 12–85 a day. ~/.codex/.tmp/marketplaces held another 9.6 GB. None of the staging clones was open by a running process when we removed them.

We haven't checked whether one of our configured plugins fails to refresh, so I can't confirm the trigger described above, only that the leak is still present in the current release. Our workaround is a weekly job that deletes staging folders untouched for a day and not open by any process.

RemingtonDev avatar Nov 30 '26 09:11 RemingtonDev