Skip to content

Process creation after debugging session not possible: teardown needed #2014

@Lexcon

Description

@Lexcon

Environment data

  • debugpy version: latest
  • OS and version: Windows 11
  • Python version 314 (but issue is not python-version related)
  • Using VS Code

Actual behavior

Connect to VSCode. Then apply

test = subprocess.Popen(
["C:\Python314\python.exe", "-c", "import sys; sys.exit(0)"],
creationflags=creationflags,
)
test.wait()
print(test.returncode)

this does not work because subprocess was patched. Also not after teardown of the debugging session:

from debugpy._vendored.pydevd.pydevd import stoptrace
stoptrace() to continue running the process. Now, subprocess.popen() does not work any more at al.
.... still subprocess.Popen will not work

Expected behavior

subprocess.Popen() should work

Steps to reproduce:

After debugging started, the following won't work. Make sure NOT to issue
debugpy.configure(subProcess=False) in order to reproduce this:

test = subprocess.Popen(
["C:\Python314\python.exe", "-c", "import sys; sys.exit(0)"],
creationflags=creationflags,
)
test.wait()
print(test.returncode)

SOLUTION
pydevd's patch_new_process_functions() patches _winapi.CreateProcess to intercept child process creation for subprocess debugging, but stoptrace() never unpatches it.

The effect is that you can't spawn processes after a debugging session is torn down and you want to resume your program without restart. The solution is a restoration on teardown:

from debugpy._vendored.pydevd.pydevd import stoptrace
stoptrace()
debugpy.server.api.listen.called = False # reset state so it can reconnect later
try:
	import _winapi as _subprocess
except ImportError:
	import _subprocess
if hasattr(_subprocess, 'original_CreateProcess'):
	_subprocess.CreateProcess = _subprocess.original_CreateProcess
	del _subprocess.original_CreateProcess		

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions