Commit graph

207 commits

Author SHA1 Message Date
Kumar Aditya
c7a9d06e06
gh-128002: optimistically remove tasks from linked list when finished (#129995) 2025-02-13 13:19:53 +05:30
Kumar Aditya
94cd2e0dde
gh-129289: fix crash when task finalizer is not called in asyncio (#129840) 2025-02-10 17:03:59 +05:30
Kumar Aditya
bff4bfeae1
gh-128002: add fast path for native tasks in asyncio.all_tasks (#129943) 2025-02-10 15:04:33 +05:30
Kumar Aditya
7246b9124c
gh-128002: simplify asyncio.all_tasks to use PyList_Extend instead of manual iteration (#129942) 2025-02-10 14:26:32 +05:30
Kumar Aditya
0d68b14a0d
gh-128002: use per threads tasks linked list in asyncio (#128869)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2025-02-06 19:51:07 +01:00
Kumar Aditya
e7bc0cd182
gh-91048: fix _asyncio.future_discard_from_awaited_by (#129731)
fix discard
2025-02-06 16:35:33 +00:00
Kumar Aditya
75c551974f
gh-91048: fix thread safety for asyncio stack introspection APIs (#129399) 2025-02-06 21:33:52 +05:30
Victor Stinner
3447f4a56a
gh-129354: Use PyErr_FormatUnraisable() function (#129514)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
2025-01-31 14:20:35 +01:00
Kumar Aditya
8e0b36006c
gh-128002: use _PyObject_SetMaybeWeakref when creating tasks in asyncio (#128885) 2025-01-24 21:12:56 +05:30
Łukasz Langa
fc3d400165
gh-91048: Also clear and set ts->asyncio_running_task with eager tasks (#129197)
This was missing from gh-124640. It's already covered by the new
test_asyncio/test_free_threading.py in combination with the runtime
assertion in set_ts_asyncio_running_task.

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-01-23 19:26:36 +01:00
Yury Selivanov
188598851d
GH-91048: Add utils for capturing async call stack for asyncio programs and enable profiling (#124640)
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
Co-authored-by: Jacob Coffee <jacob@z7x.org>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
2025-01-22 17:25:29 +01:00
Kumar Aditya
7dc41ad6a7
gh-128002: fix asyncio.all_tasks against concurrent deallocations of tasks (#128541) 2025-01-09 21:26:00 +05:30
Kumar Aditya
513a4efa75
gh-128002: fix many thread safety issues in asyncio (#128147)
* Makes `_asyncio.Task` and `_asyncio.Future` thread-safe by adding critical sections
* Add assertions to check for thread safety checking locking of object by critical sections in internal functions
* Make `_asyncio.all_tasks` thread safe when eager tasks are used
* Add a thread safety test
2025-01-04 14:18:22 +05:30
Yan Yanchii
5c814c83cd
gh-128198: Add missing error checks for usages of PyIter_Next() (GH-128199) 2024-12-25 19:42:04 +02:00
Kumar Aditya
76f1785657
gh-128002: use internal llist implementation for asyncio tasks (#128256) 2024-12-25 17:51:27 +05:30
Kumar Aditya
8a433b683f
gh-121621: clear running loop early in asyncio (#128004) 2024-12-18 14:25:03 +00:00
Kumar Aditya
dbd08fb60d
gh-127949: deprecate asyncio.get_event_loop_policy (#128053)
This deprecates `asyncio.get_event_loop_policy` and will be removed in Python 3.16.
2024-12-18 18:04:20 +05:30
Kumar Aditya
37c57dfad1
gh-126405: fix use-after-free in _asyncio.Future.remove_done_callback (#126733) 2024-11-12 18:01:34 +05:30
Kumar Aditya
b19d12f447
remove minor redundant code from _asyncio (#126578) 2024-11-08 13:17:03 +00:00
Kumar Aditya
a3e8e7bbc3
GH-107803: use circular double linked list for tasks in _asyncio (#126577) 2024-11-08 12:51:11 +00:00
Nico-Posada
f032f6ba8f
gh-126138: Fix use-after-free in _asyncio.Task by evil __getattribute__ (#126305)
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
2024-11-02 13:16:00 +05:30
Bénédikt Tran
0e8665554b
gh-126080: fix UAF on task->task_context in task_call_step_soon due to an evil loop.__getattribute__ (#126120) 2024-10-31 10:14:47 -07:00
Nico-Posada
d07dcce693
gh-126083: Fix a reference leak in asyncio.Task when reinitializing with new non-None context (#126103) 2024-10-31 10:47:57 +03:00
Bénédikt Tran
ed5059eeb1
gh-125966: fix use-after-free on fut->fut_callback0 due to an evil callback's __eq__ in asyncio (#125967) 2024-10-27 22:40:10 +05:30
Bénédikt Tran
f819d4301d
gh-125984: fix use-after-free on fut->fut_{callback,context}0 due to an evil loop.__getattribute__ (#126003) 2024-10-27 20:34:43 +05:30
Bénédikt Tran
c5b99f5c2c
gh-125969: fix OOB in future_schedule_callbacks due to an evil call_soon (#125970)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
2024-10-25 23:45:09 +05:30
Kumar Aditya
cae853e3b4
GH-125789: fix fut._callbacks to always return a copy of callbacks (#125922)
Fix `asyncio.Future._callbacks` to always return a copy of the internal list of callbacks to avoid mutation from user code affecting the internal state.
2024-10-25 18:19:30 +05:30
Mark Shannon
c9014374c5
GH-125174: Make immortal objects more robust, following design from PEP 683 (GH-125251) 2024-10-10 18:19:08 +01:00
Petr Viktorin
57c471a688
gh-123091: Use more _Py_IsImmortalLoose() (GH-123602)
Switch more _Py_IsImmortal(...) assertions to _Py_IsImmortalLoose(...)

The remaining calls to _Py_IsImmortal are in free-threaded-only code,
initialization of core objects, tests, and guards that fall back to
code that works with mortal objects.
2024-09-02 18:17:48 +02:00
Kumar Aditya
3aaed083a3
GH-120974: make _asyncio.all_tasks thread safe (#122801)
Make `_asyncio.all_tasks` thread safe, also changes state lock to use critical section.
2024-08-11 20:49:31 +05:30
Sam Gross
b5e6fb39a2
gh-120974: Make asyncio swap_current_task safe in free-threaded build (#122317)
* gh-120974: Make asyncio `swap_current_task` safe in free-threaded build
2024-08-02 19:02:08 +05:30
Peter Bierma
c08696286f
gh-122332: Fix missing NULL check in asyncio.Task.get_coro (#122338) 2024-07-27 11:57:48 +05:30
Sam Gross
a15feded71
gh-120974: Make _asyncio._leave_task atomic in the free-threaded build (#122139)
* gh-120974: Make _asyncio._leave_task atomic in the free-threaded build

Update `_PyDict_DelItemIf` to allow for an argument to be passed to the
predicate.
2024-07-23 17:06:03 +00:00
Sam Gross
c908d1f87d
gh-120974: Use common freelist code in asyncio (#122132)
This refactors asyncio to use the common freelist helper functions and
macros. As a side effect, the freelist for _asyncio.Future is now
re-enabled in the free-threaded build.
2024-07-23 21:54:24 +05:30
Sam Gross
47847aa8ef
gh-120974: Make _asyncio._enter_task atomic in the free-threaded build (#122138)
Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
2024-07-23 14:17:14 +05:30
Sam Gross
97248204a1
gh-121621: Disable asyncio freelist in free-threaded build (#122046)
The futureobj freelist isn't thread-safe. We intend to re-enable the
freelist in a thread-safe way for 3.14 (but not 3.13).
2024-07-19 15:26:29 -04:00
Sam Gross
81fd625b5c
gh-121621: Move asyncio_running_loop to private struct (#121939)
This avoids changing the ABI and keeps the field in the private struct.
2024-07-17 15:21:24 -07:00
Ken Jin
69c68de43a
gh-121621: Move asyncio running loop to thread state (GH-121695) 2024-07-17 01:09:58 +08:00
Kumar Aditya
48042c52a6
fix outdated comments in asyncio (#121783) 2024-07-15 05:59:19 +00:00
Ken Jin
5d6861ad06
gh-121621: Use PyMutex for writes to asyncio state (#121622)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2024-07-14 12:22:56 +00:00
Savannah Ostrowski
65fededf9c
Update retroactive comments from GH-117741 (segfault in FutureIter_dealloc) (GH-121638)
Address comments
2024-07-12 10:34:30 +02:00
Kumar Aditya
82235449b8
gh-107803: fix thread safety issue in double linked list implementation (#121007) 2024-06-26 05:11:32 +00:00
Kumar Aditya
4717aaa1a7
GH-107803: double linked list implementation for asyncio tasks (GH-107804)
* linked list

* add tail optmiization to linked list

* wip

* wip

* wip

* more fixes

* finally it works

* add tests

* remove weakreflist

* add some comments

* reduce code duplication in _asynciomodule.c

* address some review comments

* add invariants about the state of the linked list

* add better explanation

* clinic regen

* reorder branches for better branch prediction

* Update Modules/_asynciomodule.c

* Apply suggestions from code review

Co-authored-by: Itamar Oren <itamarost@gmail.com>

* fix capturing of eager tasks

* add comment to task finalization

* fix tests and couple c implmentation to c task

improved linked-list logic and more comments

* fix test

---------

Co-authored-by: Itamar Oren <itamarost@gmail.com>
2024-06-22 10:58:35 -07:00
Brett Simmers
c2627d6eea
gh-116322: Add Py_mod_gil module slot (#116882)
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
2024-05-03 11:30:55 -04:00
Savannah Ostrowski
d8f350309d
GH-115874: Fix segfault in FutureIter_dealloc (GH-117741) 2024-04-19 22:30:52 +00:00
Guido van Rossum
fa58e75a86
gh-116720: Fix corner cases of taskgroups (#117407)
This prevents external cancellations of a task group's parent task to
be dropped when an internal cancellation happens at the same time.
Also strengthen the semantics of uncancel() to clear self._must_cancel
when the cancellation count reaches zero.

Co-Authored-By: Tin Tvrtković <tinchester@gmail.com>
Co-Authored-By: Arthur Tacca
2024-04-09 08:17:28 -07:00
Serhiy Storchaka
72d3cc94cd
gh-116437: Use new C API PyDict_Pop() to simplify the code (GH-116438) 2024-03-07 11:21:08 +02:00
Jamie Phan
4826d52338
gh-112182: Replace StopIteration with RuntimeError for future (#113220)
When an `StopIteration` raises into `asyncio.Future`, this will cause
a thread to hang. This commit address this by not raising an exception
and silently transforming the `StopIteration` with a `RuntimeError`,
which the caller can reconstruct from `fut.exception().__cause__`
2024-01-09 21:21:00 -08:00
Serhiy Storchaka
5273655bea
gh-113848: Use PyErr_GivenExceptionMatches() for check for CancelledError (GH-113849) 2024-01-09 21:41:02 +02:00
Serhiy Storchaka
d4f83e1e3a
gh-111789: Use PyDict_GetItemRef() in Modules/_asynciomodule.c (GH-112072) 2023-11-15 04:28:30 +00:00