[INFRAHELP-2899] fix: address race condition in load_nested_zip() and add tests - #15
Conversation
84fd64b to
4d3a673CompareThere was a problem hiding this comment.
Pull request overview
This PR addresses a reported race condition in package_python_function/nested_zip_loader.py by serializing extraction/rename with a filesystem lock, and introduces a focused test suite to validate cold-start/warm-start behavior and concurrent imports.
Changes:
- Add an
fcntl.flock-based lockfile around nested dependency extraction/rename to prevent concurrent processes from racing. - Add
tests/test_nested_zip_loader.pyto validate extraction behavior, staging cleanup, and a basic multi-process concurrency scenario. - Remove the prior packaging/CLI test suite (
tests/test_package_python_function.py,tests/conftest.py,tests/test_local.py) and add.opencodeto.gitignore.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
package_python_function/nested_zip_loader.py | Adds a lockfile to avoid concurrent extraction/rename races; minor formatting changes. |
tests/test_nested_zip_loader.py | New unit tests for loader cold/warm starts, staging cleanup, and concurrency. |
tests/test_package_python_function.py | Removed (previous end-to-end packaging/reproducibility tests). |
tests/conftest.py | Removed (previous shared test data/fixtures for packaging tests). |
tests/test_local.py | Removed (previous local/manual test helper). |
.gitignore | Ignores .opencode. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
332f77b to
afe566dCompareede27cc to
5b1a7f9Compare5b1a7f9 to
1300f00CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
BrandonLWhite
left a comment
There was a problem hiding this comment.
Looks like a very good solution to the problem. Thanks!
Uh oh!
There was an error while loading. Please reload this page.
Fixes a race condition in
nested_zip_loaderwhere, if multiple processes are trying to runload_nested_zip()a race condition could occur. Two processes would try to callos.renameat the same time, which would fail if the target_package_path was not empty.To get around this, we create a filesystem lockfile. Each time
load_nested_zip()runs, it has to acquire the lock, ensuring that concurrent unzips are impossible.A basic set of tests are also added to assert this behavior. It tests that cold starts always extract, warm starts skip the extraction step, nothing is left behind, and that multiple processes running at the same time don't create a race condition.
Note
We have to use
importlibin the tests becausenested_zip_loader.pycallsload_nested_zip()at import time. This is essential to its function and can't be worked around. Therefore, we have to do some shenanigans withimportlibto be able to actually testload_nested_zip()in isolation on test data.Finally, we fix a small bug where we never explicitly closed
zipfile.ZipFile.