Skip to content

fix(pm): add thread synchronization to prevent race conditions#197

Closed
fengmk2 wants to merge 1 commit intomainfrom
09-24-fix_pm_prevent_race_conditions_in_the_download_package_manager_function
Closed

fix(pm): add thread synchronization to prevent race conditions#197
fengmk2 wants to merge 1 commit intomainfrom
09-24-fix_pm_prevent_race_conditions_in_the_download_package_manager_function

Conversation

@fengmk2
Copy link
Copy Markdown
Member

@fengmk2 fengmk2 commented Sep 24, 2025

closes #140

  • Add global mutex lock using OnceLock for file system operations
  • Protect critical sections in download_package_manager function
  • Add test-specific lock for temp directory creation
  • Fix "Directory not empty" errors during concurrent test execution

The synchronization ensures concurrent calls won't interfere when:

  • Checking if files exist
  • Removing old directories
  • Renaming temporary directories
  • Creating shim files

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

Copy link
Copy Markdown
Member Author

fengmk2 commented Sep 24, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@fengmk2 fengmk2 self-assigned this Sep 24, 2025
@fengmk2 fengmk2 marked this pull request as ready for review September 24, 2025 03:44
Copilot AI review requested due to automatic review settings September 24, 2025 03:44
@fengmk2 fengmk2 changed the title fix(package-manager): add thread synchronization to prevent race conditions fix(pm): add thread synchronization to prevent race conditions Sep 24, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes race conditions in package manager operations by adding thread synchronization using mutex locks. The changes prevent "Directory not empty" errors during concurrent execution, particularly in test scenarios.

  • Added global mutex lock for critical file system operations in the package manager
  • Protected directory removal, renaming, and shim file creation operations
  • Added test-specific synchronization for temporary directory creation
Comments suppressed due to low confidence (1)

crates/vite_package_manager/src/package_manager.rs:1

  • The lock scope is too broad and includes potentially long-running operations like create_shim_files. Consider reducing the lock scope to only protect the critical directory operations (remove and rename) to avoid blocking other threads unnecessarily during shim file creation.
use std::{

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread crates/vite_package_manager/src/package_manager.rs
@fengmk2 fengmk2 force-pushed the 09-24-fix_pm_prevent_race_conditions_in_the_download_package_manager_function branch from 6e1ad6d to 4d1b317 Compare September 24, 2025 03:47
…itions

- Add global mutex lock using OnceLock for file system operations
- Protect critical sections in download_package_manager function
- Add test-specific lock for temp directory creation
- Fix "Directory not empty" errors during concurrent test execution

The synchronization ensures concurrent calls won't interfere when:
- Checking if files exist
- Removing old directories
- Renaming temporary directories
- Creating shim files

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings September 24, 2025 03:48
@fengmk2 fengmk2 force-pushed the 09-24-fix_pm_prevent_race_conditions_in_the_download_package_manager_function branch from 4d1b317 to 53a92fb Compare September 24, 2025 03:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

crates/vite_package_manager/src/package_manager.rs:1

  • The mutex lock is held during the entire file system operation sequence, including potentially slow operations like remove_dir_all_force, tokio::fs::rename, and create_shim_files. This could create a bottleneck if multiple package managers need to be downloaded concurrently. Consider using a more granular locking strategy or per-directory locks instead of a global lock.
use std::{

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@fengmk2
Copy link
Copy Markdown
Member Author

fengmk2 commented Sep 24, 2025

This issue only exists when single testing is performed in a multi-threaded concurrent execution environment; it does not occur in actual user operations. Before performing any repairs, it is necessary to have these tests executed in the correct order.

@fengmk2 fengmk2 marked this pull request as draft September 24, 2025 05:32
@fengmk2 fengmk2 closed this Sep 24, 2025
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.

fix package_manager unstable tests

2 participants