Skip to content

Restore foreground process group when the debuggee exits - #2096

Merged
Rich Chiodo (rchiodo) merged 3 commits into
microsoft:mainfrom
PCTEJA:fix-launcher-restore-foreground-2095
Nov 24, 2026
Merged

Rich Chiodo (rchiodo) merged 3 commits into
microsoft:mainfrom
PCTEJA:fix-launcher-restore-foreground-2095

Conversation

@PCTEJA

Copy link
Copy Markdown
Contributor

Fixes #2095.

When the launcher spawns the debuggee, the preexec in launcher/debuggee.py puts the debuggee in a new process group and makes that group the foreground group of the terminal with tcsetpgrp. Nothing ever set it back, so when the debuggee exited, the terminal was left with a dead foreground group. Shells with job control (bash) reclaim the foreground themselves, but pwsh has no Unix job control, so its next prompt read fails with an Input/output error and the debug terminal dies at the end of the run.

This change restores the foreground group to the launcher's own group in wait_for_exit, right after the debuggee exits:

  • _restore_foreground_process_group() only acts when the current foreground group is still the debuggee's group, so it never takes the terminal away from anything else.
  • It runs in the wait_for_exit thread, where signal.signal() cannot be used, so SIGTTOU is blocked in that thread with pthread_sigmask while calling tcsetpgrp.
  • Like the spawn preexec, it swallows failures when there is no terminal.

Tests: new pty regression test in tests/tests/test_launcher_debuggee.py drives the real debuggee.spawn() and checks that the foreground group is the launcher's group again after the debuggee exits. It fails without the fix and passes with it. The full tests/tests suite passes (67 tests), and the test_run launch tests for integratedTerminal and internalConsole pass.

@PCTEJA
TEJA P C (PCTEJA) requested a review from a team as a code owner November 20, 2026 00:14
@PCTEJA

TEJA P C (PCTEJA) commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Rich Chiodo (@rchiodo) the fix is up, ready for your review when you have a moment. Pavel Minaev (@int19h) tagging you as well since you have worked on the launcher.

@azure-pipelines

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

@rchiodo

Copy link
Copy Markdown
Contributor

I think this can take the terminal away from a still-running subprocess. process.wait() only waits for the debuggee itself, and checking that the foreground group is process.pid doesn't mean the rest of that group has exited.

I reproduced this with the PR's launcher on WSL: the debuggee starts a child that inherits stdin/stdout and exits; with redirectOutput enabled, the child successfully reads from the terminal without the change, but gets OSError: [Errno 5] Input/output error with it.

Could we move the restore until after output.wait_for_remaining_output()? The launcher already waits there for inherited output pipes to close. In my repro, that lets the child read and still restores the foreground group before the terminated event. A regression test with a surviving child would help cover this.

That doesn't settle the case where output isn't captured, or a child closes the captured streams but keeps using the terminal. Those cases may need an explicit decision about when to return terminal ownership to the shell versus keeping it with a surviving child.

@PCTEJA

Copy link
Copy Markdown
Contributor Author

Yes Rich Chiodo (@rchiodo), process.wait() only covers the debuggee, the child processes are still in the same group.

I will move the restore to after output.wait_for_remaining_output() and add a regression test with a surviving child.

Comment thread src/debugpy/launcher/debuggee.py Resolved
@rchiodo

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

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

@PCTEJA

Copy link
Copy Markdown
Contributor Author

Rich Chiodo (@rchiodo) This is also ready to be merged. The only red check is the Windows py312 job, which was cancelled on Azure's 40 minute timeout at 98% with no test failure, and all other jobs passed.

@rchiodo
Rich Chiodo (rchiodo) merged commit 7764458 into microsoft:main Nov 24, 2026
24 of 26 checks passed
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.

Debugging to the end of a file causes the debug terminal to be killed

2 participants