Uh oh!
There was an error while loading. Please reload this page.
gh-128002: use _PyObject_SetMaybeWeakref when creating tasks in asyncio - #128885
Merged
Conversation
kumaraditya303
requested review from
1st1, asvetlov and willingc
as code ownersJanuary 15, 2025 15:32
9 tasks
colesbury
commented
Jan 15, 2025
Contributor
Can you add a test for this? I think something like the following would catch the bug:
|
kumaraditya303
commented
Jan 23, 2025
ContributorAuthor
I think the following code appears to catch that bug: importasyncioimportthreadingdeffunc():
loop=asyncio.EventLoop()
asyncdefcoro():
awaitasyncio.sleep(0.01)
lock=threading.Lock()
count=0asyncdefmain():
nonlocalcountwhileTrue:
withlock:
asyncio.create_task(coro())
count=len(asyncio.all_tasks(loop))
th=threading.Thread(target=loop.run_until_complete, args=(main(),))
th.start()
defcheck():
withlock:
assertcount==len(asyncio.all_tasks(loop))
threads= [threading.Thread(target=check) for_inrange(10)]
fortinthreads:
t.start()
fortinthreads:
t.join()
func()Without the fix, I see many assertion errors and with it there isn't any. |
kumaraditya303
commented
Jan 23, 2025
ContributorAuthor
@colesbury Added test based on the above code. |
colesbury
approved these changes
Jan 23, 2025
kumaraditya303
commented
Jan 24, 2025
ContributorAuthor
I have tweaked the test to be a little less strict when comparing, it still tests the bug. Without fix: ❯ ./python -m test test_asyncio.test_free_threading -FUsing random seed: 29444251560:00:00 load avg: 0.61 Run tests sequentially in a single process0:00:00 load avg: 0.61 [ 1] test_asyncio.test_free_threadingWarning -- Uncaught thread exception: AssertionErrorException in thread Thread-5 (check):Warning -- Uncaught thread exception: AssertionErrorException in thread Thread-2 (check):Traceback (most recent call last):Traceback (most recent call last): File "/home/realkumaraditya/cpython/Lib/threading.py", line 1054, in _bootstrap_inner self.run() ~~~~~~~~^^ File "/home/realkumaraditya/cpython/Lib/threading.py", line 996, in run self._target(*self._args, **self._kwargs) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/realkumaraditya/cpython/Lib/test/test_asyncio/test_free_threading.py", line 86, in check self.assertSetEqual(tasks & self.all_tasks(loop), tasks) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/realkumaraditya/cpython/Lib/unittest/case.py", line 1176, in assertSetEqual self.fail(self._formatMessage(msg, standardMsg)) ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/realkumaraditya/cpython/Lib/unittest/case.py", line 741, in fail raise self.failureException(msg)AssertionError: Items in the second set but not the first:<Task pending name='Task-1' coro=<TestFreeThreading.test_all_tasks_different_thread.<locals>.main() running at /home/realkumaraditya/cpython/Lib/test/test_asyncio/test_free_threading.py:77> cb=[_run_until_complete_cb() at /home/realkumaraditya/cpython/Lib/asyncio/base_events.py:181]> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Uh oh!
There was an error while loading. Please reload this page.