Skip to content

Fix subprocess patching for scripts run through a python shebang - #2106

Open
TEJA P C (PCTEJA) wants to merge 4 commits into
microsoft:mainfrom
PCTEJA:fix-shebang-subprocess-1929
Open

TEJA P C (PCTEJA) wants to merge 4 commits into
microsoft:mainfrom
PCTEJA:fix-shebang-subprocess-1929

Conversation

@PCTEJA

Copy link
Copy Markdown
Contributor

Fixes #1929

Script subprocesses launched through a python shebang were not being debugged: patch_args returned the arguments unchanged when the executable was a script rather than a known python name, so pydevd was never inserted.

This change parses the shebang when the executable name is not a known python name, covering the direct form, the /usr/bin/env form and the env -S form, and rewrites the args to run the script through that interpreter before the existing patching runs. The process creation hooks also replace the executable with the interpreter in that case, since they launch the executable separately from the args (for fork_exec, the executable list passed to it is replaced instead).

This follows the narrower approach discussed on #1929 and stays isolated to the patching code in pydev_monkey.py.

Tests: 20 new regression tests in test_pydev_monkey.py. The relevant test set passes twice with the same result (61 passed), and the new tests fail on the unmodified base.

patch_args returned the arguments unchanged when the executable was a
script whose first line is a shebang pointing at a python interpreter,
so such subprocesses were never attached to the debugger.

When the executable name is not a known python name, parse the file's
shebang (the direct form, the /usr/bin/env form and the env -S form)
and rewrite the args to run the script through that interpreter
before the existing patching runs. The process creation hooks also
replace the executable with the interpreter in that case, since they
launch the executable separately from the args (for fork_exec, the
executable list passed to it is replaced instead).
@PCTEJA
TEJA P C (PCTEJA) requested a review from a team as a code owner November 30, 2026 18:48
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@PCTEJA

Copy link
Copy Markdown
Contributor Author

Rich Chiodo (@rchiodo) opened this following your reply on #1929. The fix is isolated to the patching code, as discussed. Could you please review when you are free?

@rchiodo

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Launch env shebang scripts through the env program instead of
resolving the interpreter, reuse os.get_exec_path for child
environment PATH handling, patch based on the executable actually
selected for a launch when it differs from args[0], share one
fork_exec patching helper between both hooks, and select the first
viable entry of the fork_exec executable list.
@PCTEJA

Copy link
Copy Markdown
Contributor Author

Rich Chiodo (@rchiodo), I made a little correction to the changes you requested on 1, 4 & 5.

pls review it when your free.

return None
first_line = first_line.strip()

line = first_line[2:].decode("utf-8", "ignore").strip()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This decoding and split() can normalize malformed shebangs into valid Python launches. I verified that both #!/usr/bin/python3\xff and a vertical-tab-separated shebang fail natively on Linux, but are parsed and rewritten here. Could parsing follow the kernel's byte-level separator rules and reject bytes/whitespace it would not accept, with regression tests for those cases?

@PCTEJA

Copy link
Copy Markdown
Contributor Author

Rich Chiodo (@rchiodo) wil drop the viability probing and make the shebang parsing follow the kernels bytelevel rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Script subprocesses cannot be debugged

2 participants