test: Use asyncio.SelectorEventLoop() over deprecated asyncio.WindowsSelectorEventLoopPolicy(), move loop creation#34820
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste |
…SelectorEventLoopPolicy()
2222623 to
fa9168f
Compare
| if platform.system() == 'Windows': | ||
| asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | ||
| NetworkThread.network_event_loop = asyncio.new_event_loop() | ||
| NetworkThread.network_event_loop = asyncio.SelectorEventLoop() if platform.system() == "Windows" else asyncio.new_event_loop() |
There was a problem hiding this comment.
Removing the deprecation seems like a good start, but my understanding is that __init__ happens before thread.start(), and run() after it, so the event loop would still be created on the main thread and then run on the worker thread. My understanding is that this potential cross-thread handoff could still be the suspected source of flakiness.
There was a problem hiding this comment.
Hmm, ok, I'll move it as well.
This should fix bitcoin#34367 I am not familiar with Windows sockets thread-safety, but creating the event loop on the main thread, and running it in the network thread could lead to a fast abort in Python on Windows (without any stderr): ``` 77/276 - wallet_txn_clone.py failed, Duration: 1 s stdout: 2025-12-10T08:04:27.500134Z TestFramework (INFO): PRNG seed is: 4018092284830106117 stderr: Combine the logs and print the last 99999999 lines ... ============ Combined log for D:\a\_temp/test_runner_₿_🏃_20251210_075632/wallet_txn_clone_196: ============ test 2025-12-10T08:04:27.500134Z TestFramework (INFO): PRNG seed is: 4018092284830106117 test 2025-12-10T08:04:27.500433Z TestFramework (DEBUG): Setting up network thread ``` Also, I couldn't find any docs that require the loop must be created on the thread that runs them: * https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.new_event_loop * https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_forever However, the patch seems trivial to review, harmless, and easy to revert, so it may be a good try to fix the intermittent Windows Python crash.
|
ACK fa050da The change seems low-risk and makes sense based on my limited understanding of the situation. |
It is deprecated according to https://docs.python.org/3.14/library/asyncio-policy.html#asyncio.WindowsSelectorEventLoopPolicy The replacement exists since python 3.7
Also, move the event loop creation to happen in the thread that runs the loop.