Start a watchdog timer to kill the process if it takes too long Args: timeout: Number of seconds to wait before killing process (default: 60 seconds)
(timeout: int = 60)
| 125 | |
| 126 | |
| 127 | def setup_watchdog(timeout: int = 60) -> threading.Thread: |
| 128 | """Start a watchdog timer to kill the process if it takes too long |
| 129 | |
| 130 | Args: |
| 131 | timeout: Number of seconds to wait before killing process (default: 60 seconds) |
| 132 | """ |
| 133 | |
| 134 | def watchdog_timer() -> None: |
| 135 | time.sleep(timeout) |
| 136 | print( |
| 137 | f"\n🚨 WATCHDOG TIMER EXPIRED - Process took too long! ({timeout} seconds)" |
| 138 | ) |
| 139 | dump_thread_stacks() |
| 140 | |
| 141 | # Kill all child processes and then ourselves |
| 142 | kill_process_tree(os.getpid()) |
| 143 | |
| 144 | # If we're still here, force exit |
| 145 | time.sleep(1) # Give a moment for output to flush |
| 146 | os._exit(2) # Exit with error code 2 to indicate timeout |
| 147 | |
| 148 | watchdog = threading.Thread( |
| 149 | target=watchdog_timer, daemon=True, name="WatchdogTimer" |
| 150 | ) |
| 151 | watchdog.start() |
| 152 | return watchdog |
| 153 | |
| 154 | |
| 155 | def setup_force_exit() -> threading.Thread: |