VSCode debugger not releasing COM object reference when a Python script throws an exception

Question:

I’ve observed a strange behavior where VSCode does not release COM object references while debugging. PyCharm works fine. To see this in action do the following:

Start Excel (the problem occurs with other apps too, I’m using Excel for this example only because most people have it).

From the command line, run the script "excel_test.py" below. It attaches to the running Excel application and tries to call a non-existent method hi(). An exception is thrown and the script will exit, as expected.

import win32com.client as w32c

# Attach to a running Excel
app = w32c.GetActiveObject("Excel.Application")

# This will throw an exception
app.hi()

print("done")

Quit Excel manually by closing it via the UI. Note in the task manager that the Excel.exe process has shut down. This is all as expected.

Repeat the same steps but run the script using VSCode debug mode. When the exception is thrown the debugger halts execution. Press the Stop button on the debugger tool bar to terminate the script. After quitting Excel, notice that the UI has shutdown, but the Excel.exe process is still running. You now have a zombie process.

Following this same pattern in PyCharm debug mode works fine, the process goes away as expected.

Somehow it appears that VSCode is not properly releasing the COM object reference when exiting debug mode. Am I missing some weird setting or something, or is it just broken?

I’ve also opened an issue under the vscode-debugpy. https://github.com/microsoft/debugpy/issues/1022

I’m running Python 3.9.12, pywin32 302, VSCode 1.70.2.

Asked By: Tom Johnson

||

Answers:

Good news, it looks like a fix has been submitted and the issue https://github.com/microsoft/debugpy/issues/1022 has been closed. I don’t see a target release yet though. I’ll update this when we know.

Answered By: Tom Johnson