Commit a3ea63c
committed
gh-154535: Fix crash when a contextvars.Context iterator is shared between threads
The HAMT iterator that backs iteration over a contextvars.Context keeps its
entire depth-first cursor -- i_level, i_pos and the borrowed node pointers in
i_nodes -- inside the iterator object, and advanced it with plain reads and
writes. Two threads calling next() on the same iterator interleaved those
updates, leaving i_level pointing at an i_nodes slot that was never filled in,
so the next step dereferenced a stale or NULL node and crashed.
Advance the cursor with the iterator locked, so the whole descent is atomic.
The HAMT itself is immutable, so no other locking is required. As with the
other free-threaded iterators, concurrent iteration may still skip or repeat
items, but it no longer crashes.1 parent eef2349 commit a3ea63c
4 files changed
Lines changed: 77 additions & 4 deletions
File tree
- Include/internal
- Lib/test/test_free_threading
- Misc/NEWS.d/next/Core_and_Builtins
- Python
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
49 | 54 | | |
50 | 55 | | |
51 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
115 | 154 | | |
116 | 155 | | |
117 | 156 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
2474 | 2475 | | |
2475 | 2476 | | |
2476 | 2477 | | |
2477 | | - | |
2478 | | - | |
2479 | | - | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
| 2481 | + | |
| 2482 | + | |
| 2483 | + | |
| 2484 | + | |
| 2485 | + | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
| 2489 | + | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
2480 | 2500 | | |
2481 | 2501 | | |
2482 | 2502 | | |
2483 | 2503 | | |
2484 | 2504 | | |
2485 | 2505 | | |
2486 | 2506 | | |
2487 | | - | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
2488 | 2511 | | |
2489 | 2512 | | |
2490 | 2513 | | |
| |||
0 commit comments