Skip to content

Commit 9ff90fd

Browse files
authored
CABI: don't trap if the last thread of a task exits before the task is resolved (#767)
Resolves #763
1 parent 9f64125 commit 9ff90fd

7 files changed

Lines changed: 124 additions & 57 deletions

File tree

‎design/mvp/CanonicalABI.md‎

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ class Task:
554554
state: State
555555
num_borrows: int
556556
implicit_thread: Optional[Thread]
557-
threads: list[Thread]
558557

559558
def __init__(self, ft, opts, inst, on_start, on_resolve):
560559
self.ft = ft
@@ -565,7 +564,6 @@ class Task:
565564
self.state = Task.State.INITIAL
566565
self.num_borrows = 0
567566
self.implicit_thread = None
568-
self.threads = []
569567
```
570568

571569
The `Task.needs_exclusive` predicate returns whether this task's implicit thread
@@ -627,14 +625,8 @@ exports.
627625
if self.needs_exclusive():
628626
assert(self.inst.exclusive_thread is None)
629627
self.inst.exclusive_thread = self.implicit_thread
630-
self.register_thread(self.implicit_thread)
628+
self.implicit_thread.index = self.inst.threads.add(self.implicit_thread)
631629
return True
632-
633-
def register_thread(self, thread):
634-
assert(thread not in self.threads and thread.task is self)
635-
self.threads.append(thread)
636-
assert(thread.index is None)
637-
thread.index = self.inst.threads.add(thread)
638630
```
639631
Since the order in which suspended threads are resumed is nondeterministic (see
640632
`Store.tick` below), once `Task.enter_implicit_thread` suspends the task's
@@ -649,32 +641,26 @@ above definition ensures the following properties:
649641
thundering herd of pending tasks).
650642

651643
Once a task's implicit thread has cleared the backpressure gate, it is added to
652-
the lists of threads running inside the current task and component instance by
653-
`Task.register_thread()` (which is also called by `thread.new-indirect`, below).
644+
the component instance's `threads` table (just like the explicit threads created
645+
by `thread.new-indirect`, below).
654646

655647
Symmetrically, the `Task.exit_implicit_thread` method is called before a task's
656-
implicit thread returns to reverse the effects of `Task.enter_implicit_thread`.
657-
In particular, if the `exclusive_thread` lock was acquired, it is released.
658-
`Task.unregister_thread` (which is also called by `thread.new-indirect`, below)
659-
traps if the task's last thread is unregistered and the task has not yet
660-
returned a value to its caller.
648+
implicit thread returns to reverse the effects of `Task.enter_implicit_thread`,
649+
removing the thread from the `threads` table and, if the `exclusive_thread` lock
650+
was acquired, releasing it.
661651
```python
662652
def exit_implicit_thread(self):
663653
assert(current_thread() is self.implicit_thread)
664-
self.unregister_thread(self.implicit_thread)
654+
self.inst.threads.remove(self.implicit_thread.index)
665655
if self.ft.async_ and self.needs_exclusive():
666656
assert(self.inst.exclusive_thread is self.implicit_thread)
667657
self.inst.exclusive_thread = None
668-
669-
def unregister_thread(self, thread):
670-
assert(thread in self.threads and thread.task is self)
671-
self.threads.remove(thread)
672-
if len(self.threads) == 0:
673-
trap_if(self.state != Task.State.RESOLVED)
674-
assert(self.num_borrows == 0)
675-
assert(thread.index is not None)
676-
self.inst.threads.remove(thread.index)
677658
```
659+
Note that there is no trap when a task's implicit thread (or any other thread)
660+
exits before the task has resolved (by calling `task.return` or `task.cancel`),
661+
even if it is the task's last thread. In that case, the task will never
662+
resolve, which, from the caller's perspective, is indistinguishable from the
663+
case where the task's threads are all blocked forever.
678664

