Repository navigation
fix: defer stdin temp-file unlink until fd 0 is restored (Windows) #104
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
Open
ygd58
wants to merge
2
commits into
genlayerlabs:v0.30-dev
Choose a base branch
from
ygd58:fix/windows-stdin-tempfile-unlink
base: v0.30-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,113 @@ | ||
| """Regression tests for the stdin-injection temp file lifecycle. | ||
|
|
||
| See issue #99 (bug 3): unlinking the temp file used to inject the message | ||
| context into fd 0 immediately after os.dup2() works on POSIX (the directory | ||
| entry is removed but the data stays available via the still-open fd) but | ||
| raises PermissionError on Windows, which locks files with open handles. | ||
|
|
||
| The fix defers the unlink until fd 0's duplicate has actually been closed | ||
| in VMContext._cleanup_after_deactivate. | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from gltest.direct import loader | ||
| from gltest.direct.vm import VMContext | ||
|
|
||
|
|
||
| class _FakeAddress: | ||
| def __init__(self, data): | ||
| self.data = data | ||
|
|
||
|
|
||
| class _FakeCalldata: | ||
| """Minimal calldata stand-in: encode() just needs to produce bytes.""" | ||
|
|
||
| @staticmethod | ||
| def encode(_message_data): | ||
| return b"fake-encoded-message" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fake_sdk(monkeypatch): | ||
| """Avoid requiring a real downloaded SDK for this unit test.""" | ||
| monkeypatch.setattr(loader, "import_calldata", lambda: _FakeCalldata()) | ||
| monkeypatch.setattr(loader, "import_address", lambda: _FakeAddress) | ||
|
|
||
|
|
||
| class TestInjectMessageToFd0: | ||
| def test_temp_file_is_not_deleted_immediately(self, fake_sdk): | ||
| """Regression: the temp file must still exist right after | ||
| _inject_message_to_fd0 returns, because fd 0 still references it. | ||
| Deleting it immediately is what breaks on Windows.""" | ||
| vm = VMContext() | ||
| original_stdin = os.dup(0) | ||
| try: | ||
| loader._inject_message_to_fd0(vm) | ||
|
|
||
| temp_path = getattr(vm, "_stdin_temp_path", None) | ||
| assert temp_path is not None, "temp path must be recorded on vm for deferred cleanup" | ||
| assert os.path.exists(temp_path), "temp file must not be unlinked while fd 0 still holds it open" | ||
| finally: | ||
| # Restore real stdin and clean up manually (mirrors _cleanup_after_deactivate) | ||
| os.dup2(original_stdin, 0) | ||
| os.close(original_stdin) | ||
| stdin_fd = getattr(vm, "_original_stdin_fd", None) | ||
| if stdin_fd is not None: | ||
| os.close(stdin_fd) | ||
| temp_path = getattr(vm, "_stdin_temp_path", None) | ||
| if temp_path is not None: | ||
| os.unlink(temp_path) | ||
|
|
||
| def test_cleanup_after_deactivate_removes_temp_file(self, fake_sdk): | ||
| """After the VM context restores stdin, the deferred temp file | ||
| must be cleaned up (and cleanup must not raise, mirroring the | ||
| try/except OSError guard needed for Windows).""" | ||
| vm = VMContext() | ||
| original_stdin = os.dup(0) | ||
| try: | ||
| loader._inject_message_to_fd0(vm) | ||
| temp_path = vm._stdin_temp_path | ||
| assert os.path.exists(temp_path) | ||
|
|
||
| vm._cleanup_after_deactivate() | ||
|
|
||
| assert not os.path.exists(temp_path), "temp file must be removed after cleanup" | ||
| assert vm._stdin_temp_path is None | ||
| assert vm._original_stdin_fd is None | ||
| finally: | ||
| os.dup2(original_stdin, 0) | ||
| os.close(original_stdin) | ||
|
|
||
| def test_cleanup_is_safe_when_nothing_was_injected(self): | ||
| """Calling cleanup on a VM that never replaced stdin (e.g. ImportError | ||
| early-return in _inject_message_to_fd0) must not raise.""" | ||
| vm = VMContext() | ||
| vm._cleanup_after_deactivate() # should not raise | ||
|
|
||
| def test_cleanup_tolerates_missing_temp_file(self, fake_sdk): | ||
| """If the temp file was already removed some other way, cleanup | ||
| must swallow the OSError instead of propagating it (same guard | ||
| already used for the stdin dup2/close above it).""" | ||
| vm = VMContext() | ||
| original_stdin = os.dup(0) | ||
| try: | ||
| loader._inject_message_to_fd0(vm) | ||
| # Restore fd 0 before manually unlinking below — otherwise this | ||
| # unlink is exactly the "unlink while fd 0 still references the | ||
| # file" case this whole fix exists to avoid, and would itself | ||
| # raise PermissionError on Windows. vm._original_stdin_fd is | ||
| # left as-is so _cleanup_after_deactivate's own restore path is | ||
| # still exercised below (redundant dup2 onto the same target, | ||
| # then close — harmless). | ||
| os.dup2(original_stdin, 0) | ||
| os.unlink(vm._stdin_temp_path) # simulate external removal | ||
|
|
||
| vm._cleanup_after_deactivate() # should not raise despite missing file | ||
|
|
||
| assert vm._stdin_temp_path is None | ||
| finally: | ||
| os.dup2(original_stdin, 0) | ||
| os.close(original_stdin) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Close fd 0 before deleting the file in this test.
Line 98 unlinks the temporary file while fd 0 still references it. Windows raises
PermissionErrorfor this operation, so the regression test fails on the target platform. Restore fd 0 beforeos.unlink, while leavingvm._original_stdin_fdfor_cleanup_after_deactivate().As per PR objectives, this regression test must pass on Windows.
Proposed test fix
loader._inject_message_to_fd0(vm) + os.dup2(original_stdin, 0) os.unlink(vm._stdin_temp_path)📝 Committable suggestion
🤖 Prompt for AI Agents