Commit 2de74c1
authored
Refactor and Enhance Dispatcher: Worker Management, pg_notify Locking, Error Handling, and More (#1)
* Improve protocol documentation by adding docstrings
* Improve error handling in BrokerCallbacks.listen_for_replies
- Add JSON parsing with exception handling to ignore malformed messages.
- Log warnings when invalid JSON is received.
- Add a unit test
* Rename get_send_message to create_message for clarity
- Change method name in Control class to better reflect its role in constructing messages.
* Add resource cleanup in Control methods and tests
- Ensure broker connections are closed in acontrol, control_with_reply, and control.
- Update tests to verify that aclose() or close() is called appropriately.
* Fix race condition in manage_old_workers and add tests
- Refactor manage_old_workers to use a two-phase locking approach.
- Take a snapshot under lock, process removals, then re-acquire the lock to remove workers atomically.
Potentially closes ansible#124
* Improve error propagation in NextWakeupRunner.process_wakeups
- Wrap process_object callback in try/except to log and re-raise errors.
- Add unit tests to verify normal operation and error propagation.
* pg_notify: Improve ConnectionSaver caching, thread safety, and type correctness
-- Squashed --
Fix ConnectionSaver caching and type issues for closed connections
- Update get_connection and aget_connection to check if the cached connection is closed (i.e. .closed != 0) and reinitialize it if so, ensuring that run_demo.py and other users always receive a live connection.
- Add type assertions to guarantee that a valid (non-None) connection is returned, resolving mypy errors.
Add thread safety to ConnectionSaver in pg_notify.py and add tests
- Introduce a threading.Lock in ConnectionSaver to protect _connection and _async_connection.
- Wrap the initialization in connection_saver and async_connection_saver with the lock to avoid race conditions.
- Update tests to verify that concurrent access creates only one connection.
Note: We use a standard threading.Lock because this is protecting shared state across threads.
* Remove redundant lock in WorkerPool.dispatch_task
- Refactor dispatch_task to avoid holding workers.management_lock for the entire operation.
- Blocker and Queuer functions are expected to be used within the WorkerPool context, so extra locking is unnecessary.
* Add type annotations to context manager methods in ProcessProxy
- Implement __enter__ and __exit__ with proper type annotations.
- __exit__ ensures that a running process is terminated (or killed) and joined. It returns Optional[bool] and ensures proper process cleanup.
* Use f-string in control.py log message
Replace .format() with f-string for improved readability
in control-and-reply log message.1 parent f4b5b53 commit 2de74c1
File tree
10 files changed
+429
-46
lines changed- dispatcher
- brokers
- service
- tests/unit
10 files changed
+429
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
100 | | - | |
101 | | - | |
| 101 | + | |
| 102 | + | |
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
| |||
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
112 | | - | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
| |||
178 | 179 | | |
179 | 180 | | |
180 | 181 | | |
181 | | - | |
| 182 | + | |
| 183 | + | |
182 | 184 | | |
183 | 185 | | |
184 | 186 | | |
| |||
189 | 191 | | |
190 | 192 | | |
191 | 193 | | |
192 | | - | |
| 194 | + | |
193 | 195 | | |
194 | 196 | | |
195 | 197 | | |
| |||
234 | 236 | | |
235 | 237 | | |
236 | 238 | | |
| 239 | + | |
237 | 240 | | |
238 | 241 | | |
239 | 242 | | |
| |||
245 | 248 | | |
246 | 249 | | |
247 | 250 | | |
| 251 | + | |
| 252 | + | |
248 | 253 | | |
249 | | - | |
250 | | - | |
251 | | - | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
252 | 259 | | |
253 | 260 | | |
254 | 261 | | |
| |||
257 | 264 | | |
258 | 265 | | |
259 | 266 | | |
| 267 | + | |
| 268 | + | |
260 | 269 | | |
261 | | - | |
262 | | - | |
263 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
33 | 38 | | |
34 | 39 | | |
35 | 40 | | |
| |||
54 | 59 | | |
55 | 60 | | |
56 | 61 | | |
57 | | - | |
| 62 | + | |
58 | 63 | | |
59 | 64 | | |
60 | 65 | | |
| |||
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
68 | | - | |
| 73 | + | |
69 | 74 | | |
70 | 75 | | |
71 | 76 | | |
| |||
77 | 82 | | |
78 | 83 | | |
79 | 84 | | |
| 85 | + | |
| 86 | + | |
80 | 87 | | |
81 | 88 | | |
82 | 89 | | |
83 | 90 | | |
84 | 91 | | |
85 | | - | |
86 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
87 | 97 | | |
88 | 98 | | |
89 | | - | |
| 99 | + | |
90 | 100 | | |
91 | 101 | | |
92 | | - | |
| 102 | + | |
93 | 103 | | |
94 | 104 | | |
95 | 105 | | |
96 | 106 | | |
97 | 107 | | |
98 | 108 | | |
99 | 109 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
106 | 118 | | |
107 | 119 | | |
108 | | - | |
| 120 | + | |
109 | 121 | | |
110 | | - | |
111 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
6 | 13 | | |
7 | 14 | | |
8 | 15 | | |
| |||
35 | 42 | | |
36 | 43 | | |
37 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
38 | 51 | | |
39 | 52 | | |
40 | 53 | | |
41 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
42 | 62 | | |
43 | 63 | | |
44 | 64 | | |
| |||
55 | 75 | | |
56 | 76 | | |
57 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
58 | 85 | | |
59 | 86 | | |
60 | 87 | | |
| |||
70 | 97 | | |
71 | 98 | | |
72 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
73 | 106 | | |
74 | 107 | | |
75 | 108 | | |
76 | 109 | | |
77 | 110 | | |
78 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
79 | 118 | | |
80 | 119 | | |
81 | 120 | | |
82 | 121 | | |
83 | 122 | | |
84 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
85 | 131 | | |
86 | 132 | | |
87 | 133 | | |
| |||
90 | 136 | | |
91 | 137 | | |
92 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
93 | 146 | | |
94 | 147 | | |
95 | 148 | | |
| |||
106 | 159 | | |
107 | 160 | | |
108 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
109 | 170 | | |
110 | 171 | | |
111 | 172 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
56 | 59 | | |
57 | 60 | | |
58 | 61 | | |
| |||
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
66 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
67 | 74 | | |
68 | 75 | | |
69 | 76 | | |
| |||
0 commit comments