fix(remote): close a session channel whose loop already ended

This commit is contained in:
imSp4rky
2026-11-24 18:30:58 -07:00
parent 83e5c88f6f
commit cdeeba17ab
2 changed files with 19 additions and 2 deletions
+17 -1
View File
@@ -2,8 +2,10 @@
from __future__ import annotations from __future__ import annotations
import asyncio
import json import json
from threading import Event, Thread from threading import Event, Thread
from types import SimpleNamespace
from typing import Any, Callable from typing import Any, Callable
import pytest import pytest
@@ -11,7 +13,7 @@ import responses
from tests.remote.unit.test_session_reopen import SERVER, make_service from tests.remote.unit.test_session_reopen import SERVER, make_service
from unshackle.core import remote_channel, remote_service from unshackle.core import remote_channel, remote_service
from unshackle.core.remote_channel import PollingChannel, ServerMessages from unshackle.core.remote_channel import PollingChannel, ServerMessages, WebSocketChannel
from unshackle.core.remote_protocol import ClientErrorKind from unshackle.core.remote_protocol import ClientErrorKind
from unshackle.core.remote_service import RemoteClient from unshackle.core.remote_service import RemoteClient
@@ -136,3 +138,17 @@ def test_a_tunnel_copies_the_auth_header_the_server_reads(monkeypatch: pytest.Mo
remote_service.start_tunnel(client) remote_service.start_tunnel(client)
assert sent[0].get("X-Api-Key") == KEY and "X-Secret-Key" not in sent[0] assert sent[0].get("X-Api-Key") == KEY and "X-Secret-Key" not in sent[0]
def test_a_channel_closes_after_its_loop_has_closed() -> None:
"""A refused upgrade closes the loop of the channel thread before the thread ends."""
service = make_service()
service.ctx = SimpleNamespace(obj=None)
messages = ServerMessages(service)
channel = WebSocketChannel(messages, PollingChannel(messages))
loop = asyncio.new_event_loop()
loop.close()
channel.loop, channel.task = loop, SimpleNamespace(cancel=lambda: None)
channel.thread = SimpleNamespace(is_alive=lambda: True, join=lambda timeout=None: None)
channel.close()
+1
View File
@@ -468,6 +468,7 @@ class WebSocketChannel:
def close(self) -> None: def close(self) -> None:
if self.loop and self.task and self.thread.is_alive(): if self.loop and self.task and self.thread.is_alive():
with contextlib.suppress(RuntimeError): # the loop closes before its thread ends
self.loop.call_soon_threadsafe(self.task.cancel) self.loop.call_soon_threadsafe(self.task.cancel)
self.thread.join(5) self.thread.join(5)
self.fallback.close() self.fallback.close()