Skip to content

Commit a909bd2

Browse files
committed
Create event loop when needed, and shut it down afterwards.
This fixes issues with the tests (no more "unclosed event loops" errors), and probably allows tee_popen() to run in parallel.
1 parent 05bdba2 commit a909bd2

2 files changed

Lines changed: 2 additions & 7 deletions

File tree

nox/_cli.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from __future__ import annotations
1818

1919
__lazy_modules__ = {
20-
"asyncio",
2120
"importlib",
2221
"importlib.metadata",
2322
"nox._options",
@@ -37,7 +36,6 @@
3736
"urllib.parse",
3837
}
3938

40-
import asyncio
4139
import importlib.metadata
4240
import os
4341
import shutil
@@ -318,7 +316,6 @@ def _main(*, main_ep: bool) -> None:
318316
)
319317

320318
nox.registry.reset()
321-
asyncio.set_event_loop(asyncio.new_event_loop())
322319
exit_code = execute_workflow(args)
323320

324321
# Done; exit.

nox/popen.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,10 @@ class _TeeSubprocess:
170170
def __init__(
171171
self,
172172
*,
173-
loop: asyncio.AbstractEventLoop,
174173
interrupt_timeout: float | None,
175174
terminate_timeout: float | None,
176175
) -> None:
177-
self.loop = loop
176+
self.loop = asyncio.new_event_loop()
178177
self.stdout: list[bytes] = []
179178
self.stderr: list[bytes] = []
180179
self.interrupt_timeout = interrupt_timeout
@@ -353,6 +352,7 @@ def handle_sigint() -> None:
353352
)
354353
finally:
355354
self.loop.remove_signal_handler(signal.SIGINT)
355+
self.loop.close()
356356
sys.stdout.flush()
357357
if is_canceled:
358358
raise KeyboardInterrupt
@@ -371,9 +371,7 @@ def tee_popen(
371371
Return a tuple ``(return_code, stdout, stderr)``.
372372
Standard output and standard error are also printed to ``sys.stdout``.
373373
"""
374-
loop = asyncio.get_event_loop()
375374
teesub = _TeeSubprocess(
376-
loop=loop,
377375
interrupt_timeout=interrupt_timeout,
378376
terminate_timeout=terminate_timeout,
379377
)

0 commit comments

Comments
 (0)