Skip to content

Startup sweep for orphaned temp session directories#218

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/cleanup-orphaned-temp-sessions
Draft

Startup sweep for orphaned temp session directories#218
Copilot wants to merge 2 commits intomainfrom
copilot/cleanup-orphaned-temp-sessions

Conversation

Copy link
Contributor

Copilot AI commented Feb 26, 2026

Temp directories created by CreateSessionAsync when workingDirectory is null were never cleaned up if the app crashed or was force-killed, accumulating indefinitely under polypilot-sessions/ in the system temp folder.

Changes

  • CreateSessionAsync: When workingDirectory is null, creates an isolated temp dir under TempSessionsBase ($TMPDIR/polypilot-sessions/<uuid>) instead of falling back to ProjectDir. Logs and falls back to ProjectDir if creation fails.
  • CloseSessionAsync: Deletes the temp dir after session disposal if it's a direct child of TempSessionsBase.
  • Startup sweep (RestorePreviousSessionsAsync): Calls new SweepOrphanedTempSessionDirs on startup, deleting any dirs under TempSessionsBase whose path doesn't appear in any WorkingDirectory in active-sessions.json.
  • Test isolation: TempSessionsBase is a redirectable static property reset by SetBaseDirForTesting so tests never touch the real $TMPDIR/polypilot-sessions/.
// On startup, after loading persisted entries:
SweepOrphanedTempSessionDirs(TempSessionsBase, entries?.Select(e => e.WorkingDirectory) ?? []);

// Deletes dirs not referenced by any persisted session:
internal static void SweepOrphanedTempSessionDirs(string tempBase, IEnumerable<string?> persistedWorkingDirs)
{
    if (!Directory.Exists(tempBase)) return;
    var keepDirs = new HashSet<string>(
        persistedWorkingDirs.Where(d => !string.IsNullOrEmpty(d)).Select(d => d!),
        StringComparer.OrdinalIgnoreCase);
    foreach (var dir in Directory.GetDirectories(tempBase))
        if (!keepDirs.Contains(dir))
            try { Directory.Delete(dir, true); } catch { }
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • e7bvsblobprodcus348.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet restore --no-dependencies /tmp/codeql-scratch-abbd4936ea2bb08e/dbs/csharp/working/622A4908CDBE24FF535A100F8AC55E4F/missingpackages_workingdir --packages /tmp/codeql-scratch-abbd4936ea2bb08e/dbs/csharp/working/missingpackages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal --configfile /tmp/codeql-scratch-abbd4936ea2bb08e/dbs/csharp/working/nugetconfig/nuget.config --force (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Startup sweep for orphaned temp session directories</issue_title>
<issue_description>## Problem

When \CreateSessionAsync\ is called with \workingDirectory: null, it creates a temp directory under \polypilot-sessions/\ in the system temp folder. Cleanup only happens in \CloseSessionAsync. If the app crashes, is force-quit, or the process is killed (e.g. during
elaunch.ps1), these directories accumulate indefinitely.

Proposed Fix

On app startup (during \CopilotService.InitAsync\ or similar), scan for \polypilot-sessions/\ directories that don't correspond to any active or persisted session, and delete them.

Something like:
\\csharp
var tempBase = Path.Combine(Path.GetTempPath(), "polypilot-sessions");
if (Directory.Exists(tempBase))
{
foreach (var dir in Directory.GetDirectories(tempBase))
{
var dirName = Path.GetFileName(dir);
if (!_sessions.ContainsKey(dirName) && !persistedSessionIds.Contains(dirName))
try { Directory.Delete(dir, true); } catch { }
}
}
\\

Impact

Low risk — temp dirs are small (empty or near-empty). But over time with frequent relaunches during development, they accumulate.

Found during multi-model review of PR #205.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copilot AI changed the title [WIP] Cleanup orphaned temp session directories on startup Startup sweep for orphaned temp session directories Feb 26, 2026
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.

Startup sweep for orphaned temp session directories

2 participants