Commit 3082c91
Snapshot cancelling() before pending-drain to distinguish outer cancels
The pending-drain block in _connect_impl checked ``cancelling() > 0``
post-await to decide whether the CancelledError came from our own
``pending.cancel()`` or from an outer ``task.cancel()`` that propagated
through our fut_waiter. The premise is sound — our pending.cancel()
does NOT increment our task's cancelling counter — but the test is
not function-scoped: ``Task.cancelling()`` is the cumulative count of
cancels delivered to this task across its entire lifetime, modulo
``uncancel()``. Any prior code path on the same task that consumed a
CancelledError without ``uncancel()`` leaves the counter non-zero on
entry to _connect_impl, and the post-await ``> 0`` check fires
unconditionally — re-raising our OWN consumed cancel as if an outer
one had landed.
Snapshot ``cancelling()`` BEFORE calling pending.cancel() and compare
the post-await counter against the snapshot. Only a positive delta
indicates a fresh outer cancel landed during ``await pending``; a
stable counter means the CancelledError came from our own cancel and
must be consumed so connect() can proceed. This is the canonical
"snapshot-and-delta" pattern asyncio.timeout itself uses internally.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent e0e67ad commit 3082c91
1 file changed
Lines changed: 26 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1017 | 1017 | | |
1018 | 1018 | | |
1019 | 1019 | | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
1020 | 1034 | | |
1021 | 1035 | | |
1022 | 1036 | | |
1023 | 1037 | | |
1024 | 1038 | | |
1025 | | - | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
1031 | | - | |
1032 | | - | |
1033 | | - | |
1034 | | - | |
1035 | | - | |
1036 | | - | |
1037 | | - | |
1038 | | - | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
1039 | 1044 | | |
1040 | 1045 | | |
1041 | 1046 | | |
1042 | | - | |
1043 | | - | |
1044 | | - | |
1045 | | - | |
1046 | | - | |
1047 | | - | |
1048 | | - | |
1049 | | - | |
1050 | | - | |
1051 | | - | |
1052 | | - | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
1053 | 1054 | | |
1054 | 1055 | | |
1055 | 1056 | | |
| |||
0 commit comments