-
Notifications
You must be signed in to change notification settings - Fork 304
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
Fails to get services on Windows 11 when pkg_resources is imported and pywin32 package is installed #1132
Comments
Is this still a problem with Bleak v0.19.5? If it is, we will need to see debug logs and Bluetooth packet captures to see what is going on. |
Same problem with bleak 0.19.5. Log below. What are debug logs? Something I can enable? How? The test program doesn't rely on specific device properties. Can you try to reproduce it on your side with whatever BLE device you may have?
|
I figured out how to enable debug logging. Here it is
|
We really need the Bluetooth packet logs to see what is going on behind the scenes. Usually, this failure is caused by devices not properly responding to the requests Windows is making. See https://bleak.readthedocs.io/en/latest/troubleshooting.html#windows-10 |
Attached is a zip file with packet captures from bleak_test_good.py and bleak_test_broken.py respectively. Please let me know if you need anything else. |
... I was also able to replicate the issue with an arbitrary connectable BLE device that advertised in my neighborhood. It's very likely that you will be able to reproduce it on your side without having my specific device. |
Thanks for the logs. I don't see any obvious differences between the "good" and "broken" ones. Do you have better luck if you make the timeout longer? I'm not able to reproduce with any of my local devices. |
This screenshot shows the last matching row between the good (left) and bad (right). Everything after that is different. Does it help? It seems that the left (good) has a few HCI events that are not on the right side. https://i.imgur.com/rMlEpBg.png Also, can you post here your python and OS version and your pip freeze? I will try to reproduce with your configuration. |
It just looks like stopping scanning is happening at a different time. All of the same requests and responses are made of the device. The "broken" one disconnects earlier, I assume because of the timeout, which is why I suggested extending the timeout to see if it makes a difference. |
I see. Here it is. I increased the timeouts from 5secs to 60secs and behaviors are similar. Broken log: https://pastebin.com/WGMzR5Jp |
... long shot in the dark, can it be related to ipython/ipykernel#824 since the difference between the good and bad tests is the import of pkg_resources ? |
Doh, I missed that part. Can you check |
I was not sure how to check asyncio.get_event_loop_policy() so used a simple print. Please let me know if I should do anything else. https://github.com/zapta/ble_stepper_motor_analyzer/blob/main/python/bleak_test_broken.py#L33 https://pastebin.com/nPJuzVxP // log for good and broken tests. |
Thanks. I went back and set up an environment like you described and I am able to reproduce the problem. There seems to be some strange side effect that is breaking cancellation in asyncio or winrt. I will keep digging. |
Great! Thanks David.
…On Sun, Nov 20, 2022 at 7:46 PM David Lechner ***@***.***> wrote:
Thanks. I went back and set up an environment like you described and I am
able to reproduce the problem. There seems to be some strange side effect
that is breaking cancellation in asyncio or winrt. I will keep digging.
—
Reply to this email directly, view it on GitHub
<#1132 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAQVMQJV3CJNIPYOURBMHP3WJLWC3ANCNFSM6AAAAAASFQIN2I>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
I made some progress in understanding the issue (quite by accident) by printing the thread in the logs with The problem is that it is hanging waiting for So, if we can figure out what side effect is breaking cancel on |
I am not familiar with Python runtime but here is a data point that may
help. I just tested this on my Win 11 box.
1. Remove all python packages such that pip freeze shows an empty list.
2. Install the packages listed here
https://github.com/zapta/ble_stepper_motor_analyzer/blob/main/python/requirements_good.txt
. Now bleak_test_broken.py will connect with no errors.
3. Install the package ipykernel (which will pull a bunch of dependencies).
Now bleak_test_broken.py will fail again.
This suggests that installing ipykernel changes the behavior of the system
somehow. May it be related to this
ipython/ipykernel#824 ?
…On Sun, Nov 20, 2022 at 8:36 PM David Lechner ***@***.***> wrote:
I made some progress in understanding the issue (quite by accident) by
printing the thread in the logs with %(threadName)s (note to self, add
this to BLEAK_LOGGING) and adding a bunch more debug logs to trace the
program flow.
The problem is that it is hanging waiting for get_gatt_services_async()
to cancel. When things are working correctly and get_gatt_services_async()
is cancelled, then the completed callback of the IAsyncOperation is
called on a "dummy" thread. When we get the timeout, this callback is not
called until the device disconnects. Then somehow it is called on the main
thread when the device is disconnected (thinking about it just now,
probably calling close() on the winrt objects does this).
So, if we can figure out what side effect is breaking cancel on
get_gatt_services_async(), we can solve the issue.
—
Reply to this email directly, view it on GitHub
<#1132 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAQVMQJFYTZXFBH73HLZUPLWJL36NANCNFSM6AAAAAASFQIN2I>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
... If you will confirm that its ipykernel related, maybe you can find a relevant issue here https://github.com/ipython/ipykernel/issues |
Some other reports of GetGattServicesAsync hanging forever... |
I think I have narrowed this down to the This is a sufficient
|
That's a very narrow requirements list. Very good.
Does it make sense to try different versions to see if it's a regression?
E.g. https://pypi.org/project/pywin32/#history
…On Mon, Nov 21, 2022 at 3:11 PM David Lechner ***@***.***> wrote:
I think I have narrowed this down to the pywin32 package.
This is a sufficient requirements.txt to reproduce the problem:
bleak
pywin32
—
Reply to this email directly, view it on GitHub
<#1132 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAQVMQJP3IZ6OINMU6HUWCTWJP6S7ANCNFSM6AAAAAASFQIN2I>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
I ran the test program with https://setuptools.pypa.io/en/latest/pkg_resources.html says:
So I think the easiest solution is to use avoid using But it would be nice if we could detect this problem somehow to help unsuspecting users. I'm guessing that it would be something similar to OpenBluetoothToolbox/SimpleBLE#31 (comment) where |
Thanks David. I think that no using pkg_resources is a reasonable
resolution. Thanks!
How do you plan to detect it, detecting that pkg_resources is imported
somewhere or detecting the condition in causes?
…On Tue, Nov 22, 2022 at 10:11 AM David Lechner ***@***.***> wrote:
I ran the test program with python -m trace --trace .\test.py > trace.log
to find where something from the pywin32 package was being invoked. It is
happening through the vendored copy of appdirs in the pkg_resources
package.
https://setuptools.pypa.io/en/latest/pkg_resources.html says:
Use of pkg_resources is discouraged in favor of importlib.resources
<https://docs.python.org/3/library/importlib.html#module-importlib.resources>,
importlib.metadata
<https://docs.python.org/3/library/importlib.metadata.html>, and their
backports (importlib_resources
<https://pypi.org/project/importlib_resources>, importlib_metadata
<https://pypi.org/project/importlib_metadata>). Please consider using
those libraries instead of pkg_resources.
So I think the easiest solution is to use avoid using pkg_resources.
But it would be nice if we could detect this problem somehow to help
unsuspecting users.
I'm guessing that it would be something similar to OpenBluetoothToolbox/SimpleBLE#31
(comment)
<OpenBluetoothToolbox/SimpleBLE#31 (comment)>
where pywin32 is somehow messing with the COM threading configuration.
—
Reply to this email directly, view it on GitHub
<#1132 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAQVMQJGFD7JZ5RULQ3AJU3WJUED7ANCNFSM6AAAAAASFQIN2I>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
The first step would be to call |
This fixes a strange bug in bleak: hbldh/bleak#1132
The same thing happens when win32com is imported instead of pgk_resources. |
The pywin32-related packages will implicitly set the threading model to STA when imported. Since Bleak is using WinRT for async methods and we don't have a Windows event loop running, we need to let users know that Bleak is not going to work in this case. Also add a utility function and troubleshooting docs to provide a workaround for this issue. Fixes: #1132
The pywin32-related packages will implicitly set the threading model to STA when imported. Since Bleak is using WinRT for async methods and we don't have a Windows event loop running, we need to let users know that Bleak is not going to work in this case. Also add a utility function and troubleshooting docs to provide a workaround for this issue. Fixes: #1132
The pywin32-related packages will implicitly set the threading model to STA when imported. Since Bleak is using WinRT for async methods and we don't have a Windows event loop running, we need to let users know that Bleak is not going to work in this case. Also add a utility function and troubleshooting docs to provide a workaround for this issue. Fixes: #1132
bluetoothctl -v
) in case of Linux:Win 11 Pro
OS: Windows-10-10.0.22621-SP0
Platform:: uname_result(system='Windows', node='DESKTOP-T0P7657', release='10', version='10.0.22621', machine='AMD64')
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]
Description
Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.
I have a a Python application that in some cases hangs when trying to establish a connection via Bleak. I reduced it to a small example that demonstrate the problem.
To reproduce:
Two points that may help diagnosing the issue:
i. Uninstalling the ipykernel pip package also makes it to work.
ii. Removing 'import pkg_resources' from the test program makes it to work. (However, I do need it in my real application)
What I Did
It is preferable if an issue contains a Miminal Workable Example.
This will otherwise be one of the first questions you will get as a response.
It is also preferable if that example is not dependent on a specific peripheral device, but can be run and
reproduced with other BLE peripherals as well.
See example above. It's a subset of my full application.
Logs
Include any relevant logs here.
Logs are essential to understand what is going on behind the scenes.
See https://bleak.readthedocs.io/en/latest/troubleshooting.html for information on how to collect debug logs.
If you receive an OS error (
WinError
,BleakDBusError
,NSError
, etc.), Wireshark logs of Bluetooth packets are required to understand the issue!The text was updated successfully, but these errors were encountered: