From 62501bac94dcfa2658a687b2d2c92cc9107437e1 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Fri, 12 Jul 2019 18:54:29 -0300 Subject: [PATCH] Don't try to patch arguments when not dealing with python. Fixes #1548 (#1588) * Properly try to resolve_label multiple times in attach to pid. * Don't try to patch arguments when not dealing with python. Fixes #1548 --- .../add_code_to_python_process.py | 2 + src/ptvsd/multiproc.py | 4 +- tests/func/test_multiproc.py | 53 ++++++++++++++++++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/ptvsd/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py b/src/ptvsd/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py index 159b6a923..d2f8e400b 100644 --- a/src/ptvsd/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py +++ b/src/ptvsd/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py @@ -251,6 +251,8 @@ def resolve_label(process, label): for i in range(max_attempts): try: address = process.resolve_label(label) + if not address: + raise AssertionError('%s not resolved.' % (label,)) return address except: try: diff --git a/src/ptvsd/multiproc.py b/src/ptvsd/multiproc.py index 30859ba88..1d8b0f20b 100644 --- a/src/ptvsd/multiproc.py +++ b/src/ptvsd/multiproc.py @@ -28,7 +28,6 @@ from _pydev_bundle import pydev_monkey from _pydevd_bundle.pydevd_comm import get_global_debugger - subprocess_lock = threading.Lock() subprocess_listener_socket = None @@ -136,6 +135,7 @@ def _subprocess_listener(): def _handle_subprocess(n, stream): + class Handlers(object): _pid = None @@ -250,6 +250,8 @@ def patch_args(args): for i, arg in enumerate(args): # Skip Python binary. if i == 0: + if not pydev_monkey.is_python(arg): + return args # We're not dealing with Python, so, don't do anything. continue if arg == '-': diff --git a/tests/func/test_multiproc.py b/tests/func/test_multiproc.py index 4017c15f4..09ccda6d0 100644 --- a/tests/func/test_multiproc.py +++ b/tests/func/test_multiproc.py @@ -18,6 +18,7 @@ reason='Debugging multiprocessing module only works on Windows') @pytest.mark.parametrize('start_method', ['launch', 'attach_socket_cmdline']) def test_multiprocessing(pyfile, run_as, start_method): + @pyfile def code_to_debug(): import multiprocessing @@ -127,6 +128,7 @@ def child(q): reason='Bug #935') @pytest.mark.parametrize('start_method', ['launch', 'attach_socket_cmdline']) def test_subprocess(pyfile, run_as, start_method): + @pyfile def child(): import sys @@ -186,6 +188,7 @@ def parent(): reason='Bug #935') @pytest.mark.parametrize('start_method', ['launch', 'attach_socket_cmdline']) def test_autokill(pyfile, run_as, start_method): + @pyfile def child(): from dbgimporter import import_and_enable_debugger @@ -231,10 +234,11 @@ def parent(): @pytest.mark.skipif(sys.version_info < (3, 0) and (platform.system() != 'Windows'), reason='Bug #935') def test_argv_quoting(pyfile, run_as, start_method): + @pyfile def args(): # import_and_enable_debugger - args = [ # noqa + args = [ # noqa r'regular', r'', r'with spaces' @@ -286,3 +290,50 @@ def child(): assert expected_args == actual_args session.wait_for_exit() + + +def test_echo_and_shell(pyfile, run_as, start_method): + ''' + Checks https://github.com/microsoft/ptvsd/issues/1548 + ''' + + @pyfile + def code_to_run(): + from dbgimporter import import_and_enable_debugger + import_and_enable_debugger() + + import sys + import subprocess + import os + + if sys.platform == 'win32': + args = ['dir', '-c', '.'] + else: + args = ['ls', '-c', '-la'] + + p = subprocess.Popen( + args, + shell=True, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + cwd=os.path.dirname(os.path.abspath(__file__)), + ) + stdout, _stderr = p.communicate() + if sys.version_info[0] >= 3: + stdout = stdout.decode('utf-8') + + if "code_to_run.py" not in stdout: + raise AssertionError( + 'Did not find "code_to_run.py" when listing this dir with subprocess. Contents: %s' % ( + stdout,) + ) + + with DebugSession() as session: + session.multiprocess = True + session.initialize( + target=(run_as, code_to_run), + start_method=start_method, + ) + + session.start_debugging() + session.wait_for_exit()