Skip to content

exited event reports Windows exit codes as uint32, outside the int32 range of the DAP schema #2102

Backlog

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:

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:

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:

  1. On Windows, create exit.py:
    import sys
    sys.exit(-1)
  2. Debug it in VS Code with this configuration:
    {
        "name": "exit code",
        "type": "debugpy",
        "request": "launch",
        "program": "${workspaceFolder}/exit.py",
        "logToFile": true
    }
    If the debugger stops on SystemExit, continue.
  3. Open debugger.vscode_<session id>.log in the install folder of the debugpy extension. The exited event has "exitCode": 4294967295, not -1.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions