Skip to content

fix(upload): delete expired uploads on a timer, not only after uploads - #651

Merged
madara88645 merged 2 commits into
mainfrom
fix/scheduled-upload-cleanup
Oct 23, 2026
Merged

madara88645 merged 2 commits into
mainfrom
fix/scheduled-upload-cleanup

Conversation

@madara88645

Copy link
Copy Markdown
Owner

What and why

Expired upload folders (vibegraph_upload_* in the system temp dir) were only deleted by a background task that ran after a later successful upload. On a quiet server, uploaded source code could stay on disk indefinitely, and the README privacy section said so.

This PR adds a timed cleanup, so expired uploads are deleted even when no further upload arrives.

New behavior

  • create_app() now passes a FastAPI lifespan (Starlette 1.x has no on_event):
    • Startup: runs one sweep (cleanup_expired_upload_dirs) before the app serves requests, then starts one asyncio task, upload-cleanup.
    • While running: the task sweeps every UPLOAD_CLEANUP_INTERVAL_SECONDS.
    • Shutdown: the task is cancelled and awaited, so no task is left pending.
  • Each sweep runs in a worker thread (asyncio.to_thread), so filesystem work never blocks the event loop.
  • A sweep that raises is logged (Upload cleanup failed) and later sweeps still run. Cancellation is not swallowed.
  • The per-upload BackgroundTask cleanup is unchanged; it keeps the guarantee tight under load. cleanup_expired_upload_dirs keeps its signature and behavior.
  • Production (uvicorn app:app in Dockerfile and Dockerfile.fly), serve.py and main.py start all use the same app object, so they all get the lifespan.
  • Tests that create TestClient(app) without with do not run the lifespan, and requests work exactly as before.

Guarantee: while the server is running, an upload is deleted within about one hour (retention) plus the interval (5 minutes by default). If the process is stopped (Fly stops idle machines), nothing runs while it is stopped. The sweep runs again on the next start, before requests are served.

Config

  • New: VIBEGRAPH_UPLOAD_CLEANUP_INTERVAL_SECONDS, default 300.
  • Only positive whole numbers of seconds are accepted. Values such as abc, 1.5, an empty string, 0 or -5 log a warning and fall back to 300. So a typo can neither switch the sweep off nor turn it into a busy loop.
  • Retention (VIBEGRAPH_UPLOAD_RETENTION_SECONDS, default 3600) is unchanged.
  • The new variable is documented in the README privacy section and as a comment in .env.example.

How verified

  • New tests/test_upload_cleanup_schedule.py (14 tests, about 1 s):
    • the startup sweep removes an expired dir and keeps a fresh one;
    • the timed sweep removes a dir that expires after startup;
    • failing sweeps are logged and later ones still run;
    • the cleanup runs in a worker thread, not on the event loop's thread;
    • cancelling the task while a sweep is running stops it;
    • leaving the TestClient block cancels the task with no asyncio errors;
    • a direct lifespan test checks that the task is cancelled and awaited while the loop is still running;
    • env parsing covers the default, a valid value, and invalid, zero and negative values.
  • Existing upload and security tests are unchanged, byte for byte.
  • Full suite: 729 passed, 3 runs. ruff format, ruff check and mypy are clean.
  • Mutation checks: each of these makes at least one new test fail:
    • removing the lifespan;
    • removing the shutdown cancel;
    • removing the error handling;
    • removing the interval validation;
    • running the sweep on the loop thread;
    • swallowing CancelledError.
  • Manual checks with python -X dev -m uvicorn app:app, a private TMPDIR, a short interval and an artificially slow delete:
    • the expired dir was gone before the first request;
    • /api/health stayed under 13 ms while a 3 s sweep ran;
    • SIGINT and SIGTERM both completed the lifespan shutdown with no pending-task warnings.
    • Note on SIGTERM: uvicorn re-raises the signal after shutdown, so a sweep still in progress can be cut off. The rest of that expired folder is deleted on the next start.

Out of scope

  • No changes to upload validation, the zip rules, the retention length, dependencies, deploy config (Dockerfiles, fly.toml, workflows), LLM prompts or frontend.
  • No lock between the timed sweep and the per-upload sweep. If they overlap, one may log a harmless "Failed to remove" error, as concurrent uploads already can.
  • The in-memory snippet cache (documented in the next README bullet) is unchanged.

🤖 Generated with Claude Code

madara88645 and others added 2 commits October 23, 2026 21:30
Expired upload folders were only removed by a background task that ran
after a later successful upload, so on a quiet server uploaded source
code could stay on disk indefinitely.

Add a FastAPI lifespan that sweeps expired uploads once at startup and
then every VIBEGRAPH_UPLOAD_CLEANUP_INTERVAL_SECONDS (default 300) until
shutdown. The sweep runs in a worker thread, a failed sweep is logged
and does not stop later ones, and shutdown cancels and awaits the task.
Invalid, zero or negative interval values fall back to the default. The
per-upload cleanup and the one-hour retention are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No test failed when the sweep ran on the event loop thread, and a sweep
that swallowed CancelledError only made the suite hang at random. Add
two tests: one checks that the cleanup runs in a worker thread, the
other cancels the task while a sweep is running and expects it to stop.

Also reword the README line so "every 5 minutes" clearly refers to the
running server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
vibegraph Ready Ready Preview Oct 23, 2026 8:45pm UTC

@madara88645
madara88645 marked this pull request as ready for review October 23, 2026 20:50
@madara88645
madara88645 merged commit 911cb32 into main Oct 23, 2026
10 checks passed

This branch was successfully deployed

1 active deployment
Preview — 1374c54b Deployed Oct 23, 2026 by vercel[bot]
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