Skip to content

Commit

Permalink
Gracefully handle missing /dev/tty when setting up process group for …
Browse files Browse the repository at this point in the history
…the debuggee.
  • Loading branch information
int19h committed Aug 19, 2020
1 parent 17ff9c8 commit 45e5299
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/debugpy/launcher/debuggee.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,28 @@ def spawn(process_name, cmdline, env, redirect_output):
if sys.platform != "win32":

def preexec_fn():
# Start the debuggee in a new process group, so that the launcher can
# kill the entire process tree later.
os.setpgrp()

# Make the new process group the foreground group in its session, so
# that it can interact with the terminal. The debuggee will receive
# SIGTTOU when tcsetpgrp() is called, and must ignore it.
hdlr = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
try:
tty = os.open("/dev/tty", os.O_RDWR)
# Start the debuggee in a new process group, so that the launcher can
# kill the entire process tree later.
os.setpgrp()

# Make the new process group the foreground group in its session, so
# that it can interact with the terminal. The debuggee will receive
# SIGTTOU when tcsetpgrp() is called, and must ignore it.
old_handler = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
try:
os.tcsetpgrp(tty, os.getpgrp())
tty = os.open("/dev/tty", os.O_RDWR)
try:
os.tcsetpgrp(tty, os.getpgrp())
finally:
os.close(tty)
finally:
os.close(tty)
finally:
signal.signal(signal.SIGTTOU, hdlr)
signal.signal(signal.SIGTTOU, old_handler)
except Exception:
# Not an error - /dev/tty doesn't work when there's no terminal.
log.swallow_exception(
"Failed to set up process group", level="info"
)

kwargs.update(preexec_fn=preexec_fn)

Expand Down

0 comments on commit 45e5299

Please sign in to comment.