-
Notifications
You must be signed in to change notification settings - Fork 3
[INFRAHELP-2899] fix: address race condition in load_nested_zip() and add tests #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f90731a
fix: make sure zip file descriptors are properly closed
ajaxbits d9ddb3c
fix: use file locking to avoid race conditions on nested zip extraction
ajaxbits 1300f00
test: add unit tests for nested_zip_loader
ajaxbits c5d4bcf
Potential fix for pull request finding
ajaxbits 8e6476f
Spelling
ajaxbits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import importlib | ||
| import multiprocessing | ||
| import shutil | ||
| import sys | ||
| import tempfile | ||
| import zipfile | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| # We have to use importlib here because nested_zip_loader calls load_nested_zip | ||
| # at IMPORT TIME, which causes us a world of hurt in these tests if we try to | ||
| # import it "normally" here. | ||
| LOADER_PATH = Path(__file__).parent.parent / "package_python_function" / "nested_zip_loader.py" | ||
| PKG_NAME = "_test_nested_zip" | ||
|
|
||
| def _make_deps_zip(path: Path) -> None: | ||
| with zipfile.ZipFile(path, "w") as zf: | ||
| zf.writestr(f"{PKG_NAME}/__init__.py", "LOADED = True\n") | ||
|
|
||
| @pytest.fixture() | ||
| def lambda_env(tmp_path, monkeypatch): | ||
| """Simulate a Lambda-like layout: a task dir with a package whose __init__.py | ||
| is the nested_zip_loader code, and a .dependencies.zip with the 'real' code.""" | ||
| task_dir = tmp_path / "task" | ||
| pkg_dir = task_dir / PKG_NAME | ||
| pkg_dir.mkdir(parents=True) | ||
| shutil.copy(LOADER_PATH, pkg_dir / "__init__.py") | ||
| _make_deps_zip(pkg_dir / ".dependencies.zip") | ||
|
|
||
| tmp_dir = tmp_path / "tmp" | ||
| tmp_dir.mkdir() | ||
| monkeypatch.setenv("TMPDIR", str(tmp_dir)) | ||
| tempfile.tempdir = None | ||
|
|
||
| monkeypatch.syspath_prepend(str(task_dir)) | ||
|
|
||
| yield tmp_path | ||
|
|
||
| sys.modules.pop(PKG_NAME, None) | ||
| tempfile.tempdir = None | ||
|
|
||
| def test_cold_start_extracts(lambda_env): | ||
| mod = importlib.import_module(PKG_NAME) | ||
| assert mod.LOADED is True | ||
| assert (lambda_env / "tmp" / "package-python-function").exists() | ||
|
|
||
| def test_warm_start_skips_extraction(lambda_env): | ||
| target_pkg = lambda_env / "tmp" / "package-python-function" / PKG_NAME | ||
| target_pkg.mkdir(parents=True) | ||
| (target_pkg / "__init__.py").write_text("LOADED = 'warm'\n") | ||
|
|
||
| mod = importlib.import_module(PKG_NAME) | ||
| assert mod.LOADED == "warm" | ||
|
|
||
| def test_stale_staging_cleaned(lambda_env): | ||
| staging = lambda_env / "tmp" / ".stage.package-python-function" | ||
| staging.mkdir(parents=True) | ||
| (staging / "stale.txt").write_text("leftover") | ||
|
|
||
| importlib.import_module(PKG_NAME) | ||
| assert not staging.exists() | ||
|
|
||
| def _worker(task_dir): | ||
| import importlib | ||
| import sys | ||
|
|
||
| sys.path.insert(0, task_dir) | ||
| assert importlib.import_module(PKG_NAME).LOADED is True | ||
|
|
||
| def test_concurrent_no_race(lambda_env): | ||
| ctx = multiprocessing.get_context("forkserver") | ||
| procs = [ctx.Process(target=_worker, args=(str(lambda_env / "task"),)) for _ in range(2)] | ||
| for p in procs: | ||
| p.start() | ||
| for p in procs: | ||
| p.join(timeout=10) | ||
|
ajaxbits marked this conversation as resolved.
ajaxbits marked this conversation as resolved.
|
||
| assert p.exitcode == 0, "A race condition occurred while extracting." | ||
| assert (lambda_env / "tmp" / "package-python-function").exists() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.