|
2 | 2 | import glob |
3 | 3 | import logging |
4 | 4 | import os |
| 5 | +import threading |
5 | 6 | from contextlib import contextmanager |
6 | 7 | from signal import SIGINT, SIGTERM, getsignal, signal |
7 | 8 |
|
@@ -218,29 +219,24 @@ def read_lock(obj: object) -> object: |
218 | 219 | raise AttributeError(f"Cannot lock: {obj}.") |
219 | 220 |
|
220 | 221 | # handle a premature Ctrl+C exit from this context manager |
221 | | - old_sigterm = None |
222 | | - old_sigint = None |
223 | | - try: |
224 | | - old_sigterm = getsignal(SIGTERM) |
225 | | - old_sigint = getsignal(SIGINT) |
| 222 | + prev_sigterm = None |
| 223 | + prev_sigint = None |
| 224 | + if threading.current_thread() is threading.main_thread(): |
| 225 | + prev_sigterm = getsignal(SIGTERM) |
| 226 | + prev_sigint = getsignal(SIGINT) |
226 | 227 | signal(SIGTERM, locker._interrupt_handler) |
227 | 228 | signal(SIGINT, locker._interrupt_handler) |
228 | | - # If this is run in a thread, the signal module is not available and raises an exception. |
229 | | - # ValueError: signal only works in main thread of the main interpreter |
230 | | - # That's fine; in this case, we don't need to handle signals anyway. |
231 | | - except ValueError as e: |
232 | | - _LOGGER.error(f"Failed to set interrupt handler: {e}") |
233 | 229 |
|
234 | 230 | locker.read_lock() |
235 | 231 |
|
236 | 232 | try: |
237 | 233 | yield obj |
238 | 234 | finally: |
239 | 235 | locker.read_unlock() |
240 | | - if old_sigterm is not None: |
| 236 | + if prev_sigterm is not None: |
241 | 237 | try: |
242 | | - signal(SIGTERM, old_sigterm) |
243 | | - signal(SIGINT, old_sigint) |
| 238 | + signal(SIGTERM, prev_sigterm) |
| 239 | + signal(SIGINT, prev_sigint) |
244 | 240 | except ValueError: |
245 | 241 | pass |
246 | 242 |
|
@@ -276,30 +272,28 @@ def write_lock(obj: object) -> object: |
276 | 272 | raise AttributeError(f"Cannot lock: {obj}.") |
277 | 273 |
|
278 | 274 | # handle a premature Ctrl+C exit from this context manager |
279 | | - old_sigterm = None |
280 | | - old_sigint = None |
281 | | - try: |
282 | | - old_sigterm = getsignal(SIGTERM) |
283 | | - old_sigint = getsignal(SIGINT) |
| 275 | + prev_sigterm = None |
| 276 | + prev_sigint = None |
| 277 | + if threading.current_thread() is threading.main_thread(): |
| 278 | + prev_sigterm = getsignal(SIGTERM) |
| 279 | + prev_sigint = getsignal(SIGINT) |
284 | 280 | signal(SIGTERM, locker._interrupt_handler) |
285 | 281 | signal(SIGINT, locker._interrupt_handler) |
286 | | - except ValueError as e: |
287 | | - _LOGGER.error(f"Failed to set interrupt handler: {e}") |
288 | 282 |
|
289 | 283 | locker.write_lock() |
290 | 284 | try: |
291 | 285 | yield obj |
292 | 286 | finally: |
293 | 287 | locker.write_unlock() |
294 | | - if old_sigterm is not None: |
| 288 | + if prev_sigterm is not None: |
295 | 289 | try: |
296 | | - signal(SIGTERM, old_sigterm) |
297 | | - signal(SIGINT, old_sigint) |
| 290 | + signal(SIGTERM, prev_sigterm) |
| 291 | + signal(SIGINT, prev_sigint) |
298 | 292 | except ValueError: |
299 | 293 | pass |
300 | 294 |
|
301 | 295 |
|
302 | | -def locked_read_file(filepath, create_file: bool = False) -> str: |
| 296 | +def locked_read_file(filepath: str, create_file: bool = False) -> str: |
303 | 297 | """Read a file contents into memory after locking the file. |
304 | 298 |
|
305 | 299 | This will prevent other ThreeLocker-protected processes from writing to the |
|
0 commit comments