Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/debugpy/launcher/debuggee.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ def wait_for_exit():
# bogus clean exit (code 0) if that invariant is ever broken.
assert process is not None
code = process.wait()
if sys.platform != "win32" and code < 0:
if sys.platform == "win32":
# On Windows, the exit code is an unsigned 32-bit DWORD, but DAP requires
# "exitCode" to be a signed 32-bit integer. Keep the same bits, so that
# e.g. 0xC000013A (STATUS_CONTROL_C_EXIT) is reported as -1073741510.
code = ctypes.c_int32(code).value
elif code < 0:
# On POSIX, if the process was terminated by a signal, Popen will use
# a negative returncode to indicate that - but the actual exit code of
# the process is always an unsigned number, and can be determined by
Expand Down
7 changes: 6 additions & 1 deletion tests/debug/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# for license information.

import collections
import ctypes
import itertools
import os
import psutil
Expand Down Expand Up @@ -988,7 +989,11 @@ def wait_for_exit(self):
# FIXME: "exited" event is not properly reported in attach scenarios at the
# moment, so the exit code is only checked if it's present.
if self.debuggee is not None and self.exit_code is not None:
assert self.debuggee.returncode == self.exit_code
returncode = self.debuggee.returncode
if sys.platform == "win32":
# Popen reports the unsigned DWORD, but "exited" reports it as int32.
returncode = ctypes.c_int32(returncode).value
assert returncode == self.exit_code
return self.exit_code

def wait_for_terminated(self):
Expand Down
19 changes: 19 additions & 0 deletions tests/debugpy/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,22 @@ def test_unicode_dir(tmpdir, run, target):

received = backchannel.receive()
assert received == "ok"


@pytest.mark.parametrize("run", runners.all_launch)
def test_exit_code(pyfile, target, run):
@pyfile
def code_to_debug():
import sys

import debuggee

debuggee.setup()
sys.exit(-1)

with debug.Session() as session:
# On Windows, sys.exit(-1) sets the exit code to the DWORD 0xFFFFFFFF.
# DAP requires "exitCode" to be a signed 32-bit integer, so it must be -1.
session.expected_exit_code = -1 if sys.platform == "win32" else 255
with run(session, target(code_to_debug)):
pass