Add failing tests for #403: File Handle Resource Leak in SyncLock.acquire()#405
Merged
gltanaka merged 2 commits intopromptdriven:mainfrom Jan 27, 2026
Merged
Conversation
…ak in SyncLock.acquire() This commit adds comprehensive unit and E2E tests that detect the file handle resource leak in SyncLock.acquire() when non-IOError/OSError exceptions occur. Tests added: - Unit tests in test_sync_determine_operation.py (6 tests) - KeyboardInterrupt during lock acquisition - RuntimeError during lock acquisition - Exception during file operations - IOError/OSError regression tests - Normal operation regression tests - Context manager exception handling - E2E tests in test_e2e_issue_403_file_handle_leak.py (4 tests) - Real-world KeyboardInterrupt scenario (Ctrl+C) - RuntimeError leak detection - Normal operation verification - Context manager interrupt handling All tests correctly fail on current code, detecting the bug where file descriptors remain open when exceptions other than IOError/OSError occur during lock acquisition. Related to promptdriven#403 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive test coverage to detect a file descriptor resource leak in SyncLock.acquire() when interrupted by exceptions other than IOError or OSError. The tests verify that the current exception handling is insufficient and demonstrate that the proposed fix (nested try-finally block) will resolve the issue.
Changes:
- Added 6 unit tests covering KeyboardInterrupt, RuntimeError, write failures, and regression scenarios
- Added 4 E2E tests that simulate real-world interrupt scenarios using subprocess isolation
- Included the fix implementation showing nested try-finally pattern for guaranteed cleanup
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/test_sync_determine_operation.py | Unit tests for file handle cleanup under various exception conditions |
| tests/test_e2e_issue_403_file_handle_leak.py | E2E tests simulating real-world KeyboardInterrupt and RuntimeError scenarios |
| pdd/sync_determine_operation.py | Implementation of nested try-finally fix for guaranteed file descriptor cleanup |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
Fixes the file handle resource leak bug in
SyncLock.acquire()reported in #403.Changes
Code Fix
File:
pdd/sync_determine_operation.py(lines 185-202)Added nested try-except block to ensure file descriptors are properly closed on ANY exception:
Tests Added
File:
tests/test_sync_determine_operation.pytest_file_handle_cleanup_on_keyboard_interrupt- Verifies fd cleanup on Ctrl+Ctest_file_handle_cleanup_on_runtime_error- Verifies fd cleanup on unexpected errorstest_file_handle_cleanup_on_exception_during_write- Verifies fd cleanup during write failuresTesting
✅ All 11 SyncLock unit tests pass
✅ All 88 tests in test_sync_determine_operation.py pass
✅ 5/5 manual comprehensive tests pass
Manual Testing Performed
Impact
OSError: [Errno 24] Too many open filesRoot Cause Fixed
The original code only caught
(IOError, OSError)for cleanup, allowing other exceptions likeKeyboardInterrupt,RuntimeError, orValueErrorto leak file descriptors. The fix uses a bareexcept:clause to catch ALL exceptions and ensure cleanup.Fixes #403