Skip to content

Commit 45e5299

Browse files
committed
Gracefully handle missing /dev/tty when setting up process group for the debuggee.
1 parent 17ff9c8 commit 45e5299

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

src/debugpy/launcher/debuggee.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,28 @@ def spawn(process_name, cmdline, env, redirect_output):
6363
if sys.platform != "win32":
6464

6565
def preexec_fn():
66-
# Start the debuggee in a new process group, so that the launcher can
67-
# kill the entire process tree later.
68-
os.setpgrp()
69-
70-
# Make the new process group the foreground group in its session, so
71-
# that it can interact with the terminal. The debuggee will receive
72-
# SIGTTOU when tcsetpgrp() is called, and must ignore it.
73-
hdlr = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
7466
try:
75-
tty = os.open("/dev/tty", os.O_RDWR)
67+
# Start the debuggee in a new process group, so that the launcher can
68+
# kill the entire process tree later.
69+
os.setpgrp()
70+
71+
# Make the new process group the foreground group in its session, so
72+
# that it can interact with the terminal. The debuggee will receive
73+
# SIGTTOU when tcsetpgrp() is called, and must ignore it.
74+
old_handler = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
7675
try:
77-
os.tcsetpgrp(tty, os.getpgrp())
76+
tty = os.open("/dev/tty", os.O_RDWR)
77+
try:
78+
os.tcsetpgrp(tty, os.getpgrp())
79+
finally:
80+
os.close(tty)
7881
finally:
79-
os.close(tty)
80-
finally:
81-
signal.signal(signal.SIGTTOU, hdlr)
82+
signal.signal(signal.SIGTTOU, old_handler)
83+
except Exception:
84+
# Not an error - /dev/tty doesn't work when there's no terminal.
85+
log.swallow_exception(
86+
"Failed to set up process group", level="info"
87+
)
8288

8389
kwargs.update(preexec_fn=preexec_fn)
8490

0 commit comments

Comments
 (0)