diff --git a/src/debugpy/launcher/debuggee.py b/src/debugpy/launcher/debuggee.py index be0a955a4..e04c9696f 100644 --- a/src/debugpy/launcher/debuggee.py +++ b/src/debugpy/launcher/debuggee.py @@ -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 diff --git a/tests/debug/session.py b/tests/debug/session.py index 50471f994..0b42302cf 100644 --- a/tests/debug/session.py +++ b/tests/debug/session.py @@ -3,6 +3,7 @@ # for license information. import collections +import ctypes import itertools import os import psutil @@ -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): diff --git a/tests/debugpy/test_run.py b/tests/debugpy/test_run.py index d6dd1a9a7..d83477c52 100644 --- a/tests/debugpy/test_run.py +++ b/tests/debugpy/test_run.py @@ -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