pywinauto: [pytest integration] (Windows fatal exception: code 0x80010108) @ comtypes

Description

Running pytest with pywinauto causes an exception in shutdown of comtypes. Tests are collected / run correctly, but at the end there’s always an exception output.

This breaks test discovery in VSCode, though I can work around it by moving import statements to the inside of fixtures / functions.

I am not sure if this is an issue with how I am using pytest and pywinauto, or its a bug.

I tried python 3.6 (Visual Studio 2017), 3.7 (Anaconda3), and 3.8 (python.org).

Example code

File test_case.py

import pytest
from pywinauto.application import Application

def test_case():
    assert True

Test discovery

In the directory containing test_case.py python -m pytest --collect-only

Output

========================================================================================= test session starts ========================================================================================== 
platform win32 -- Python 3.8.0, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: C:\Users\Yujin\Desktop\pytest_pywinauto
collected 1 item
<Module test_case.py>
  <Function test_case>

=========================================================================================== warnings summary =========================================================================================== 
C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\win32\lib\pywintypes.py:2
  C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\win32\lib\pywintypes.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp, sys, os

-- Docs: https://docs.pytest.org/en/latest/warnings.html
========================================================================================== 1 warning in 0.17s ========================================================================================== 
Windows fatal exception: code 0x80010108

Thread 0x0000625c (most recent call first):
  File "C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\comtypes\__init__.py", line 185 in shutdown
Windows fatal exception: code 0x80010108

Thread 0x0000625c (most recent call first):
  File "C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\comtypes\__init__.py", line 185 in shutdown

Running the test

python -m pytest

C:\Users\Yujin\Desktop\pytest_pywinauto>"C:/Program Files/Python38/python.exe" -m pytest
========================================================================================= test session starts ========================================================================================== 
platform win32 -- Python 3.8.0, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: C:\Users\Yujin\Desktop\pytest_pywinauto
collected 1 item                                                                                                                                                                                         

test_case.py .                                                                                                                                                                                    [100%] 

=========================================================================================== warnings summary =========================================================================================== 
C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\win32\lib\pywintypes.py:2
  C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\win32\lib\pywintypes.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp, sys, os

-- Docs: https://docs.pytest.org/en/latest/warnings.html
===================================================================================== 1 passed, 1 warning in 0.18s ===================================================================================== 
Windows fatal exception: code 0x80010108

Thread 0x00005e8c (most recent call first):
  File "C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\comtypes\__init__.py", line 185 in shutdown
Windows fatal exception: code 0x80010108

Thread 0x00005e8c (most recent call first):
  File "C:\Users\Yujin\AppData\Roaming\Python\Python38\site-packages\comtypes\__init__.py", line 185 in shutdown

Specifications

  • pywin32 version: 227
  • comtypes version: 1.1.7
  • Pytest version: 5.3.1
  • Pywinauto version: 0.6.8
  • Python version and bitness: 3.8 (64 bit) (also tried 3.6, 3.7)
  • Platform and OS: Win10 x64

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 20 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I started pytest with -p no:faulthandler and it helped me. Look at this for more information.

Ok, so this is the minimum code to reproduce the error:

import comtypes
import comtypes.client

UIA_dll = comtypes.client.GetModule('UIAutomationCore.dll')
ui_automation_client = comtypes.gen.UIAutomationClient
iuia = comtypes.CoCreateInstance(
    ui_automation_client.CUIAutomation().IPersist_GetClassID(),
    interface=ui_automation_client.IUIAutomation,
    clsctx=comtypes.CLSCTX_INPROC_SERVER,
).GetRootElement()


def test_case():
    assert True

This above snippet is modified from the below pywinauto code:

https://github.com/pywinauto/pywinauto/blob/4005bff2aacb6e5421a43abdbdd2f4c178ca53b4/pywinauto/windows/uia_defines.py#L43-L64

I’m a little out of my depth here. From what I can piece together, this only occurs when I call the above snippet (the simplified code from pywinauto–no need to import pywinauto) and use pytest. Calling the snippet with Python doesn’t throw the fatal exception.

I can’t locate what calls the shutdown() method from comtypes that is reporting the Fatal exception on couninitialize. I tried to call the snippet with multiprocess, but I haven’t been able to create an example without pytest

What do you think? What could I try for next steps?

Seems to be also reported on StackOverflow: https://stackoverflow.com/questions/57523762/pytest-windows-fatal-exception-code-0x8001010d

The error: 0x80010108 - RPC_E_DISCONNECTED reported for many other cases as well. Examples: https://stackoverflow.com/questions/47429782/rpc-e-disconnected-error0x80010108-while-calling-a-callback-aftre-6-mins https://stackoverflow.com/questions/38964478/comexception-the-object-invoked-has-disconnected-from-its-clients

For starters, I would try to enable debugs on comtypes and run the simplest test again. Notice that the code below won’t be enough since the pywinauto sets comtypes logging explicitly to ‘warning’ level. So this line in pywinauto soureces should be commented: https://github.com/pywinauto/pywinauto/blob/bea648b134664f2dc4ce27b30d628f86bd8b5a26/pywinauto/sysinfo.py#L43

import logging
logger = logging.getLogger('comtypes')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

import pytest
from pywinauto.application import Application

def test_case():
    assert True

Edit: updated logging code.