()
| 212 | |
| 213 | # --- Cleanup function (executed on exit) (from dev - more detailed logging and checks) --- |
| 214 | def cleanup(): |
| 215 | global camoufox_proc |
| 216 | logger.info("--- Starting cleanup routine (launch_camoufox.py) ---") |
| 217 | if camoufox_proc and camoufox_proc.poll() is None: |
| 218 | pid = camoufox_proc.pid |
| 219 | logger.info(f"Terminating Camoufox internal subprocess (PID: {pid})...") |
| 220 | try: |
| 221 | if ( |
| 222 | sys.platform != "win32" |
| 223 | and hasattr(os, "getpgid") |
| 224 | and hasattr(os, "killpg") |
| 225 | ): |
| 226 | try: |
| 227 | pgid = os.getpgid(pid) |
| 228 | logger.info( |
| 229 | f" Sending SIGTERM to Camoufox process group (PGID: {pgid})..." |
| 230 | ) |
| 231 | os.killpg(pgid, signal.SIGTERM) |
| 232 | except ProcessLookupError: |
| 233 | logger.info( |
| 234 | f" Camoufox process group (PID: {pid}) not found, attempting direct termination..." |
| 235 | ) |
| 236 | camoufox_proc.terminate() |
| 237 | else: |
| 238 | if sys.platform == "win32": |
| 239 | logger.info( |
| 240 | f"🔥 [ID-02] Windows Force-Kill Strategy: Using immediate /F /T for process tree (PID: {pid})" |
| 241 | ) |
| 242 | # [ID-02] Enhanced Windows Force-Kill: Immediate /F /T without grace period |
| 243 | result = subprocess.run( |
| 244 | ["taskkill", "/F", "/T", "/PID", str(pid)], |
| 245 | capture_output=True, |
| 246 | text=True, |
| 247 | timeout=5, |
| 248 | ) |
| 249 | if result.returncode == 0: |
| 250 | logger.info( |
| 251 | " ✅ Successfully force-killed Camoufox process tree via taskkill." |
| 252 | ) |
| 253 | else: |
| 254 | logger.warning( |
| 255 | f" ⚠️ Taskkill /F /T returned code {result.returncode}: {result.stderr.strip()}" |
| 256 | ) |
| 257 | # Fallback: try regular terminate |
| 258 | camoufox_proc.terminate() |
| 259 | else: |
| 260 | logger.info(f" Sending SIGTERM to Camoufox (PID: {pid})...") |
| 261 | camoufox_proc.terminate() |
| 262 | camoufox_proc.wait(timeout=5) |
| 263 | logger.info( |
| 264 | f" ✓ Camoufox (PID: {pid}) successfully terminated via SIGTERM." |
| 265 | ) |
| 266 | except subprocess.TimeoutExpired: |
| 267 | logger.warning( |
| 268 | f" ⚠️ Camoufox (PID: {pid}) SIGTERM timed out. Sending SIGKILL to force terminate..." |
| 269 | ) |
| 270 | if ( |
| 271 | sys.platform != "win32" |
no test coverage detected