mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 20:45:11 +00:00
fix(remote): run the session channel and tunnel loops without asyncio.run so exit starts no thread on Python 3.12.0-3.12.2
This commit is contained in:
@@ -17,6 +17,7 @@ from unshackle.core import remote_channel, remote_service
|
||||
from unshackle.core.remote_channel import PollingChannel, ServerMessages, WebSocketChannel
|
||||
from unshackle.core.remote_protocol import ClientErrorKind
|
||||
from unshackle.core.remote_service import RemoteClient
|
||||
from unshackle.core.tunnel import TunnelClient, run_loop
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -153,3 +154,47 @@ def test_a_channel_closes_after_its_loop_has_closed() -> None:
|
||||
channel.thread = SimpleNamespace(is_alive=lambda: True, join=lambda timeout=None: None)
|
||||
|
||||
channel.close()
|
||||
|
||||
|
||||
def refuse_threads_after_executor_use(monkeypatch: pytest.MonkeyPatch) -> Callable[[Any], Any]:
|
||||
"""A run that uses the default executor, then refuses every new thread, as Python 3.12.0 to 3.12.2 do at exit."""
|
||||
|
||||
def refuse(self: Thread) -> None:
|
||||
raise RuntimeError("can't create new thread at interpreter shutdown")
|
||||
|
||||
async def run(self: Any) -> None:
|
||||
await asyncio.get_running_loop().run_in_executor(None, lambda: None)
|
||||
monkeypatch.setattr(Thread, "start", refuse)
|
||||
|
||||
return run
|
||||
|
||||
|
||||
def test_a_channel_loop_ends_without_starting_a_thread(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""The channel thread shuts its loop's executor down without a new thread."""
|
||||
monkeypatch.setattr(WebSocketChannel, "run", refuse_threads_after_executor_use(monkeypatch))
|
||||
service = make_service()
|
||||
service.ctx = SimpleNamespace(obj=None)
|
||||
messages = ServerMessages(service)
|
||||
channel = WebSocketChannel(messages, PollingChannel(messages))
|
||||
|
||||
channel.thread.run()
|
||||
|
||||
|
||||
def test_a_tunnel_loop_ends_without_starting_a_thread(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""The tunnel thread, which the same atexit close stops, shuts its loop's executor down without a new thread."""
|
||||
monkeypatch.setattr(TunnelClient, "run", refuse_threads_after_executor_use(monkeypatch))
|
||||
client = TunnelClient(SERVER, {})
|
||||
|
||||
client.thread.run()
|
||||
|
||||
|
||||
def test_a_thread_loop_cancels_the_tasks_its_run_leaves() -> None:
|
||||
"""A task that the run leaves, such as a channel answer still sending, ends cancelled when the loop closes."""
|
||||
left: list[asyncio.Future[None]] = []
|
||||
|
||||
async def main() -> None:
|
||||
left.append(asyncio.ensure_future(asyncio.sleep(10)))
|
||||
|
||||
run_loop(main)
|
||||
|
||||
assert left[0].cancelled()
|
||||
|
||||
Reference in New Issue
Block a user