Skip to content

Commit 978c346

Browse files
committed
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'. ```
1 parent 942f30b commit 978c346

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- Use relative paths when compiling multi-file projects.
1111

12+
### Fixed
13+
- Fixed `pybricksdev` BLE commands not working on Windows when `pythoncom`
14+
package is present in environment.
15+
1216
## [1.0.0-alpha.48] - 2024-05-04
1317

1418
### Changed

pybricksdev/cli/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ async def run(self, args: argparse.Namespace):
343343
def main():
344344
"""Runs ``pybricksdev`` command line interface."""
345345

346+
if sys.platform == "win32":
347+
# Hack around bad side-effects of pythoncom on Windows
348+
try:
349+
from bleak_winrt._winrt import MTA, init_apartment
350+
except ImportError:
351+
from winrt._winrt import MTA, init_apartment
352+
353+
init_apartment(MTA)
354+
346355
# Provide main description and help.
347356
parser = argparse.ArgumentParser(
348357
prog=PROG_NAME,

0 commit comments

Comments
 (0)