-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
_winrt.init_apartment() RuntimeError: Cannot change thread mode after it is set. #690
Comments
Hi, same error here but when running scripts with a debugger attached. import winrt.windows.foundation as wf And it prints (in VSCode)
My system language is Chinese, the last line translates to |
I may not get time to debug this right away, but I can give you a few quick tips that might help> WinRT users RoInitialize, which is basically just a wrapper around CoInitialize/ CoInitializeEx. If _winrt.init_apartment is failing due to having already been set, you should be able to debug by running the process under the VS or WinDbg debugger, and setting a breakpoint on three fucntions: combase!CoInitialize, combase!RoInitialize, and combase!CoInitializeEx. You might only actually need the last one. The error suggests that something else you're importing is already initializing COM, but defaulting to a different mode. Breaking on combase entry points should allow you to see when that's happening, and see which modes are being set. PyWinRT should be tolerant of running in different modes though. This is something that I should be able to address (and probably add an API to check mode, since it will matter for some apps). PyWinRT defaults to MTA. It's likely the case that some other module that you're loading defaulted to STA and loaded first. I'll leave the issue open to address this in a future update. In the meantime I hope that helps you diagnose your issue. |
I have run into this issue as well. In this case, I was able to get |
I like the suggestion. We're not going to have time to look at this soon, so I'm going to mark this as a good first issue. The code in question is currently using C++ /WinRT's init_apartment helper. See: The work would be to change to check the python property, and call CoInitializeEx. It should also probably handle RPC_E_CHANGED_MODE gracefully and avoid calling CoUninitialize in that case as well. |
Hello folks, did you find a solution for this one? |
We found that simply using the API on a separate thread worked for the minimal use it is being used for in Anki. |
Ideally, importing modules should not have side effects. Having So, I think the options here are:
|
It seems that this is not needed. It could be that cppwinrt calls CoIncrementMTAUsage implicitly (as in idea suggested in contextfree/winrt-rust#62 (comment)). Fixes: microsoft#690
This defect also caused us a bit of headache in our Sphinx (autodoc) documentation generation. We have a series of programs and libraries on our program; while none of the runtime combinations are problematic, the combined imports generating our documentation is. Since this dependency is only needed inside a function body, we ended up putting try/except around the import to work around this. |
I actually found out recently that the explicit call to For the Sphinx autodoc issue though, autodoc_mock_imports is probably a better solution. |
This issue is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
Please see this repo for Python support: https://github.com/pywinrt/pywinrt |
It's amazing that this can go unfixed. The code on the WinRT pypi page (https://pypi.org/project/winrt/) won't run in VSCode. Going to the file that calls init_apartment has a big WARNING: Please don't edit How is any dev to know what they're supposed to do here? |
Use https://pypi.org/project/winsdk/ instead of https://pypi.org/project/winrt/. |
Hi All,
We are trying to invoke winrt apis using remote rpyc calls in remote machine. When we tried to inistialize the
objects we faced error
Error added: <Error: _get_exception_class..Derived: Cannot change thread mode after it is set.
RuntimeError: Cannot change thread mode after it is set.
and observed the following error in call stack
========= Remote Traceback (1) =========
Traceback (most recent call last):
File "C:\Users\itl\pytm_win\pytm_win\lib\site-packages\rpyc\core\protocol.py", line 323, in _dispatch_request
res = self._HANDLERS[handler](self, *args)
File "C:\Users\itl\pytm_win\pytm_win\lib\site-packages\rpyc\core\protocol.py", line 585, in handle_call
return obj(args, **dict(kwargs))
File "C:\Users\itl\pytm_win\pytm_win\lib\site-packages\rpyc\core\service.py", line 160, in getmodule
return import(name, None, None, "")
File "C:\Users\itl\pytm_win\pytm_win\lib\site-packages\winrt_init.py", line 4, in
_winrt.init_apartment()
RuntimeError: Cannot change thread mode after it is set.>
Could someone help us to get it solved?
Regards,
Vinoth P B
The text was updated successfully, but these errors were encountered: