Skip to content

Purge stale tmp dirs at startup - #2176

Merged
stefanprodan merged 2 commits into
fluxcd:mainfrom
andrewb3000:fix-purge-stale-tmp
Oct 22, 2026
Merged

stefanprodan merged 2 commits into
fluxcd:mainfrom
andrewb3000:fix-purge-stale-tmp

Conversation

@andrewb3000

Copy link
Copy Markdown
Contributor

Fixes #1793

When source-controller exits without running its deferred cleanup, after losing its leader lease or being OOM killed, the temporary directories of the reconciles in flight stay behind. On an emptyDir /tmp they survive container restarts, so a controller that restarts often slowly fills the node's disk.

This is the source-controller counterpart of fluxcd/kustomize-controller#1729.

kustomize-controller's fix matches a fixed name prefix. That doesn't carry over here: temp paths are named after the object (<kind>-<namespace>-<name>-*, or Kind-... in the Bucket reconciler), and the Helm code adds chart-index-*, chart-build-* and *.tgz paths of its own. So instead:

  • At startup the controller creates $TMPDIR/source-controller and points TMPDIR at it, so every temp path, including the ones created by the Helm and Git libraries, lands in one directory.
  • Whatever a previous process left there is renamed aside first (source-controller.stale-<ts>) and removed in the background, with a two minute budget, so startup isn't delayed. A purge cut short by a later crash is picked up on the next start.

Only the controller's own subdirectory is touched, so this is safe when TMPDIR is shared, for example when running the binary locally.

When the controller exits without running its deferred cleanup, after
losing its leader lease or being OOM killed, the temporary directories of
the reconciles in flight stay behind. They survive container restarts on
an emptyDir volume, so a controller that restarts often fills the node.

The controller now keeps its temporary files in a directory of its own
under the system temp dir and points TMPDIR at it, so the files created by
the Helm and Git libraries land there too. At startup, whatever a previous
process left in that directory is moved aside and removed in the
background, the same way kustomize-controller purges its tmp dirs.

Signed-off-by: Andrii Bryndzak <abryndzak@datum.net>
Log tmp dir setup failures and keep the default TMPDIR instead of
exiting. Empty stale roots one entry at a time so the purge timeout
applies between entries.

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
@stefanprodan stefanprodan changed the title fix: purge stale tmp dirs at startup Purge stale tmp dirs at startup Oct 22, 2026

@stefanprodan stefanprodan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks @andrewb3000

@stefanprodan
stefanprodan merged commit 8b56e12 into fluxcd:main Oct 22, 2026
7 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.

Problem with /tmp file

2 participants