Fix #1810: tests using attach_by_pid fail on Linux and macOS

Add time.sleep() to attach_by_pid spinning loop to give the injected code a better chance to execute.

Read and log injector output to prevent it from blocking on prints.

Improve code injection logging.

Fix race between socket.accept() backchannel listener thread, and debug.Session.close().
This commit is contained in:
Pavel Minaev 2019-11-12 18:40:06 -08:00 committed by Pavel Minaev
parent 672c310905
commit 646981e4af
4 changed files with 41 additions and 18 deletions

View file

@ -46,6 +46,11 @@ class BackChannel(object):
sock, _ = server_socket.accept()
except socket.timeout:
raise log.exception("Timed out waiting for {0} to connect", self)
except Exception:
if self._server_socket is None:
return
else:
raise
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
log.info("Incoming connection from {0} accepted.", self)

View file

@ -152,9 +152,7 @@ def _attach_common_config(session, target, cwd):
@_runner
def attach_by_pid(session, target, cwd=None, wait=True):
if platform.system() != "Windows":
pytest.skip("https://github.com/microsoft/ptvsd/issues/1810")
if sys.version_info < (3,):
if sys.version_info < (3,) and platform.system() == "Windows":
pytest.skip("https://github.com/microsoft/ptvsd/issues/1811")
log.info("Attaching {0} to {1} by PID.", session, target)
@ -172,9 +170,16 @@ def attach_by_pid(session, target, cwd=None, wait=True):
if wait:
debug_me = """
import sys
while not "ptvsd" in sys.modules: pass
import threading
import time
while not "ptvsd" in sys.modules:
time.sleep(0.1)
import ptvsd
while not ptvsd.is_attached(): pass
while not ptvsd.is_attached():
time.sleep(0.1)
"""
else:
debug_me = None