Commit 195e85a
committed
fix: Implement production-ready shutdown timeout system
Fixes indefinite hangs during graceful shutdown (Ctrl+C) by implementing a
three-tier timeout strategy that ensures the application always exits within
10 seconds, even when printers are unresponsive or HTTP connections are stuck.
**Changes:**
- **New timeout utility** (src/utils/ShutdownTimeout.ts):
- TimeoutError class for timeout failures
- withTimeout() wrapper for promises with timeout enforcement
- createHardDeadline() for absolute maximum shutdown time
- **ConnectionFlowManager.disconnectContext()**:
- Added 5s timeout wrapper (configurable)
- On timeout: forces cleanup (removes context, marks disconnected)
- Backward compatible with existing callers
- **WebUIManager.stop()**:
- Added 3s timeout wrapper (configurable)
- On timeout: calls closeAllConnections() to force-close stuck HTTP connections
- Added isStopping guard flag to prevent concurrent stop calls
- Added timing information logging
- **Main shutdown logic** (src/index.ts):
- Replaced sequential disconnect loop with parallel Promise.allSettled()
- All printers disconnect concurrently instead of one-by-one
- Added 10s hard deadline that calls process.exit(1) if exceeded
- Added step-by-step logging with clear progress indicators
- Preserved second Ctrl+C force-exit as safety net
**Benefits:**
- Single printer: < 5s shutdown
- Multiple printers: < 10s (parallelized)
- Hard deadline: Always exits within 10s maximum
- Second Ctrl+C: Immediate force exit
- Clear logging: Step-by-step progress visible
**Timeout hierarchy:**
```
HARD DEADLINE (10s) ─────────────────────────────────┐
├── Stop polling (immediate) │
├── Parallel disconnects (5s each, concurrent) ─┤
└── WebUI server stop (3s, force close if timeout)─┘
```
All changes are backward compatible - optional timeout parameters have
sensible defaults and existing callers work without modification.1 parent 8a31cbc commit 195e85a
4 files changed
Lines changed: 278 additions & 64 deletions
File tree
- src
- managers
- utils
- webui/server
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
49 | 69 | | |
50 | 70 | | |
51 | 71 | | |
| |||
247 | 267 | | |
248 | 268 | | |
249 | 269 | | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
250 | 274 | | |
| 275 | + | |
251 | 276 | | |
252 | 277 | | |
253 | 278 | | |
| |||
283 | 308 | | |
284 | 309 | | |
285 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
286 | 319 | | |
287 | 320 | | |
288 | 321 | | |
289 | 322 | | |
290 | 323 | | |
291 | 324 | | |
292 | | - | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
293 | 330 | | |
294 | 331 | | |
295 | | - | |
| 332 | + | |
| 333 | + | |
296 | 334 | | |
297 | | - | |
| 335 | + | |
298 | 336 | | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
307 | 365 | | |
308 | 366 | | |
309 | | - | |
310 | | - | |
311 | | - | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
312 | 376 | | |
313 | | - | |
314 | 377 | | |
315 | | - | |
| 378 | + | |
| 379 | + | |
316 | 380 | | |
317 | 381 | | |
318 | 382 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
451 | 452 | | |
452 | 453 | | |
453 | 454 | | |
454 | | - | |
| 455 | + | |
455 | 456 | | |
456 | 457 | | |
457 | 458 | | |
| |||
461 | 462 | | |
462 | 463 | | |
463 | 464 | | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
469 | 486 | | |
470 | | - | |
471 | | - | |
472 | | - | |
| 487 | + | |
| 488 | + | |
473 | 489 | | |
474 | | - | |
475 | | - | |
| 490 | + | |
| 491 | + | |
476 | 492 | | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
482 | 497 | | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | 498 | | |
494 | | - | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
495 | 507 | | |
496 | 508 | | |
497 | 509 | | |
| |||
| 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 | + | |
0 commit comments