Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 14, 2025

The WSL cleanup feature was deleting user instances instead of properly exporting and re-importing them, causing data loss. This occurred because the cleanup process lacked proper error handling and would proceed to remove instances even when the export step failed.

Problem

The original cleanup() function had several critical issues:

// Original problematic implementation
Future<String> cleanup(String distribution) async {
  var instancePath = getInstancePath(distribution);
  var file = instancePath.file('export.tar.gz');

  // Export, remove, and import - no error checking!
  await export(distribution, file);
  await remove(distribution);  // This runs even if export failed
  return await import(distribution, instancePath.path, file);
}

This meant that if the export failed silently (which it could due to missing exit code checks), the instance would still be removed, resulting in permanent data loss.

Solution

🛡️ Enhanced Error Handling

  • Added comprehensive try-catch blocks with proper error propagation
  • All WSL operations (export, import, remove) now check exit codes and throw exceptions on failure
  • Cleanup only proceeds to removal after successful export verification

🔍 Export Validation

  • Verifies export file exists and has non-zero size before proceeding
  • Prevents removal of instances when export fails
  • Preserves export files for manual recovery if cleanup fails

👤 Improved User Experience

  • Added progress notifications: "Cleaning up [instance]. Exporting, removing and importing back..."
  • Clear success/failure messages with detailed error information
  • Async operation handling with proper error catching in the UI

🧹 Resource Management

  • Automatic cleanup of temporary export files after successful import
  • Graceful handling of file cleanup errors without failing the main operation
  • Proper exception handling maintains system consistency

Key Changes

Core Logic (lib/api/wsl.dart):

  • Enhanced cleanup() function with atomic operation guarantee
  • Added exit code checking to export(), import(), and remove() functions
  • Comprehensive error handling with detailed error messages

User Interface (lib/components/list_item.dart):

  • Added progress notifications during cleanup process
  • Proper async/await handling with error display
  • Clear success and failure feedback

Testing (test/wsl_test.dart):

  • Enhanced cleanup test to verify success messages and file cleanup
  • Added error handling test for nonexistent instances
  • Validation of temporary file management

This fix ensures users never lose their WSL instances due to cleanup failures while providing clear feedback about the operation's progress and results.

Fixes #273.


💡 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.

Copy link
Contributor Author

Copilot AI commented Aug 14, 2025

@bostrot 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI changed the title [WIP] [BUG] "Cleanup" deleted my instance only Fix WSL cleanup function to prevent instance deletion and add proper error handling Aug 14, 2025
Copilot AI requested a review from bostrot August 14, 2025 11:07
Copilot finished work on behalf of bostrot August 14, 2025 11:07
@bostrot bostrot marked this pull request as ready for review August 19, 2025 21:13
@github-actions github-actions bot added source Source code has been changed here. test labels Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

source Source code has been changed here. test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] "Cleanup" deleted my instance only

2 participants