Commit 9adb0f9
authored
Track whether there are any async tasks in a store (#13246)
* Track whether there are any async tasks in a store
This commit adds a new API to `Accessor` to learn whether there are any
async tasks that are "interesting" in a store. This is borne out of many
discussions over the past few weeks amongst a number of folks where the
basic problem is that for WASIp3 HTTP guests there's no way right now to
send a signal of "keep me alive after I send my response" because that's
post-return-style work. In WASIp2 this was modeled where the outparam
that was passed in was set, and then work was done before returning.
With WASIp3, however, a response is sent by returning which means that
this is no longer possible.
The general idea is that Wasmtime will now consider all tasks which have
not yet exited as "interesting". These tasks in theory mean that the
store should be kept alive as the guest still has work to complete. Note
that tasks first return, and then exit, and they're still considered
interesting after returning before they exit. In the future it's
expected that the component model might have an ABI option to say "this
task, by default, isn't interesting" with an opt-in method at runtime of
saying "ok wait but yes this is interesting". In this manner it's
expected that not all tasks for all of time will be considered
interesting, only those that are intentionally opted-in to being
interesting (and today that just so happens to be all of them).
The API itself on `Accessor` is a `poll_*`-style function which enables
both checking to see if there are no more tasks remaining as well as
registering a callback to get notified when there are actually no more
tasks remaining. In this manner HTTP servers can await this as one of
the events that concludes when a guest is finished processing.
The implementation of this API required some refactoring of
`concurrent.rs`. Namely I wanted to have a relatively "narrow waist"
through which increments/decrements of this internal counter happened to
ensure that nothing was forgot. This is a bit non-trivial because there
are a number of situations which means that a task is "exited", such as:
* A synchronously lifted function returns.
* An asynchronously lifted function returns `CALLBACK_CODE_EXIT`.
* An asynchronously lifted function is stuck in the `STARTING` state,
and then gets cancelled.
* A task is running, is then cancelled, acknowledges the cancellation,
and then exits.
* A task with threads exits without actually returning, and then one of
its threads returns for it.
For now the logic of "are interesting tasks done" is now hooked into
`cleanup_thread`. This will likely require adjusting in the future for
threads because in this last bullet the `task.return` invocation is the
point where all conditions are met (main thread exited, task returned)
as opposed to when the thread itself exits. For now though this is
intended to be as close an approximation as possible.
Throughout this implementation I've performed a number of refactorings
to reduce duplication, shuffle where methods are implemented, reduce
some verbosity, etc. There are debug assertions in place for if this
predicate ever goes wrong, which is intended to assist during
development.
* Include instances in the graceful shutdown of `wasmtime serve`
Now that their lifetime is extended they need to be included in this
calculation.1 parent 1551750 commit 9adb0f9
10 files changed
Lines changed: 566 additions & 313 deletions
File tree
- crates
- test-programs/src/bin
- wasi-http/src
- wasmtime/src/runtime
- component
- func
- vm/component
- src/commands
- tests
- all
- misc_testsuite/component-model/async
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
350 | 350 | | |
351 | 351 | | |
352 | 352 | | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
353 | 360 | | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
412 | 388 | | |
413 | 389 | | |
414 | 390 | | |
415 | | - | |
416 | 391 | | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
432 | 407 | | |
433 | 408 | | |
434 | | - | |
435 | 409 | | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | 410 | | |
440 | | - | |
441 | | - | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
442 | 436 | | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
443 | 449 | | |
444 | | - | |
445 | | - | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
446 | 510 | | |
447 | | - | |
| 511 | + | |
| 512 | + | |
448 | 513 | | |
449 | 514 | | |
450 | 515 | | |
| |||
0 commit comments