fix(storage): resolve cross-platform WinError 32 during data and meta…#21
Open
asked637 wants to merge 1 commit into
Open
fix(storage): resolve cross-platform WinError 32 during data and meta…#21asked637 wants to merge 1 commit into
asked637 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses critical cross-platform storage issues where
HiveStorageandStorageBackendraisePermissionError: [WinError 32] The process cannot access the file because it is being used by another processon Windows environments during data and metadata serialization.Cause of the Bugs
The deadlocks occur due to strict NTFS file-locking mechanisms on Windows when attempting atomic replacement operations while file handles are still active or targets are implicitly locked:
_atomic_write): The original code executestmp_path.replace(target_path)inside thewith tempfile.NamedTemporaryFile(...)context block. Because the current Python process still holds an open file handle to the temporary file, Windows blocks any rename or replace action on it._write_metadata_file): The metadata logic usestmp_path.replace(path)to atomic-rename the JSON config. On Windows, if the target metadata file already exists, or if there is a cross-drive/partition boundary operation (as default temp dirs reside onC:while data directories might be onD:orE:),os.replaceimmediately fails with a permission error.Solution
To preserve the author's original transactional "write-to-temp-first" philosophy while introducing cross-platform resilience, the following optimizations were made:
withcontext blocks. This ensures all file descriptors are fully closed and released by Python before the OS manipulates the file paths.os.name == 'nt') during initialization. This binds an optimized Windows wrapper (shutil.move+ explicitunlink) for Windows environments, while maintaining the raw high-performanceos.replacefor Linux/macOS._write_metadata_fileto use the same safe atomic-replace pattern, preventing transient JSON locks.try...finallyboundaries to ensure any partial temporary files (.parquet.tmp/.json.tmp) are cleanly deleted and never left orphaned on disk upon errors.How to Verify
A dedicated automated test suite
tests/test_windows_compatibility.pyhas been added following the project's native standard layout. It forces back-to-back duplicate writes onto both Parquet matrices and metadata tracking logs to catch any NTFS handle leaks:src/layout (e.g., on a Windows host), run:set PYTHONPATH=src pytest tests/test_windows_compatibility.py -vPASSEDstatus because the patched handle-release pattern fully cloaks Windows' file-locking nature, and successfully triggers.collect()on Polars' nativeLazyFrame. (The unpatched version on themainbranch would instantly crash with aPermissionErrorduring duplicate execution).