Part 4 - asyncio: removes all deprecated patterns and adopts modern asyncio idioms.#972
Open
ping-ke wants to merge 15 commits intoupgrade/py313-baselinefrom
Open
Part 4 - asyncio: removes all deprecated patterns and adopts modern asyncio idioms.#972ping-ke wants to merge 15 commits intoupgrade/py313-baselinefrom
ping-ke wants to merge 15 commits intoupgrade/py313-baselinefrom
Conversation
Python 3.10 deprecated the loop= parameter in most asyncio APIs, and Python 3.12 removed it entirely. This removes all deprecated patterns across the codebase and adopts modern asyncio idioms. Changes: - Remove loop= parameter from asyncio.Event, asyncio.wait, asyncio.ensure_future, asyncio.start_server, open_connection, etc. - Replace asyncio.ensure_future() with asyncio.create_task() - Replace get_event_loop().run_until_complete() with asyncio.run() - Replace asyncio.get_event_loop() with asyncio.get_running_loop() where inside a running event loop - Add _get_or_create_event_loop() helper in utils.py for call sites that run before the loop starts (SlaveServer, SlaveConnectionManager) - Replace asyncio.Future used as a one-shot signal with asyncio.Event (active_future → active_event, ping_received_future → ping_received_event) - Make start()/shutdown() methods async where they previously called loop.run_until_complete() internally - Update eth_hash import: BasePreImage → PreImageAPI (eth-hash >=0.5 API) - Update cryptography API: EllipticCurvePublicNumbers.from_encoded_point → EllipticCurvePublicKey.from_encoded_point - Add conftest.py to cancel pending asyncio tasks between tests - Replace assertRaisesRegexp (removed in Py 3.12) with assertRaisesRegex Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Track and cancel all fire-and-forget asyncio tasks that were leaking across tests, causing resource exhaustion and timeout failures. - AbstractConnection: add _loop_task and _handler_tasks tracking; use try/finally in active_and_loop_forever to ensure cleanup on cancellation; cancel _loop_task in close() - master.py: track SlaveConnection loop task and __init_cluster task; cancel _init_task on shutdown - slave.py: track MasterConnection, SlaveConnection, PeerShardConnection loop tasks and __start_server task - shard.py: track PeerShardConnection loop task - miner.py: track and cancel mining task in disable() - simple_network.py: track Peer loop task and connect_seed task - test_utils.py: restructure shutdown_clusters with try/finally to guarantee task cleanup; await server.wait_closed() for slave servers - conftest.py: multi-round task cancellation; reset aborted_rpc_count
Contributor
Code reviewFound 2 issues:
pyquarkchain/quarkchain/p2p/p2p_server.py Lines 104 to 106 in e720240
pyquarkchain/quarkchain/utils.py Lines 383 to 388 in e720240 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
qzhodl
reviewed
Mar 25, 2026
qzhodl
reviewed
Mar 25, 2026
Contributor
Author
For issue 1: |
qzhodl
reviewed
Mar 26, 2026
qzhodl
reviewed
Mar 26, 2026
qzhodl
reviewed
Mar 26, 2026
qzhodl
reviewed
Mar 26, 2026
qzhodl
reviewed
Mar 26, 2026
- convert test_cluster.py and test_jsonrpc.py to unittest.IsolatedAsyncioTestCase - make create_test_clusters/shutdown_clusters async, ClusterContext async context manager - replace call_async() with await, assert_true_with_timeout with async_assert_true_with_timeout - replace _get_or_create_event_loop() with asyncio.get_running_loop() in master/slave - fix mock_pay_native_token_as_gas to support both sync and async wrapped functions - remove obsolete _get_or_create_event_loop, call_async, assert_true_with_timeout helpers - fix conftest to restore event loop after IsolatedAsyncioTestCase closes it
qzhodl
reviewed
Mar 26, 2026
Migrate test classes to IsolatedAsyncioTestCase so that asyncio.create_task calls in production code (shard_state, root_state) have a running event loop. - Replace asyncio.ensure_future with asyncio.create_task in production code - Replace _get_or_create_event_loop with asyncio.get_running_loop in master/slave - Convert ClusterContext to async context manager - Convert create_test_clusters / shutdown_clusters to async - Add fire_and_forget and async_assert_true_with_timeout utilities - Simplify conftest fixture to only restore event loop after IsolatedAsyncioTestCase teardown
qzhodl
reviewed
Mar 30, 2026
qzhodl
approved these changes
Mar 31, 2026
This was referenced Mar 31, 2026
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 free
to 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.
Summary
Python 3.10 deprecated the loop= parameter in most asyncio APIs, and Python 3.12 removed it entirely. This removes all deprecated patterns across the codebase and adopts modern asyncio idioms.
Key changes:
Remove loop= parameter from asyncio.Event, asyncio.wait, asyncio.ensure_future, asyncio.start_server, open_connection, etc.
Replace asyncio.ensure_future() with asyncio.create_task()
Replace get_event_loop().run_until_complete() with asyncio.run()
Replace asyncio.get_event_loop() with asyncio.get_running_loop() where inside a running event loop
Add _get_or_create_event_loop() helper in utils.py for call sites that run before the loop starts (SlaveServer, SlaveConnectionManager)
Replace asyncio.Future used as a one-shot signal with asyncio.Event (active_future → active_event, ping_received_future → ping_received_event)
Make start()/shutdown() methods async where they previously called loop.run_until_complete() internally
Update eth_hash import: BasePreImage → PreImageAPI (eth-hash >=0.5 API)
Update cryptography API: EllipticCurvePublicNumbers.from_encoded_point → EllipticCurvePublicKey.from_encoded_point
Add conftest.py to cancel pending asyncio tasks between tests
Replace assertRaisesRegexp (removed in Py 3.12) with assertRaisesRegex
Test plan
pytest quarkchain/cluster/tests/passespytest quarkchain/p2p/passesDeprecationWarningabout event loop parameters