Commit 80f8c48
committed
bug #61861 [Console] Ensure terminal is usable after termination signal (johnstevenson)
This PR was squashed before being merged into the 6.4 branch.
Discussion
----------
[Console] Ensure terminal is usable after termination signal
| Q | A
| ------------- | ---
| Branch? | 6.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | Fix #61732
| License | MIT
This PR introduces a new helper `TerminalInputHelper` that restores the terminal to its original state after it has been modified when reading using input. This ensures that the terminal is not broken when a terminating signal, like Ctrl-C, is received.
QuestionHelper.php disables `icanon` and `echo` when accepting input from a selection, and disables `echo` when accepting hidden input. If these are not restored before termination, the user's terminal can end up in a broken state.
Usage:
```php
$inputHelper = new TerminalInputHelper($inputStream);
// Change terminal settings then wait for input before all input reads
$inputHelper->waitForInput();
// Read the input then call finish to restore terminal settings and signal handlers
$inputHelper->finish()
```
The helper creates its own signal handlers (for `SIGINT`, `SIGQUIT`, and `SIGTERM`) that restore the original terminal settings then call any original handler callback. If the original handler callback does not terminate the process then the current terminal settings are restored.
If there is no original signal handler callback and the signal's disposition is set to the default action (`SIG_DFL`), then that action is invoked by a `posix_kill` call.
The `finish` method restores the terminal settings and replaces the new signal handlers with the original ones.
Commits
-------
fe75d75d22e [Console] Ensure terminal is usable after termination signalFile tree
6 files changed
+230
-28
lines changed- Helper
- Tests
- Fixtures
6 files changed
+230
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1018 | 1018 | | |
1019 | 1019 | | |
1020 | 1020 | | |
1021 | | - | |
1022 | | - | |
1023 | | - | |
1024 | | - | |
1025 | | - | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
1029 | 1021 | | |
1030 | 1022 | | |
1031 | 1023 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
258 | 258 | | |
259 | 259 | | |
260 | 260 | | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
| 261 | + | |
266 | 262 | | |
267 | 263 | | |
268 | 264 | | |
| |||
272 | 268 | | |
273 | 269 | | |
274 | 270 | | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
| 271 | + | |
279 | 272 | | |
280 | 273 | | |
281 | 274 | | |
282 | 275 | | |
283 | | - | |
| 276 | + | |
| 277 | + | |
284 | 278 | | |
285 | 279 | | |
286 | 280 | | |
| |||
382 | 376 | | |
383 | 377 | | |
384 | 378 | | |
385 | | - | |
386 | | - | |
| 379 | + | |
| 380 | + | |
387 | 381 | | |
388 | 382 | | |
389 | 383 | | |
| |||
434 | 428 | | |
435 | 429 | | |
436 | 430 | | |
| 431 | + | |
| 432 | + | |
437 | 433 | | |
438 | | - | |
| 434 | + | |
439 | 435 | | |
440 | 436 | | |
441 | 437 | | |
442 | 438 | | |
443 | 439 | | |
| 440 | + | |
| 441 | + | |
444 | 442 | | |
445 | 443 | | |
446 | 444 | | |
447 | 445 | | |
448 | 446 | | |
449 | 447 | | |
450 | 448 | | |
451 | | - | |
452 | | - | |
453 | | - | |
| 449 | + | |
| 450 | + | |
454 | 451 | | |
455 | 452 | | |
456 | 453 | | |
| |||
| 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 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2198 | 2198 | | |
2199 | 2199 | | |
2200 | 2200 | | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
| 2214 | + | |
| 2215 | + | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
2201 | 2226 | | |
2202 | 2227 | | |
2203 | 2228 | | |
| |||
2209 | 2234 | | |
2210 | 2235 | | |
2211 | 2236 | | |
2212 | | - | |
| 2237 | + | |
| 2238 | + | |
2213 | 2239 | | |
2214 | 2240 | | |
2215 | 2241 | | |
2216 | 2242 | | |
2217 | | - | |
| 2243 | + | |
2218 | 2244 | | |
2219 | 2245 | | |
2220 | 2246 | | |
2221 | 2247 | | |
2222 | | - | |
| 2248 | + | |
2223 | 2249 | | |
2224 | 2250 | | |
2225 | 2251 | | |
2226 | 2252 | | |
2227 | 2253 | | |
| 2254 | + | |
| 2255 | + | |
| 2256 | + | |
| 2257 | + | |
| 2258 | + | |
| 2259 | + | |
2228 | 2260 | | |
2229 | 2261 | | |
2230 | 2262 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
| 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 | + | |
0 commit comments