Repository navigation
Commit 259ce8c
authored
Fix the itertools.tee leak and race, the contextvars and generator races, _asyncio's InvalidStateError lookup, and _imp's frozen data (#8518)
* _imp: read get_frozen_object's data as a whole marshal value
The data argument went through deserialize_code(), which reads a code body
without the type byte in front of it, so nothing marshal.dumps() produces was
accepted. Read it with marshal.loads() and require a code object back.
Assisted-by: Claude
* _imp: bind find_frozen's arguments with FromArgs
The arity check was done by hand on a FuncArgs. Take the arguments through a
FromArgs struct instead, which makes withdata keyword-only, and fill in the
data it asks for: the frozen encoding is not marshal, so the code is
re-serialized into what get_frozen_object() reads back.
Assisted-by: Claude
* itertools: give tee's shared buffer a Python type
The buffer was a PyRc<PyItertoolsTeeData>, which is not a Python object, so the
collector could not walk into it and any cycle running through a tee was
uncollectable. Make it the _tee_dataobject type with a traverse, held by
PyRef, and split the rest along the same lines: tee() is a function, _tee is
the iterator type it builds, and _tee takes a single iterable rather than
returning a tuple from __new__. _tee is weak-referenceable, tee() rejects a
negative n with ValueError and reserves its tuple fallibly.
test_itertools.test_tee passes now; its expectedFailure marker is removed.
Assisted-by: Claude
* contextvars: hold the shared context state under locks
The variable map was a RefCell, the enter flag, the context index, the token's
used flag and the variable hash were Cells, and each carried an unsafe impl
Sync. A Context or ContextVar shared between threads overlapped their borrows
and panicked. The map and the per-variable cache are now PyMutex, the flags and
the index are atomics, entering a context is a compare_exchange, and the three
unsafe impl Sync are gone.
The cache also stopped being read through AtomicCell::as_ptr, which raced a
concurrent store on a value holding a PyObjectRef.
Values displaced from the map or the cache are dropped after the lock is
released: __del__ can come straight back into the same context, and the locks
are not reentrant.
Assisted-by: Claude
* generator: read the frame state under the running claim
send(), send_none(), throw() and close() read `closed` and `frame.lasti()`
before `running` was compare_exchanged, so the frame they went on to resume
could be one another thread had already advanced. A resume that decided from
`lasti() == 0` that the generator had not started pushes no value onto the
value stack, and the code after the yield pops one, which underflows the stack.
The compare_exchange now hands back a guard, taken before those reads and
released after maybe_close(), so the generator is retired while it is still
claimed.
Assisted-by: Claude
* itertools: claim a tee's position and its buffer as one step
`_tee::next` read `index` and moved it on afterwards, and
`_tee_dataobject::get_item` released `running` before the value it fetched
from the source was cached.
Two callers on one `_tee` then read the same index, hand out the same value
twice and advance past a value that was never cached, which leaves `index`
past `values.len()` and indexes the buffer out of bounds. Two callers at the
same index on separate tees each fetch a value from the source, and one of the
two is dropped without reaching a caller.
Both claims now cover the read and the update.
Assisted-by: Claude
* _asyncio: require InvalidStateError to be a type
new_invalid_state_error() called whatever `asyncio.exceptions.InvalidStateError`
names and unwrapped the downcast of the result, so a future asked for a result
it does not have panicked once that attribute was rebound to something that is
not an exception:
asyncio.InvalidStateError = lambda *args: 42
_asyncio.Future(loop=object()).result()
The type is looked up the way get_cancelled_error_type() looks its own up, and
raised with new_exception_msg; a lookup that does not produce an exception type
falls back to RuntimeError, as the other arms already did.
Assisted-by: Claude1 parent 00461a0 commit 259ce8c
12 files changed
Lines changed: 521 additions & 190 deletions
File tree
- Lib/test
- crates
- stdlib/src
- vm/src
- stdlib
- extra_tests/snippets
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1270 | 1270 | | |
1271 | 1271 | | |
1272 | 1272 | | |
1273 | | - | |
1274 | 1273 | | |
1275 | 1274 | | |
1276 | 1275 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2745 | 2745 | | |
2746 | 2746 | | |
2747 | 2747 | | |
| 2748 | + | |
| 2749 | + | |
| 2750 | + | |
| 2751 | + | |
| 2752 | + | |
| 2753 | + | |
| 2754 | + | |
| 2755 | + | |
| 2756 | + | |
| 2757 | + | |
2748 | 2758 | | |
2749 | | - | |
2750 | | - | |
2751 | | - | |
2752 | | - | |
2753 | | - | |
2754 | | - | |
2755 | | - | |
2756 | | - | |
2757 | | - | |
| 2759 | + | |
| 2760 | + | |
| 2761 | + | |
2758 | 2762 | | |
2759 | 2763 | | |
2760 | 2764 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | | - | |
52 | 50 | | |
53 | 51 | | |
54 | | - | |
| 52 | + | |
55 | 53 | | |
56 | 54 | | |
57 | | - | |
| 55 | + | |
58 | 56 | | |
59 | 57 | | |
60 | | - | |
61 | | - | |
62 | 58 | | |
63 | 59 | | |
64 | 60 | | |
| |||
71 | 67 | | |
72 | 68 | | |
73 | 69 | | |
74 | | - | |
| 70 | + | |
75 | 71 | | |
76 | | - | |
| 72 | + | |
77 | 73 | | |
78 | 74 | | |
79 | 75 | | |
80 | 76 | | |
81 | | - | |
82 | | - | |
| 77 | + | |
| 78 | + | |
83 | 79 | | |
84 | 80 | | |
85 | 81 | | |
86 | | - | |
| 82 | + | |
87 | 83 | | |
88 | 84 | | |
89 | 85 | | |
90 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
96 | 99 | | |
97 | 100 | | |
98 | | - | |
| 101 | + | |
99 | 102 | | |
100 | 103 | | |
101 | | - | |
102 | 104 | | |
103 | 105 | | |
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
107 | | - | |
| 109 | + | |
108 | 110 | | |
109 | 111 | | |
110 | 112 | | |
| |||
120 | 122 | | |
121 | 123 | | |
122 | 124 | | |
123 | | - | |
| 125 | + | |
124 | 126 | | |
125 | 127 | | |
126 | 128 | | |
| |||
131 | 133 | | |
132 | 134 | | |
133 | 135 | | |
134 | | - | |
135 | | - | |
| 136 | + | |
| 137 | + | |
136 | 138 | | |
137 | 139 | | |
138 | 140 | | |
| |||
170 | 172 | | |
171 | 173 | | |
172 | 174 | | |
173 | | - | |
| 175 | + | |
174 | 176 | | |
175 | 177 | | |
176 | 178 | | |
177 | | - | |
| 179 | + | |
178 | 180 | | |
179 | | - | |
| 181 | + | |
180 | 182 | | |
181 | 183 | | |
182 | 184 | | |
| |||
186 | 188 | | |
187 | 189 | | |
188 | 190 | | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
| 191 | + | |
| 192 | + | |
194 | 193 | | |
195 | 194 | | |
196 | 195 | | |
| |||
290 | 289 | | |
291 | 290 | | |
292 | 291 | | |
293 | | - | |
| 292 | + | |
294 | 293 | | |
295 | | - | |
| 294 | + | |
296 | 295 | | |
297 | | - | |
| 296 | + | |
298 | 297 | | |
299 | 298 | | |
300 | 299 | | |
| |||
303 | 302 | | |
304 | 303 | | |
305 | 304 | | |
306 | | - | |
307 | | - | |
308 | 305 | | |
309 | 306 | | |
310 | 307 | | |
| |||
320 | 317 | | |
321 | 318 | | |
322 | 319 | | |
323 | | - | |
| 320 | + | |
| 321 | + | |
324 | 322 | | |
325 | 323 | | |
326 | 324 | | |
327 | | - | |
328 | | - | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| |||
338 | 338 | | |
339 | 339 | | |
340 | 340 | | |
341 | | - | |
342 | | - | |
| 341 | + | |
| 342 | + | |
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
346 | 346 | | |
347 | 347 | | |
348 | | - | |
| 348 | + | |
349 | 349 | | |
350 | | - | |
| 350 | + | |
| 351 | + | |
351 | 352 | | |
352 | 353 | | |
353 | 354 | | |
| |||
370 | 371 | | |
371 | 372 | | |
372 | 373 | | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
378 | 382 | | |
379 | 383 | | |
380 | 384 | | |
381 | | - | |
| 385 | + | |
382 | 386 | | |
383 | | - | |
384 | | - | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
385 | 390 | | |
386 | 391 | | |
387 | | - | |
388 | | - | |
| 392 | + | |
389 | 393 | | |
390 | 394 | | |
391 | | - | |
| 395 | + | |
392 | 396 | | |
393 | | - | |
| 397 | + | |
394 | 398 | | |
| 399 | + | |
395 | 400 | | |
396 | 401 | | |
397 | 402 | | |
| |||
425 | 430 | | |
426 | 431 | | |
427 | 432 | | |
428 | | - | |
| 433 | + | |
429 | 434 | | |
430 | 435 | | |
431 | 436 | | |
| |||
447 | 452 | | |
448 | 453 | | |
449 | 454 | | |
450 | | - | |
| 455 | + | |
451 | 456 | | |
452 | 457 | | |
453 | 458 | | |
| |||
484 | 489 | | |
485 | 490 | | |
486 | 491 | | |
487 | | - | |
488 | | - | |
| 492 | + | |
| 493 | + | |
489 | 494 | | |
490 | 495 | | |
491 | 496 | | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
| 497 | + | |
| 498 | + | |
496 | 499 | | |
497 | 500 | | |
498 | 501 | | |
| |||
504 | 507 | | |
505 | 508 | | |
506 | 509 | | |
507 | | - | |
| 510 | + | |
508 | 511 | | |
509 | 512 | | |
510 | 513 | | |
511 | 514 | | |
512 | 515 | | |
513 | 516 | | |
514 | | - | |
| 517 | + | |
515 | 518 | | |
516 | 519 | | |
517 | 520 | | |
| |||
537 | 540 | | |
538 | 541 | | |
539 | 542 | | |
540 | | - | |
| 543 | + | |
541 | 544 | | |
542 | 545 | | |
543 | | - | |
544 | | - | |
545 | 546 | | |
546 | 547 | | |
547 | 548 | | |
| |||
598 | 599 | | |
599 | 600 | | |
600 | 601 | | |
601 | | - | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
602 | 607 | | |
603 | 608 | | |
604 | 609 | | |
| |||
0 commit comments