GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-142615: disallow multiple initializations of asyncio tasks and futures by kumaraditya303 · Pull Request #142616 · python/cpython

gh-142615: disallow multiple initializations of asyncio tasks and futures #142616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
kumaraditya303 merged 11 commits into python:main from kumaraditya303:immutable-task
Jan 3, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-142615: disallow multiple initializations of asyncio tasks and futures #142616

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
37f8e99
disallow multiple initializations of asyncio tasks and futures
kumaraditya303 Dec 12, 2025
ffa9ec9
do the same for python tasks
kumaraditya303 Dec 12, 2025
4eb0119
add tests
kumaraditya303 Dec 12, 2025
a457ba1
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 12, 2025
82a7fd0
fix news
kumaraditya303 Dec 12, 2025
5ae2e22
fmt
kumaraditya303 Dec 12, 2025
3e697f6
make error message same for both implementations
kumaraditya303 Dec 12, 2025
09983fc
Merge branch 'main' into immutable-task
kumaraditya303 Dec 29, 2025
ed666f9
remove redundant ref counting
kumaraditya303 Dec 29, 2025
080a0c8
fix compilation
kumaraditya303 Dec 29, 2025
c85880b
Merge branch 'main' into immutable-task
kumaraditya303 Jan 3, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
disallow multiple initializations of asyncio tasks and futures
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303
kumaraditya303 committed Dec 12, 2025
commit 37f8e990ac4819e20018f39c91623265fe17ffcc

There are no files selected for viewing

27 changes: 0 additions & 27 deletions 27 Lib/test/test_asyncio/test_futures.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1091,33 +1091,6 @@ def __getattribute__(self, name):
fut.add_done_callback(fut_callback_0)
self.assertRaises(ReachableCode, fut.set_result, "boom")

def test_use_after_free_on_fut_context_0_with_evil__getattribute__(self):
# see: https://github.com/python/cpython/issues/125984

class EvilEventLoop(SimpleEvilEventLoop):
def call_soon(self, *args, **kwargs):
super().call_soon(*args, **kwargs)
raise ReachableCode

def __getattribute__(self, name):
if name == 'call_soon':
# resets the future's event loop
fut.__init__(loop=SimpleEvilEventLoop())
return object.__getattribute__(self, name)

evil_loop = EvilEventLoop()
with mock.patch.object(self, 'loop', evil_loop):
fut = self._new_future()
self.assertIs(fut.get_loop(), evil_loop)

fut_callback_0 = mock.Mock()
fut_context_0 = mock.Mock()
fut.add_done_callback(fut_callback_0, context=fut_context_0)
del fut_context_0
del fut_callback_0
self.assertRaises(ReachableCode, fut.set_result, "boom")


@unittest.skipUnless(hasattr(futures, '_CFuture'),
'requires the C _asyncio module')
class CFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
Expand Down
36 changes: 0 additions & 36 deletions 36 Lib/test/test_asyncio/test_tasks.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2776,29 +2776,6 @@ def test_get_context(self):
finally:
loop.close()

def test_proper_refcounts(self):
# see: https://github.com/python/cpython/issues/126083
class Break:
def __str__(self):
raise RuntimeError("break")

obj = object()
initial_refcount = sys.getrefcount(obj)

coro = coroutine_function()
with contextlib.closing(asyncio.EventLoop()) as loop:
task = asyncio.Task.__new__(asyncio.Task)
for _ in range(5):
with self.assertRaisesRegex(RuntimeError, 'break'):
task.__init__(coro, loop=loop, context=obj, name=Break())

coro.close()
task._log_destroy_pending = False
del task

self.assertEqual(sys.getrefcount(obj), initial_refcount)


def add_subclass_tests(cls):
BaseTask = cls.Task
BaseFuture = cls.Future
Expand Down Expand Up @@ -2921,19 +2898,6 @@ class CTask_CFuture_Tests(BaseTaskTests, SetMethodsTest,
all_tasks = getattr(tasks, '_c_all_tasks', None)
current_task = staticmethod(getattr(tasks, '_c_current_task', None))

@support.refcount_test
def test_refleaks_in_task___init__(self):
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
async def coro():
pass
task = self.new_task(self.loop, coro())
self.loop.run_until_complete(task)
refs_before = gettotalrefcount()
for i in range(100):
task.__init__(coro(), loop=self.loop)
self.loop.run_until_complete(task)
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)

def test_del__log_destroy_pending_segfault(self):
async def coro():
pass
Expand Down
19 changes: 6 additions & 13 deletions 19 Modules/_asynciomodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -498,21 +498,14 @@ future_schedule_callbacks(asyncio_state *state, FutureObj *fut)
static int
future_init(FutureObj *fut, PyObject *loop)
{
if (fut->fut_loop != NULL) {
PyErr_Format(PyExc_RuntimeError,
"%T object is already initialized", fut);
return -1;
}

PyObject *res;
int is_true;

Py_CLEAR(fut->fut_loop);
Py_CLEAR(fut->fut_callback0);
Py_CLEAR(fut->fut_context0);
Py_CLEAR(fut->fut_callbacks);
Py_CLEAR(fut->fut_result);
Py_CLEAR(fut->fut_exception);
Py_CLEAR(fut->fut_exception_tb);
Py_CLEAR(fut->fut_source_tb);
Py_CLEAR(fut->fut_cancel_msg);
Py_CLEAR(fut->fut_cancelled_exc);
Py_CLEAR(fut->fut_awaited_by);

fut->fut_state = STATE_PENDING;
fut->fut_log_tb = 0;
fut->fut_blocking = 0;
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.