Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/debugpy/adapter/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down
28 changes: 28 additions & 0 deletions tests/debugpy/adapter/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Unit tests for debugpy.adapter.clients message handlers."""

import io

import pytest

from debugpy.common import json, messaging
Expand Down Expand Up @@ -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)
Expand Down