Skip to content

fix(cli): skip background session cleanup when listing sessions#27906

Open
i-anubhav-anand wants to merge 1 commit into
google-gemini:mainfrom
i-anubhav-anand:fix/27273-list-sessions-cleanup-race
Open

fix(cli): skip background session cleanup when listing sessions#27906
i-anubhav-anand wants to merge 1 commit into
google-gemini:mainfrom
i-anubhav-anand:fix/27273-list-sessions-cleanup-race

Conversation

@i-anubhav-anand

Copy link
Copy Markdown

Summary

Fixes #27273.

On startup, cleanupExpiredSessions() is launched as an unawaited background task. A few lines later, --list-sessions reads the same session directory via listSessions(). Because cleanup runs concurrently, it can delete files while listing is scanning them: a file can exist during readdir and then disappear before readFile, so the list shown to the user depends on timing rather than a stable snapshot.

Fix

Skip the background cleanup when the run only reads or mutates the session list (--list-sessions / --delete-session). The list/delete commands then operate on a stable directory, and cleanup still runs as before on normal interactive/non-interactive startups.

This is the minimal version of the issue author's suggested options (skip cleanup for --list-sessions), with no added startup latency.

Testing

  • Added a test in gemini_cleanup.test.tsx asserting that the cleanup task is not invoked when --list-sessions is set (and listSessions still runs). Verified it fails before this change and passes after.
  • npx vitest run src/gemini_cleanup.test.tsx → 4 passed (1 pre-existing skip).
  • eslint (touched files) and npm run typecheck (packages/cli) clean.

On startup, cleanupExpiredSessions() is launched as an unawaited
background task. A few lines later --list-sessions reads the same session
directory. Cleanup can delete files while listSessions is scanning them,
so a file can exist during readdir and then disappear before readFile —
making the listed result depend on timing rather than a stable snapshot.

Skip the background cleanup when the run only reads or mutates the
session list (--list-sessions / --delete-session). The list command then
sees a stable directory, and cleanup still runs on normal startups.

Fixes google-gemini#27273
@i-anubhav-anand i-anubhav-anand requested a review from a team as a code owner June 14, 2026 11:37
@github-actions github-actions Bot added the size/m A medium sized PR label Jun 14, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 75
  • Additions: +71
  • Deletions: -4
  • Files changed: 2

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the summary. You can try again by commenting /gemini summary.

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality labels Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p2 Important but can be addressed in a future release. size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cleanup and --list-sessions race each other

1 participant