679665
The `Task.request_cancellation` method implements the `OnCancel` callback that
680666
is called by the `subtask.cancel` built-in. If a task's implicit thread is
@@ -3465,7 +3451,8 @@ In both of the `async` cases below (with or without `callback`), the
34653451
`task.return` built-in must be called, providing the return value as core wasm
34663452
*parameters* to the `task.return` built-in (rather than as core function
34673453
results as in the synchronous case). If `task.return` is *not* called by the
3468-
time the `Task`'s last `Thread` exits, there is a trap (in `Task.unregister_thread`).
3454+
time the `Task`'s last `Thread` exits, the task never resolves (as described
3455+
in `Task.exit_implicit_thread` above).
34693456

34703457
In the `async` non-`callback` ("stackful async") case, there is a single call
34713458
to the core wasm callee which must return empty core results. Waiting for async
@@ -4621,10 +4608,10 @@ def canon_thread_new_indirect(ft, ftbl: Table[CoreFuncRef], fi, c):
46214608
return [-1]
46224609
def thread_func():
46234610
[] = call_and_trap_on_throw(f.callee, [c])
4624-
task.unregister_thread(new_thread)
4611+
task.inst.threads.remove(new_thread.index)
46254612
new_thread = Thread(task, thread_func)
46264613
assert(new_thread.suspended())
4627-
task.register_thread(new_thread)
4614+
new_thread.index = task.inst.threads.add(new_thread)
46284615
return [new_thread.index]
46294616
```
46304617
The newly-created thread starts out in a "suspended" state and so, to

‎design/mvp/Concurrency.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,12 @@ the "started" state.
729729
The way an `async` export returns its value using the async ABI is by calling
730730
[`task.return`], passing the core values that are to be lifted as *parameters*.
731731
When using the async ABI, *any* of the threads contained by a task can call
732-
`task.return`; there is no "main thread" of a task. When the last thread of a
733-
task returns, there is a trap if `task.return` has not been called. Thus, *some*
734-
thread (either the thread created implicitly for the initial export call or some
735-
thread transitively created by that thread) must call `task.return`.
732+
`task.return`; there is no "main thread" of a task. However, *some* thread
733+
(either the thread created implicitly for the initial export call or some
734+
thread transitively created by that thread) must call `task.return` for the
735+
caller to receive a result. If the last thread of a task exits before
736+
`task.return` has been called, the task simply never returns, as if its threads
737+
blocked forever on some never-met condition.
736738

737739
Returning values by calling `task.return` allows a task to continue executing
738740
even after it has passed its initial results to the caller. This is also

‎design/mvp/Text.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,8 +1712,8 @@ For details, see [Backpressure] in the concurrency explainer and
17121712
| Canonical ABI signature | `[lower(FuncT.results)*] -> []` |
17131713

17141714
The `task.return` built-in takes as parameters the result values of the
1715-
[current task]. One of `task.return` or `task.cancel` must be called exactly
1716-
once from any of a task's threads.
1715+
[current task]. At most one call to `task.return` or `task.cancel` may be made
1716+
from any of a task's threads; if neither is called, the task never resolves.
17171717

17181718
The `canon task.return` definition takes component-level return type and the
17191719
list of `canonopt` to be used to lift the return value. When called, the

‎design/mvp/canonical-abi/definitions.py‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ class State(Enum):
405405
state: State
406406
num_borrows: int
407407
implicit_thread: Optional[Thread]
408-
threads: list[Thread]
409408

410409
def __init__(self, ft, opts, inst, on_start, on_resolve):
411410
self.ft = ft
@@ -416,7 +415,6 @@ def __init__(self, ft, opts, inst, on_start, on_resolve):
416415
self.state = Task.State.INITIAL
417416
self.num_borrows = 0
418417
self.implicit_thread = None
419-
self.threads = []
420418

421419
def needs_exclusive(self):
422420
assert(self.ft.async_)
@@ -439,31 +437,16 @@ def has_backpressure():
439437
if self.needs_exclusive():
440438
assert(self.inst.exclusive_thread is None)
441439
self.inst.exclusive_thread = self.implicit_thread
442-
self.register_thread(self.implicit_thread)
440+
self.implicit_thread.index = self.inst.threads.add(self.implicit_thread)
443441
return True
444442

445-
def register_thread(self, thread):
446-
assert(thread not in self.threads and thread.task is self)
447-
self.threads.append(thread)
448-
assert(thread.index is None)
449-
thread.index = self.inst.threads.add(thread)
450-
451443
def exit_implicit_thread(self):
452444
assert(current_thread() is self.implicit_thread)
453-
self.unregister_thread(self.implicit_thread)
445+
self.inst.threads.remove(self.implicit_thread.index)
454446
if self.ft.async_ and self.needs_exclusive():
455447
assert(self.inst.exclusive_thread is self.implicit_thread)
456448
self.inst.exclusive_thread = None
457449

458-
def unregister_thread(self, thread):
459-
assert(thread in self.threads and thread.task is self)
460-
self.threads.remove(thread)
461-
if len(self.threads) == 0:
462-
trap_if(self.state != Task.State.RESOLVED)
463-
assert(self.num_borrows == 0)
464-
assert(thread.index is not None)
465-
self.inst.threads.remove(thread.index)
466-
467450
def request_cancellation(self):
468451
if self.state == Task.State.INITIAL:
469452
self.state = Task.State.PENDING_CANCEL
@@ -2625,10 +2608,10 @@ def canon_thread_new_indirect(ft, ftbl: Table[CoreFuncRef], fi, c):
26252608
return [-1]
26262609
def thread_func():
26272610
[] = call_and_trap_on_throw(f.callee, [c])
2628-
task.unregister_thread(new_thread)
2611+
task.inst.threads.remove(new_thread.index)
26292612
new_thread = Thread(task, thread_func)
26302613
assert(new_thread.suspended())
2631-
task.register_thread(new_thread)
2614+
new_thread.index = task.inst.threads.add(new_thread)
26322615
return [new_thread.index]
26332616

26342617
### 🧵 `canon thread.resume-later`

‎test/async/during-sync-call-exclusive-resume.wast‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@
103103
(assert_trap (invoke "sync-block") "cannot block a synchronous task before returning")
104104

105105
;; Same as above, except the async callback task has *not* resolved when its
106-
;; callback returns EXIT during the sync call, which traps.
106+
;; callback returns EXIT during the sync call. Exiting without having resolved
107+
;; does not itself trap (the task is simply left unresolved), so the outcome is
108+
;; the same as above: once no ready threads remain, the sync call traps.
107109
(component
108110
(component $C
109111
(core module $Core
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
;; When the last thread of an async-typed task exits before the task has
2+
;; resolved (via task.return or task.cancel), there is no trap at the point of
3+
;; exit: the task is simply left unresolved forever, just as if its threads
4+
;; were all blocked forever. Whether this leads to a trap depends on whether
5+
;; anyone waits for the task to resolve.
6+
(component definition $Tester
7+
(component $C
8+
(core module $Core
9+
(func (export "exit") (result i32)
10+
(i32.const 0 (; EXIT ;)))
11+
(func (export "unreachable-cb") (param i32 i32 i32) (result i32)
12+
unreachable)
13+
)
14+
(core instance $core (instantiate $Core))
15+
(func (export "exit") async
16+
(canon lift (core func $core "exit") async (callback (core func $core "unreachable-cb"))))
17+
)
18+
(component $D
19+
(import "c" (instance $c
20+
(export "exit" (func async))
21+
))
22+
(core module $Core
23+
(import "" "sync-exit" (func $sync-exit))
24+
(import "" "async-exit" (func $async-exit (result i32)))
25+
(import "" "waitable-set.new" (func $waitable-set.new (result i32)))
26+
(import "" "waitable.join" (func $waitable.join (param i32 i32)))
27+
28+
;; non-async-typed: synchronously wait for a subtask that will never
29+
;; resolve
30+
(func (export "sync-typed-wait") (result i32)
31+
(call $sync-exit)
32+
unreachable)
33+
34+
;; async-typed: start a subtask that runs until its thread exits, leaving
35+
;; the subtask unresolved, and then WAIT for the subtask to resolve
36+
(func (export "async-typed-wait") (result i32)
37+
(local $ret i32) (local $ws i32)
38+
(local.set $ret (call $async-exit))
39+
(if (i32.ne (i32.and (local.get $ret) (i32.const 0xf)) (i32.const 1 (; STARTED ;)))
40+
(then unreachable))
41+
(local.set $ws (call $waitable-set.new))
42+
(call $waitable.join (i32.shr_u (local.get $ret) (i32.const 4)) (local.get $ws))
43+
(i32.or (i32.const 2 (; WAIT ;)) (i32.shl (local.get $ws) (i32.const 4))))
44+
(func (export "unreachable-cb") (param i32 i32 i32) (result i32)
45+
unreachable)
46+
47+
;; non-async-typed: start a subtask that runs until its thread exits,
48+
;; leaving the subtask unresolved, and then return a value anyway
49+
(func (export "no-wait") (result i32)
50+
(if (i32.ne (i32.and (call $async-exit) (i32.const 0xf)) (i32.const 1 (; STARTED ;)))
51+
(then unreachable))
52+
(i32.const 42))
53+
)
54+
(canon lower (func $c "exit") (core func $sync-exit))
55+
(canon lower (func $c "exit") async (core func $async-exit))
56+
(canon waitable-set.new (core func $waitable-set.new))
57+
(canon waitable.join (core func $waitable.join))
58+
(core instance $core (instantiate $Core (with "" (instance
59+
(export "sync-exit" (func $sync-exit))
60+
(export "async-exit" (func $async-exit))
61+
(export "waitable-set.new" (func $waitable-set.new))
62+
(export "waitable.join" (func $waitable.join))
63+
))))
64+
(func (export "sync-typed-wait") (result u32)
65+
(canon lift (core func $core "sync-typed-wait")))
66+
(func (export "async-typed-wait") async (result u32)
67+
(canon lift (core func $core "async-typed-wait") async (callback (core func $core "unreachable-cb"))))
68+
(func (export "no-wait") (result u32)
69+
(canon lift (core func $core "no-wait")))
70+
)
71+
(instance $c (instantiate $C))
72+
(instance $d (instantiate $D (with "c" (instance $c))))
73+
(func (export "sync-typed-wait") (alias export $d "sync-typed-wait"))
74+
(func (export "async-typed-wait") (alias export $d "async-typed-wait"))
75+
(func (export "no-wait") (alias export $d "no-wait"))
76+
)
77+
78+
;; A non-async-typed caller waiting on a subtask whose threads have all exited
79+
;; traps once no threads can run.
80+
(component instance $i $Tester)
81+
(assert_trap (invoke "sync-typed-wait") "cannot block a synchronous task before returning")
82+
83+
;; An async-typed caller waiting on a subtask whose threads have all exited
84+
;; deadlocks, since nothing can ever wake it.
85+
(component instance $i $Tester)
86+
(assert_trap (invoke "async-typed-wait") "deadlock detected: event loop cannot make further progress")
87+
88+
;; A caller that doesn't wait on the unresolved subtask returns its value
89+
;; without trapping and can even be reentered.
90+
(component instance $i $Tester)
91+
(assert_return (invoke "no-wait") (u32.const 42))
92+
(assert_return (invoke "no-wait") (u32.const 42))

‎test/nyi.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
./values/post-return.wast
44
./async/big-interleaving-test.wast
55
./async/forward.wast
6+
./async/exit-without-resolving.wast

0 commit comments

Comments
 (0)