Skip to content

Fix for Windows file locking (os error 1224)#589

Open
PantelisAndrianakis wants to merge 5 commits intounslothai:mainfrom
PantelisAndrianakis:main
Open

Fix for Windows file locking (os error 1224)#589
PantelisAndrianakis wants to merge 5 commits intounslothai:mainfrom
PantelisAndrianakis:main

Conversation

@PantelisAndrianakis
Copy link
Copy Markdown

Implement robust Windows file locking handling in model export

The export process was failing with os error 1224 ("file with a user-mapped section open") when trying to save merged LoRA weights on Windows. This occurred because safetensors was memory-mapping files that couldn't be replaced due to locks held by the kernel or other processes.

Changes:

  • Add retry logic with exponential backoff (10 attempts, up to ~1.6s wait)
  • Force garbage collection and CUDA cache cleanup before each write attempt
  • Attempt to delete the original locked file before writing
  • Write directly to target location instead of temp file (simpler atomic ops)
  • Improved error messages with clear remediation steps

This allows GGUF exports to succeed on Windows systems with aggressive file locking (antivirus, indexing, etc.). Users can now export Gemma4 models directly in Unsloth Studio without workarounds.

Tested with: Gemma4 model, Q4_K_M quantization, Windows 11 Pro

Implement robust Windows file locking handling in model export

The export process was failing with os error 1224 ("file with a user-mapped section open") when trying to save merged LoRA weights on Windows. This occurred because safetensors was memory-mapping files that couldn't be replaced due to locks held by the kernel or other processes.

Changes:
- Add retry logic with exponential backoff (10 attempts, up to ~1.6s wait)
- Force garbage collection and CUDA cache cleanup before each write attempt
- Attempt to delete the original locked file before writing
- Write directly to target location instead of temp file (simpler atomic ops)
- Improved error messages with clear remediation steps

This allows GGUF exports to succeed on Windows systems with aggressive file locking (antivirus, indexing, etc.). Users can now export Gemma4 models directly in Unsloth Studio without workarounds.

Tested with: Gemma4 model, Q4_K_M quantization, Windows 11 Pro
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a retry mechanism with exponential backoff to resolve Windows file locking issues (OS error 1224) when saving tensors. The implementation includes aggressive cleanup steps such as manual garbage collection and CUDA cache clearing to help release file handles. Review feedback correctly identifies redundant local imports and inconsistent module aliasing that should be addressed to maintain code quality and consistency with the rest of the project.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f205ac82a5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

PantelisAndrianakis and others added 2 commits April 10, 2026 15:48
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 41997c33b3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Applied the safe atomic write fix to the new version.
The key differences:
- Write to temp file first (original stays intact)
- Delete original only after successful write (no data loss)
- Atomic move replaces with temp file
- Cleanup on failure
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 06249dd53d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

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