diff --git a/src/debugpy/adapter/clients.py b/src/debugpy/adapter/clients.py index f2fd81484..9f9a5df41 100644 --- a/src/debugpy/adapter/clients.py +++ b/src/debugpy/adapter/clients.py @@ -707,7 +707,10 @@ def disconnect_request(self, request): if terminate_debuggee == (): terminate_debuggee = None self.session.finalize('client requested "disconnect"', terminate_debuggee) - request.respond({}) + try: + request.respond({}) + except messaging.NoMoreMessages: + pass if self.using_stdio: # There's no way for the client to reconnect to this adapter once it disconnects @@ -726,6 +729,9 @@ def disconnect_request(self, request): except Exception: log.swallow_exception(level="warning") + if request.response is None: + return messaging.NO_RESPONSE + def disconnect(self): super().disconnect() diff --git a/tests/debugpy/adapter/test_clients.py b/tests/debugpy/adapter/test_clients.py index 35e89fb8d..1bb29464e 100644 --- a/tests/debugpy/adapter/test_clients.py +++ b/tests/debugpy/adapter/test_clients.py @@ -4,6 +4,8 @@ """Unit tests for debugpy.adapter.clients message handlers.""" +import io + import pytest from debugpy.common import json, messaging @@ -67,6 +69,32 @@ def test_initialize_advertises_terminate_debuggee(): assert capabilities["supportTerminateDebuggee"] is True +@pytest.mark.parametrize("client_closes_early", [False, True]) +def test_disconnect_response_when_client_closes(client_closes_early): + stream = messaging.JsonIOStream(io.BytesIO(), io.BytesIO()) + client = clients.Client.__new__(clients.Client) + client.using_stdio = False + channel = messaging.JsonMessageChannel(stream, client) + client.channel = channel + + class Session(_FakeSession): + def finalize(self, reason, terminate_debuggee): + if client_closes_early: + stream.close() + + client.session = Session() + request = messaging.Request( + channel, 1, "disconnect", messaging.MessageDict(None, {}) + ) + request._handle() + + assert channel._closed + if client_closes_early: + assert request.response is None + else: + assert request.response is not None and request.response.success + + def _make_client(start_request, has_started): stream = _MemoryStream() channel = messaging.JsonMessageChannel(stream, None)