Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pybricksdev.cli: initialize thread to MTA on Windows
This fixes crashes like the following due to changes in Bleak that detects issues on Windows where callbacks won't be called because the pythoncom package sets the threading model to STA and there is no Windows message loop running. By initializing the thread to MTA before pythoncom is (lazy) imported, we avoid this issue. Issue: hbldh/bleak#1607 (comment) ``` Traceback (most recent call last): File "C:\Users\david\work\pybricksdev\.venv\Scripts\\pybricksdev", line 6, in <module> sys.exit(main()) ^^^^^^ File "C:\Users\david\work\pybricksdev\pybricksdev\cli\__init__.py", line 387, in main asyncio.run(subparsers.choices[args.tool].tool.run(args)) File "C:\Users\david\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\david\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\david\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 650, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\Users\david\work\pybricksdev\pybricksdev\cli\lwp3\repl.py", line 134, in repl device = await BleakScanner.find_device_by_filter(match_lwp3_uuid) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\david\work\pybricksdev\.venv\Lib\site-packages\bleak\__init__.py", line 444, in find_device_by_filter async with cls(**kwargs) as scanner: File "C:\Users\david\work\pybricksdev\.venv\Lib\site-packages\bleak\__init__.py", line 158, in __aenter__ await self._backend.start() File "C:\Users\david\work\pybricksdev\.venv\Lib\site-packages\bleak\backends\winrt\scanner.py", line 225, in start await assert_mta() File "C:\Users\david\work\pybricksdev\.venv\Lib\site-packages\bleak\backends\winrt\util.py", line 149, in assert_mta raise BleakError( bleak.exc.BleakError: Thread is configured for Windows GUI but callbacks are not working. Suspect unwanted side effects from importing 'pythoncom'. ```
- Loading branch information