Commit 7202dfd
Refactor session shutdown from Notify to CancellationToken
Replaces the per-session `Arc<Notify>` shutdown signal with
`tokio_util::sync::CancellationToken`, the canonical primitive for
SDK-internal task coordination in async Rust. Same cooperative-shutdown
semantics, materially better fit for the use case:
- `tonic` uses `CancellationToken` for the equivalent task coordination
case (request cancel propagated into spawned server handlers).
- Parent/child token tree gives us a clean way to expose a public
`Session::cancellation_token()` accessor that returns a child token —
consumers can `select!` on `child.cancelled()` to bind their own work
to the session lifetime, but cancelling the child does NOT shut down
the session itself (child cancel is isolated).
- `Drop` impl just calls `cancel()`, no buffering quirks (Notify's
`notify_one` was already documented as buffer-on-no-listeners; token
cancellation is sticky, which is the simpler mental model).
Plumbing:
- `tokio-util = { version = "0.7", default-features = false }` added as
a runtime dep. No new transitive deps the SDK didn't already pull
through tokio.
- `Session.shutdown` field type changes from `Arc<Notify>` to
`CancellationToken` (which is itself internally `Arc`-backed and
`Clone`).
- `stop_event_loop()` calls `cancel()` instead of `notify_one()`.
- `Drop` impl calls `cancel()` instead of `notify_one()`.
- Event-loop `select!` arm becomes `_ = shutdown.cancelled() => break`.
- New public `Session::cancellation_token()` returning a `child_token()`
— power-user API for binding external tasks to session lifetime.
Tests:
- `cancellation_token_fires_on_session_drop`: dropping the session
fires the child token within 2s.
- `cancellation_token_child_cancel_does_not_kill_session`: cancelling
a child does not propagate up to the parent.
Closes the cancellation idiom gap stephentoub flagged on the SKILL doc.
The framing "drop the future or call abort()" was too absolute — drop-
to-cancel is the right primitive for caller-owned futures, but for
SDK-internal task coordination CancellationToken is the canonical
answer (citations: tokio-util docs, tonic cancellation example,
withoutboats "Asynchronous clean-up", `tokio-graceful-shutdown` crate).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent b907681 commit 7202dfd
5 files changed
Lines changed: 120 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
74 | 81 | | |
75 | 82 | | |
76 | 83 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
98 | 104 | | |
99 | 105 | | |
100 | 106 | | |
| |||
137 | 143 | | |
138 | 144 | | |
139 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
140 | 176 | | |
141 | 177 | | |
142 | 178 | | |
| |||
200 | 236 | | |
201 | 237 | | |
202 | 238 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
207 | 243 | | |
208 | 244 | | |
209 | | - | |
| 245 | + | |
210 | 246 | | |
211 | 247 | | |
212 | 248 | | |
| |||
618 | 654 | | |
619 | 655 | | |
620 | 656 | | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | | - | |
625 | | - | |
626 | | - | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
627 | 664 | | |
628 | 665 | | |
629 | 666 | | |
630 | 667 | | |
631 | | - | |
| 668 | + | |
632 | 669 | | |
633 | 670 | | |
634 | 671 | | |
| |||
824 | 861 | | |
825 | 862 | | |
826 | 863 | | |
827 | | - | |
| 864 | + | |
828 | 865 | | |
829 | 866 | | |
830 | 867 | | |
| |||
927 | 964 | | |
928 | 965 | | |
929 | 966 | | |
930 | | - | |
| 967 | + | |
931 | 968 | | |
932 | 969 | | |
933 | 970 | | |
| |||
986 | 1023 | | |
987 | 1024 | | |
988 | 1025 | | |
989 | | - | |
| 1026 | + | |
990 | 1027 | | |
991 | 1028 | | |
992 | 1029 | | |
| |||
997 | 1034 | | |
998 | 1035 | | |
999 | 1036 | | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
1005 | | - | |
1006 | | - | |
1007 | | - | |
1008 | | - | |
1009 | | - | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
1010 | 1047 | | |
1011 | | - | |
| 1048 | + | |
1012 | 1049 | | |
1013 | 1050 | | |
1014 | 1051 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2052 | 2052 | | |
2053 | 2053 | | |
2054 | 2054 | | |
| 2055 | + | |
| 2056 | + | |
| 2057 | + | |
| 2058 | + | |
| 2059 | + | |
| 2060 | + | |
| 2061 | + | |
| 2062 | + | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
| 2066 | + | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
| 2081 | + | |
| 2082 | + | |
| 2083 | + | |
| 2084 | + | |
| 2085 | + | |
| 2086 | + | |
| 2087 | + | |
| 2088 | + | |
| 2089 | + | |
| 2090 | + | |
| 2091 | + | |
| 2092 | + | |
| 2093 | + | |
2055 | 2094 | | |
2056 | 2095 | | |
2057 | 2096 | | |
| |||
0 commit comments