Repository navigation
Description
Environment data
- debugpy version: XXX
- OS and version: Windows 11 x64 (10.0.26200)
- Python version (& distribution if applicable, e.g. Anaconda): XXX
- Using VS Code or Visual Studio: VS Code 1.148.0. VS Code shows no error, because a JavaScript number holds the value. We first saw the error in the PyCharm DAP client.
Actual behavior
On Windows, the launcher sends the raw exit code of the debuggee in the exited event.
A Windows exit code is an unsigned 32-bit DWORD, and Popen.wait() returns it unsigned (_winapi.GetExitCodeProcess uses PyLong_FromUnsignedLong).
So a code with the high bit set arrives as a value above 2^31 - 1:
{"type": "event", "event": "exited", "body": {"exitCode": 3221225786}}3221225786 is 0xC000013A (STATUS_CONTROL_C_EXIT).
The DAP schema declares exitCode as "type": "integer", "format": "int32" (microsoft/debug-adapter-protocol#583).
A client that decodes the field as a 32-bit integer rejects the event. Our client failed with:
JsonDecodingException: Failed to parse literal '3221225786' as an int value at path: $.exitCode
The POSIX branch normalizes the code, but the Windows branch does not:
debugpy/src/debugpy/launcher/debuggee.py
Lines 232 to 238 in 7764458
| code = process.wait() | |
| if sys.platform != "win32" and 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 | |
| # taking the lowest 8 bits of that negative returncode. | |
| code &= 0xFF |
The adapter forwards the value unchanged:
debugpy/src/debugpy/adapter/launchers.py
Lines 46 to 55 in 7764458
| self.exit_code = event("exitCode", int) | |
| # We don't want to tell the client about this just yet, because it will then | |
| # want to disconnect, and the launcher might still be waiting for keypress | |
| # (if wait-on-exit was enabled). Instead, we'll report the event when we | |
| # receive "terminated" from the launcher, right before it exits. | |
| @message_handler | |
| def terminated_event(self, event): | |
| try: | |
| self.client.channel.send_event("exited", {"exitCode": self.exit_code}) |
Expected behavior
exitCode stays in the int32 range.
A Windows code at or above 2^31 maps to the signed int32 with the same bits: 0xC000013A becomes -1073741510, and 0xFFFFFFFF becomes -1.
Steps to reproduce:
- On Windows, create
exit.py:import sys sys.exit(-1)
- Debug it in VS Code with this configuration:
If the debugger stops on
{ "name": "exit code", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/exit.py", "logToFile": true }SystemExit, continue. - Open
debugger.vscode_<session id>.login the install folder of the debugpy extension. Theexitedevent has"exitCode": 4294967295, not-1.
Activity