Skip to content

Commit

Permalink
Fix #1008: Re-attaching continuously creates 'accept_worker' threads …
Browse files Browse the repository at this point in the history
…in debugpy

Don't recreate the server socket needlessly.
  • Loading branch information
Pavel Minaev authored and int19h committed Aug 15, 2022
1 parent 71d42ed commit 8b5eeee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
license_file = LICENSE
license_files = LICENSE

[versioneer]
VCS = git
Expand All @@ -10,4 +10,4 @@ tag_prefix = v
parentdir_prefix = debugpy-

[aliases]
test=pytest
test = pytest
6 changes: 5 additions & 1 deletion src/debugpy/adapter/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,16 @@ def attach_request(self, request):
)

if listen != ():
if servers.is_serving():
raise request.isnt_valid('Multiple concurrent "listen" sessions are not supported')
host = listen("host", "127.0.0.1")
port = listen("port", int)
adapter.access_token = None
host, port = servers.serve(host, port)
else:
host, port = servers.serve()
if not servers.is_serving():
servers.serve()
host, port = servers.listener.getsockname()

# There are four distinct possibilities here.
#
Expand Down
12 changes: 11 additions & 1 deletion src/debugpy/adapter/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
access_token = None
"""Access token used to authenticate with the servers."""

listener = None
"""Listener socket that accepts server connections."""

_lock = threading.RLock()

_connections = []
Expand Down Expand Up @@ -433,9 +436,16 @@ def serve(host="127.0.0.1", port=0):
return listener.getsockname()


def is_serving():
return listener is not None


def stop_serving():
global listener
try:
listener.close()
if listener is not None:
listener.close()
listener = None
except Exception:
log.swallow_exception(level="warning")

Expand Down
6 changes: 6 additions & 0 deletions src/debugpy/common/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def report_paths(get_paths, label=None):

report_paths("os.__file__")
report_paths("threading.__file__")
report_paths("debugpy.__file__")

result = "".join(result).rstrip("\n")
info("{0}", result)
Expand Down Expand Up @@ -376,3 +377,8 @@ def _vars(*names): # pragma: no cover
def _stack(): # pragma: no cover
stack = "\n".join(traceback.format_stack())
warning("$STACK:\n\n{0}", stack)


def _threads(): # pragma: no cover
output = "\n".join([str(t) for t in threading.enumerate()])
warning("$THREADS:\n\n{0}", output)

0 comments on commit 8b5eeee

Please sign in to comment.