Commit fd3eb9f
feat: signal handler race condition (#170)
* refactor: Fix race condition in signal handler calling sys.exit during async execu
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
* fix: address review comments - restore sys.exit and add concurrent test
- Revert os._exit() to sys.exit() to allow proper stack unwinding and
finally block execution (prevents leaking external resources)
- Update docstrings to clarify 'at-most-once' execution semantics
- Add concurrent cleanup test to verify thread-safety under parallel access
- The lock now correctly ensures cleanup runs only once while preserving
proper Python cleanup semantics
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
* fix: use RLock to prevent signal handler deadlock
- Change threading.Lock to threading.RLock to prevent deadlock when signal
arrives while atexit holds the lock (signal handlers run in same thread)
- Remove test_docker_manager_has_cleanup_lock (tests implementation details)
- Clean up dead code in concurrent test (unused tracking variables)
- Update docstring to document RLock usage
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
* fix: prevent signal handler from interrupting ongoing cleanup
- Add _cleanup_in_progress flag to detect re-entrant calls during cleanup
- Move entire cleanup operation inside the lock to prevent partial cleanup
- Return status from _cleanup_resources (True=done, False=already done, None=in progress)
- Signal handler returns without sys.exit() if cleanup is in progress,
allowing ongoing cleanup to complete without SystemExit interruption
- Add sys.stdout.flush() before os._exit(1) to ensure message is printed
- Set _cleanup_done after cleanup completes (not before) for accurate state
- Add test for in-progress cleanup detection
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
* fix: apply race condition fixes to BinaryManager and improve tests
- Apply same thread-safe cleanup pattern to BinaryManager:
- Add RLock, _cleanup_in_progress, _cleanup_done flags
- Update signal handler to check cleanup result before sys.exit()
- Add sys.stdout.flush() before os._exit(1)
- Move cleanup operations inside lock with try/finally
- Fix _cleanup_on_exit in both managers to call cleanup unconditionally
(it's idempotent and returns immediately if already done/in progress)
- Update concurrent test to verify return value distribution:
exactly one True (performed cleanup), rest False (already done)
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
* refactor: use CleanupResult enum and add BinaryManager tests
- Add CleanupResult enum (PERFORMED, ALREADY_DONE, IN_PROGRESS) to constants.py
for self-documenting cleanup return values
- Update DockerManager and BinaryManager to use CleanupResult enum
- Update DockerManager tests to use CleanupResult enum
- Add BinaryManager tests for:
- Double cleanup prevention
- Concurrent access handling with return value distribution
- In-progress state detection
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
* fix: restore deleted tests and add IN_PROGRESS assertion
- Restore all graceful shutdown tests from master that were lost during rebase
- Restore all CORS validation and configuration tests
- Add IN_PROGRESS assertion to concurrent access tests to verify no re-entrancy bugs
- Rename flag state tests to clarify they test flag check logic specifically
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
* refactor: extract common cleanup logic into CleanupMixin
- Create CleanupMixin ABC in cleanup_mixin.py with:
- _init_cleanup_state(): Initialize cleanup-related instance variables
- _setup_signal_handlers(): Register SIGINT/SIGTERM handlers and atexit
- _signal_handler(): Handle signals with graceful shutdown
- _cleanup_on_exit(): Atexit handler
- _cleanup_resources_guarded(): Thread-safe cleanup wrapper with RLock
- _cleanup_resources(): Default implementation calling _do_cleanup()
- remove_signal_handlers(): Restore original signal handlers
- Abstract _do_cleanup(): Subclasses implement actual cleanup
- Update DockerManager to inherit from CleanupMixin:
- Call _init_cleanup_state() in __init__
- Implement _do_cleanup() with container graceful shutdown
- Override _cleanup_resources() to add drain_timeout/stop_timeout params
- Update BinaryManager to inherit from CleanupMixin:
- Call _init_cleanup_state() in __init__
- Implement _do_cleanup() with process termination logic
- Use default _cleanup_resources() from mixin
This eliminates ~90 lines of duplicated code between the managers while
maintaining identical behavior and thread-safety guarantees.
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>
---------
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Sandi Fatic <chefsale@users.noreply.github.com>1 parent 3b010d5 commit fd3eb9f
File tree
6 files changed
+455
-102
lines changed- merobox
- commands
- tests/unit
6 files changed
+455
-102
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | 73 | | |
75 | 74 | | |
76 | 75 | | |
77 | 76 | | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
| 77 | + | |
| 78 | + | |
115 | 79 | | |
116 | 80 | | |
117 | 81 | | |
| |||
132 | 96 | | |
133 | 97 | | |
134 | 98 | | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | 99 | | |
145 | 100 | | |
146 | 101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
5 | 22 | | |
6 | 23 | | |
7 | 24 | | |
| |||
0 commit comments