Commit 1389b63
Fix race condition on SandboxService.waiters (#1289)
This PR fixes #1277.
`SandboxService.waiters` had a consistency issue (not exactly race).
`SandboxService.wait` XPC can be executed on arbitrary `id`, and it will
hang forever if no other handler resumes it. Without knowing this
internal, the high level entity can run into this issue, and deadlock.
This PR simplifies the mental model: **`SandboxService.waiters[id]:
ExitWaiter(continuations, exitCode)` can only be in three states: i)
non-existing, ii) existing with nil `exitCode`, and iii) existing with
concrete `exitCode`.**
**If it is non-existing, no handler has been registered to resume it
later. If existing with nil `exitCode`, It is guaranteed the registered
`continuations` will be resumed later with a concrete `exitCode`.
Finally, if already a concrete `exitCode`, a handler has been
registered, and already resumed (with that `exitCode`).**
Thus, `SandboxService.wait` should return immediately if `waiters[id]`
is non-existing or existing with a concrete `exitCode` (as no handler
will resume it later). It should only block when `waiters[id]` is
existing with nil `exitCode` as it is guaranteed to be resumed later. By
doing so, we can guarantee there is no deadlock at all.
For that this PR does followings:
1. Introduce `ExitMonitor` class to updates `continuations` and
`exitCode` all together atomically. Initially, `state` variable saved
the `exitCode`, but it cannot be tied with `continuations` as they are
protected by different primitives (i.e., lock and actor).
2. Gather `waiters` related operations into a single actor method,
guaranteeing those are performed atomically under actor
protection---i.e., we actually don't need Mutex here.
3. Ensure initialized `waiters` are released (i.e., resumed) later
(under any possible circumstances).
4. Move `process.wait` after `process.start` in `io.handleProcess` to
run `SandboxService.wait` only after the `waiters[id]` is initialized.
By doing fourth step, we can guarantee `SandboxService.wait` can meet
only one of two following `ExitMonitor` state: i) existing with nil
`exitCode`, or ii) existing with concrete `exitCode` (in case the
process exited too early). In both cases, `exitCode` is preserved and
returned.
## Type of Change
- [X] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
## Motivation and Context
[Why is this change needed?]
## Testing
- [X] Tested locally
- [ ] Added/updated tests
- [ ] Added/updated docs1 parent 01a05eb commit 1389b63
3 files changed
Lines changed: 93 additions & 93 deletions
File tree
- Sources/Services
- ContainerAPIService/Client
- ContainerSandboxService/Server
- Tests/CLITests/Subcommands/Run
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
| |||
160 | 163 | | |
161 | 164 | | |
162 | 165 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
| |||
Lines changed: 75 additions & 90 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
57 | 78 | | |
58 | 79 | | |
59 | 80 | | |
| |||
225 | 246 | | |
226 | 247 | | |
227 | 248 | | |
| 249 | + | |
| 250 | + | |
228 | 251 | | |
229 | 252 | | |
230 | 253 | | |
| |||
233 | 256 | | |
234 | 257 | | |
235 | 258 | | |
236 | | - | |
| 259 | + | |
237 | 260 | | |
238 | 261 | | |
239 | 262 | | |
| |||
320 | 343 | | |
321 | 344 | | |
322 | 345 | | |
323 | | - | |
| 346 | + | |
324 | 347 | | |
325 | 348 | | |
326 | 349 | | |
| |||
360 | 383 | | |
361 | 384 | | |
362 | 385 | | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
377 | 398 | | |
| 399 | + | |
| 400 | + | |
378 | 401 | | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
384 | 407 | | |
385 | 408 | | |
386 | 409 | | |
| |||
410 | 433 | | |
411 | 434 | | |
412 | 435 | | |
413 | | - | |
| 436 | + | |
414 | 437 | | |
415 | 438 | | |
416 | 439 | | |
| |||
464 | 487 | | |
465 | 488 | | |
466 | 489 | | |
467 | | - | |
| 490 | + | |
468 | 491 | | |
469 | 492 | | |
470 | 493 | | |
471 | 494 | | |
472 | 495 | | |
473 | 496 | | |
474 | | - | |
| 497 | + | |
475 | 498 | | |
476 | 499 | | |
477 | 500 | | |
| |||
596 | 619 | | |
597 | 620 | | |
598 | 621 | | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | | - | |
605 | | - | |
606 | | - | |
607 | | - | |
608 | | - | |
609 | | - | |
610 | | - | |
611 | | - | |
612 | | - | |
613 | | - | |
614 | | - | |
615 | | - | |
616 | | - | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | 622 | | |
629 | 623 | | |
630 | | - | |
631 | | - | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
632 | 627 | | |
633 | 628 | | |
634 | 629 | | |
| |||
703 | 698 | | |
704 | 699 | | |
705 | 700 | | |
706 | | - | |
| 701 | + | |
707 | 702 | | |
708 | 703 | | |
709 | 704 | | |
| |||
828 | 823 | | |
829 | 824 | | |
830 | 825 | | |
831 | | - | |
| 826 | + | |
832 | 827 | | |
833 | 828 | | |
834 | 829 | | |
| |||
839 | 834 | | |
840 | 835 | | |
841 | 836 | | |
842 | | - | |
| 837 | + | |
843 | 838 | | |
844 | 839 | | |
845 | 840 | | |
| |||
1077 | 1072 | | |
1078 | 1073 | | |
1079 | 1074 | | |
1080 | | - | |
1081 | | - | |
1082 | | - | |
1083 | | - | |
1084 | | - | |
1085 | | - | |
1086 | | - | |
1087 | | - | |
| 1075 | + | |
1088 | 1076 | | |
1089 | 1077 | | |
1090 | 1078 | | |
| |||
1304 | 1292 | | |
1305 | 1293 | | |
1306 | 1294 | | |
1307 | | - | |
1308 | | - | |
1309 | | - | |
1310 | | - | |
1311 | | - | |
1312 | | - | |
1313 | | - | |
1314 | | - | |
1315 | | - | |
1316 | | - | |
1317 | | - | |
1318 | | - | |
1319 | | - | |
1320 | | - | |
1321 | | - | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
1322 | 1298 | | |
| 1299 | + | |
1323 | 1300 | | |
1324 | 1301 | | |
1325 | | - | |
1326 | | - | |
1327 | | - | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
1328 | 1306 | | |
1329 | | - | |
1330 | 1307 | | |
1331 | | - | |
1332 | | - | |
1333 | | - | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
1334 | 1311 | | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
1335 | 1320 | | |
1336 | 1321 | | |
1337 | 1322 | | |
| |||
1387 | 1372 | | |
1388 | 1373 | | |
1389 | 1374 | | |
1390 | | - | |
| 1375 | + | |
1391 | 1376 | | |
1392 | 1377 | | |
1393 | 1378 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
126 | 141 | | |
0 commit comments