Thomas Grainger <tagr...@gmail.com> added the comment:
> weakref.WeakSet is not thread-safe, which means concurrent create_task's in > different threads (even on separate event loops) is not safe. actually it looks like WeakSet is *supposed* to be thread-safe ``` import patchy patchy.patch( "weakref:WeakSet._commit_removals", """\ @@ -1,5 +1,10 @@ def _commit_removals(self): - l = self._pending_removals + pop = self._pending_removals.pop discard = self.data.discard - while l: - discard(l.pop()) + while True: + try: + item = pop() + except IndexError: + return + else: + discard(item) """ ) import itertools import asyncio import concurrent.futures import sys import threading threads = 200 def test_all_tasks_threading() -> None: async def foo() -> None: await asyncio.sleep(0) async def create_tasks() -> None: for i in range(1000): asyncio.create_task(foo()) await asyncio.sleep(0) results = [] with concurrent.futures.ThreadPoolExecutor(threads) as tpe: for f in concurrent.futures.as_completed( tpe.submit(asyncio.run, create_tasks()) for i in range(threads) ): results.append(f.result()) assert results == [None] * threads def main(): for i in itertools.count(): test_all_tasks_threading() print(f"worked {i}") return 0 if __name__ == "__main__": sys.exit(main()) ``` ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue44962> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com