Skip to content
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

windows 11 Run Time out #1262

Open
yonghee1982 opened this issue Mar 20, 2023 · 25 comments
Open

windows 11 Run Time out #1262

yonghee1982 opened this issue Mar 20, 2023 · 25 comments
Labels
3rd party issue The issue is with the Bluetooth stack, the BLE device, or other 3rd party code not with Bleak itself Backend: WinRT Issues or PRs relating to the WinRT backend more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback

Comments

@yonghee1982
Copy link

  • bleak version: 0.19.5
  • Python version: 3.11
  • Operating System: windows 10 / 11
  • BlueZ version (bluetoothctl -v) in case of Linux:

Description

Please understand that I am writing using a translation tool.

I represented the searched Bluetooth devices in a list box and attempted to connect to a device by selecting it.
However, although the connection is completed within seconds on Windows 10, on Windows 11 the program stops or a Time Out Error message occurs.

What I Did

This code is a part of the whole code.

async def connect_device_async(self, device):
    try:
        self.client = BleakClient(device.address, timeout=60)
        await self.client.connect()
        messagebox.showinfo("Success", f"Connected to {device.address} - {device.name}")
        self.result_text.insert("1.0", f"Connected to {device.address} - {device.name}\n")
    except BleakError as e:
        messagebox.showerror("Error", str(e))

def connect_device(self):
    if len(self.devices_listbox.curselection()) == 0:
        messagebox.showerror("Error", "No device selected.")
        return
    index = self.devices_listbox.curselection()[0]
    device = self.devices[index]
    asyncio.run(self.connect_device_async(device))

Logs

Exception in Tkinter callback
Traceback (most recent call last):
  File "bleak\backends\winrt\client.py", line 402, in connect
  File "asyncio\tasks.py", line 418, in wait
  File "asyncio\tasks.py", line 525, in wait
asyncio.exceptions.CancelledError During handling of the above exception, another exception occurred: Traceback (most recent call last):
  File "tkinter_init
.py", line 1948, in call
  File "InBodyBT500ModeChange.py", line 115, in connect_device
  File "asyncio\runners.py", line 190, in run
  File "asyncio\runners.py", line 118, in run
  File "asyncio\base_events.py", line 653, in run_until_complete
  File "InBodyBT500ModeChange.py", line 103, in connect_device_async
  File "bleak_init_.py", line 471, in connect
  File "bleak\backends\winrt\client.py", line 389, in connect
  File "async_timeout_init_.py", line 129, in aexit
  File "async_timeout_init_.py", line 212, in _do_exit
TimeoutError

@dlech
Copy link
Collaborator

dlech commented Mar 21, 2023

Is this still a problem with Bleak v0.20.0?

@dlech dlech added the Backend: WinRT Issues or PRs relating to the WinRT backend label Mar 21, 2023
@yonghee1982
Copy link
Author

yonghee1982 commented Mar 21, 2023

It was version 0.19.5, and I tried to proceed after upgrading to 0.20.0, but the problem was not resolved.

On Windows 10, both versions 0.19.5 and 0.20.0 work properly.

image

@dlech
Copy link
Collaborator

dlech commented Mar 21, 2023

What are the exact differences between the working case and the nit working case? Exact Windows version? Bluetooth radio (in PowerShell, run wget https://github.com/Microsoft/busiotools/raw/master/bluetooth/tracing/GetBluetoothRadioInfo.ps1 -UseBasicParsing | iex)? Is it the same computer or a different computer?

@yonghee1982
Copy link
Author

I'm sorry for the late reply.

In Windows 10, device search and connection are possible, but in Windows 11, device search is possible, but connection is not.

Each computer is different, and here are the OS version and Bluetooth Radio information for each:

Windows 10

Version: 21H2 (OS Build 19044.2130)
Bluetooth Radio
InstanceId: USB\VID_8087&PID_0026\5&3B1E8281&0&10
MAC: F4B30105AD7E
DriverDescription: Intel(R) Wireless Bluetooth(R)
DriverVersion: 22.50.1.1
ErrorRecovery: PLDR
ScoType: Unknown
Windows 11

Version: 22H2 (OS Build 22621.1413)
Bluetooth Radio
InstanceId: USB\VID_0BDA&PID_B00C\00E04C000001
MAC: 18473D6E945C
DriverDescription: Realtek Bluetooth 5 Adapter
DriverVersion: 1.8.1037.3000
ErrorRecovery: None
ScoType: InBand

@dlech
Copy link
Collaborator

dlech commented Mar 24, 2023

On Windows 11 do other BLE tools like https://apps.microsoft.com/store/detail/bluetooth-le-explorer/9N0ZTKF1QD98?hl=en-us&gl=us or chrome://bluetooth-internals in a Chrome or Edge browser work? If they do, what is the difference in Bluetooth packets captured with them compared to Bleak (https://bleak.readthedocs.io/en/latest/troubleshooting.html#windows-10).

@artodd1
Copy link

artodd1 commented Mar 31, 2023

I am having a similar issue. Connection to my BLE device worked fine with my app on a computer running Windows 10 and running the same app on a computer running Windows 11 it is getting hung up while awaiting BleakClient.connect(), no timeout exception is occuring.

bleak version: 0.20.1
Python version: 3.10.10

OS: Windows 11 Pro
Version: 10.0.2.2261

Bluetooth Radio Info:
InstanceId : USB\VID_10AB&PID_9309\6&1CAA2945&0&3
DriverDescription : Qualcomm WCN685x Bluetooth Adapter
DriverVersion : 1.0.0.1290
ErrorRecovery : None
ScoType : InBand

I have tried chrome://bluetooth-internals on the computer running Windows 11 and I am able to connect to my BLE device.
I also tried connecting to my device with Bluetooth LE Explorer, I was unable to connect to my device using this BLE tool.

chrome_ble_internals_connect_disconnect_ble.pcapng.gz
my_app_ble_connect_attempt.pcapng.gz
bluetooth_le_explorer_connect_attempt.pcapng.gz

@dlech
Copy link
Collaborator

dlech commented Apr 2, 2023

Thanks for the logs. Can you also share the output of your Python script with debug logging enabled?

You could also try fiddling with the winrt use_cached_services option.

@dlech dlech added the more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback label Jul 19, 2023
@wpilgri
Copy link

wpilgri commented Nov 12, 2023

bleak_ConnectDebug.pcapng.gz
Wanted to add some more information via another example of Windows 11 having problems connecting.

  • Latest build of Windows 11
  • Bleak v0.21.1
  • python v3.11.5
  • device I'm connecting to: STM WB55RG Nucleo

It's a very simple program that I have working on both macOS and Windows 10 which properly scans and finds the device Iwant to connect to (MyST), but BleakClient() call fails to connect with no errors. Here's the snippet of the main program and the output I see with BLEAK_LOGGING=1

async def runmain():

   #Works fine
    await scanfordevices() 

    if found:
        print(f'*** runmain: Trying to connect to: {DEVICE_NAME}')
         
       #  ******* THIS IS THE CALL WHICH NEVER RETURNS AND NEVER PRODUCES AS ERROR  ********
        async with BleakClient(ardAddress) as client:

            await getservices(client)

            print('*** runmain: Assign notification callbacks')
            await client.start_notify(Sensor1DataUUID, notification_sensordata_handler) #1st peak
            await client.start_notify(Sensor2DataUUID, notification_sensordata_handler) #2nd peak
            await client.start_notify(Sensor3DataUUID, notification_sensordata_handler) #3rd peak

            while not exit_flag:
                await asyncio.sleep(1)

print('*** runmain: Done.')

When I run the above I get this output in the console:

2023-11-12 15:25:30,936 bleak.backends.winrt.scanner MainThread DEBUG: 31 devices found. Watcher status: <BluetoothLEAdvertisementWatcherStatus.STOPPED: 3>.
00:80:E1:26:C4:FE: MyST
d.name='MyST'
d.address='00:80:E1:26:C4:FE'
adv_data.rssi=-58
*** runmain: Scan is done, checking if Microcontroller with name= MyST found.
*** runmain: Trying to connect to: MyST
2023-11-12 15:25:30,986 bleak.backends.winrt.scanner MainThread DEBUG: Received EF:F7:57:DD:EA:16: .
2023-11-12 15:25:31,011 bleak.backends.winrt.scanner MainThread DEBUG: Received 76:17:EF:19:80:69: .
2023-11-12 15:25:31,011 bleak.backends.winrt.scanner MainThread DEBUG: Received 76:17:EF:19:80:69: .
2023-11-12 15:25:31,100 bleak.backends.winrt.scanner MainThread DEBUG: Received 6C:4A:85:1F:42:E0: .
2023-11-12 15:25:31,100 bleak.backends.winrt.scanner MainThread DEBUG: Received 6C:4A:85:1F:42:E0: .
2023-11-12 15:25:31,107 bleak.backends.winrt.scanner MainThread DEBUG: Received CD:E9:FF:E6:E0:82: .
2023-11-12 15:25:31,118 bleak.backends.winrt.scanner MainThread DEBUG: Received C0:28:8D:FF:B5:F0: .
2023-11-12 15:25:31,124 bleak.backends.winrt.scanner MainThread DEBUG: Received C0:28:8D:FF:B5:F0: .
2023-11-12 15:25:31,209 bleak.backends.winrt.scanner MainThread DEBUG: Received C0:28:8D:FF:B5:F0: .
2023-11-12 15:25:31,213 bleak.backends.winrt.scanner MainThread DEBUG: Received C0:28:8D:FF:B5:F0: .
2023-11-12 15:25:31,217 bleak.backends.winrt.scanner MainThread DEBUG: Received 2C:41:A1:D8:D4:E3: .
2023-11-12 15:25:31,217 bleak.backends.winrt.scanner MainThread DEBUG: Received 2C:41:A1:D8:D4:E3: LE-reserved_C.
2023-11-12 15:25:31,234 bleak.backends.winrt.scanner MainThread DEBUG: Received 00:80:E1:26:C4:FE: MyST.
2023-11-12 15:25:31,234 bleak.backends.winrt.scanner MainThread DEBUG: Received 00:80:E1:26:C4:FE: .
2023-11-12 15:25:31,247 bleak.backends.winrt.scanner MainThread DEBUG: 7 devices found. Watcher status: <BluetoothLEAdvertisementWatcherStatus.STOPPED: 3>.
2023-11-12 15:25:31,247 bleak.backends.winrt.client MainThread DEBUG: Connecting to BLE device @ 00:80:E1:26:C4:FE
2023-11-12 15:25:31,304 bleak.backends.winrt.client MainThread DEBUG: getting services (service_cache_mode=None, cache_mode=None)...
2023-11-12 15:25:31,328 bleak.backends.winrt.client Dummy-5 DEBUG: session_status_changed_event_handler: id: BluetoothLE#BluetoothLEf4:b3:01:ba:7f:ab-00:80:e1:26:c4:fe, error: <BluetoothError.SUCCESS: 0>, status: <GattSessionStatus.ACTIVE: 1>
2023-11-12 15:25:31,393 bleak.backends.winrt.client Dummy-5 DEBUG: max_pdu_size_changed_handler: 156
2023-11-12 15:25:31,657 bleak.backends.winrt.client Dummy-6 DEBUG: 00:80:E1:26:C4:FE: services changed
2023-11-12 15:25:32,771 bleak.backends.winrt.client Dummy-5 DEBUG: 00:80:E1:26:C4:FE: services changed

I'm stumped as it works flawlessly on Windows 10, macOS, and it used to work on the same Windows 11 machine.

Any troubleshooting tips would be greatly appreciated. Wireshark capture file attached.

bleak_ConnectDebug.pcapng.gz

@wpilgri
Copy link

wpilgri commented Nov 13, 2023

Final comment to add is, as others have mentioned here, uninstalling ipykernel in my python environment solved the problem.

@dlech
Copy link
Collaborator

dlech commented Nov 13, 2023

Interesting. Was the ipykernel module loaded? If I am remembering correctly, it changes (or at least used to) the default run loop type on Windows. So that could something to look into. I.e. print the default run loop type in the runmain() function.

@wpilgri
Copy link

wpilgri commented Nov 13, 2023

It was not being used/loaded. Not sure what you mean by printing loop type, sorry.

@dlech
Copy link
Collaborator

dlech commented Nov 15, 2023

Not sure what you mean by printing loop type, sorry.

loop = asyncio.get_running_loop()
print(type(loop))

It was not being used/loaded.

I wonder if this is something similar to #1132, i.e. pywin32 is a dependency of ipykernel and some other package is importing pkg_resources.

@wpilgri
Copy link

wpilgri commented Nov 17, 2023

Sorry for the slow reply, but type(loop) returns: class 'asyncio.windows_events.ProactorEventLoop'> for both the working and non-working cases.

@isaac-aavaa
Copy link

Final comment to add is, as others have mentioned here, uninstalling ipykernel in my python environment solved the problem.

Hello,
I am having this exact issue, code works perfectly on windows 10 but hangs during the connection on windows 11.

My specialty is embedded firmware; could I trouble you for how to uninstall ipykernel? Do I just do jupyter kernelspec remove python3?

Thanks!

@markmadsen82
Copy link

Same issue. Did anyone solve this matter in windows 11?

@Quaditz
Copy link

Quaditz commented Apr 5, 2024

Hello,

I also have the same issue. I can find the device on my windows 10 and windows 11 computer when I do a windows bluetooth device search.

When I do a discover with BleakScanner, I can find the device in Windows 10 but not in Windows 11 :(

Possible hint to find the error:

On Windows 10, I can find the device with the windows ble search without an issue.

On Windows 11, I have to switch the "Bluetooth Device Detection" Option from "Standard" to "Advanced" (Translated from German).

Then I can also find the device in windows 11 with the windows search. But I am still not able to detect the device via BleakScanner.

Maybe the BleakScanner needs somehow also this "Advanced" setting in Windows 11?

@dlech
Copy link
Collaborator

dlech commented Apr 5, 2024

Interesting findings. I'm guessing this "Bluetooth Device Detection" Option is in the Windows settings?

Is the only difference Windows 10 and Windows 11 (e.g. identical computers with the same Bluetooth adapter)

It would also be helpful to get Bluetooth packet captures of working and not working cases to compare them so we can see what the actual difference is.

@Quaditz
Copy link

Quaditz commented Apr 5, 2024

Regarding the settings: I found an English instruction how to find it. This is was I did in Windows 11 to be able to find my device. In Windows 10 I was able to find it by default.

https://answers.microsoft.com/en-us/windows/forum/all/bluetooth-cant-find-any-device-when-i-update-to/ca06c184-7be5-47c9-91ce-0d93bc6e3048

How could I make such a Bluetooth Capture? Are there any free programs for windows to do that?

Edit 1: Trying my best with Wireshark now :)

Edit 2: When I use Wireshark and select "Bluetooth", I can find a Bluetooth Connection and can start the record. But somehow no data appears, even if the Windows integrated BLE device discovery does actually find the device. Any ideas how to easily record the bluetooth data that is coming in and out?

Edit 3: Using the following instruction I am able to see the packages. I will try to create nice logs with hopefully not too much unncessary data.

Instruction: https://stackoverflow.com/questions/22085989/how-to-capture-and-analyze-bluetooth-packets-using-wireshark

Edit 4: Somehow, at least the device discovery itself works again in Windows 11 using the BleakScanner. However, the connection to the client still does not work.

So the "lowest amount of code" where the code is hanging is the following:

async with BleakClient(ADRESS) as client:
            pass

With Adress being the MAC adress of my device. This line of code works perfectly in Windows 10 but not in Windows 11.

I will now record the Wireshark logs for both calls, once in Windows 11 and once in Windows 10.

Edit 5: I recorded now two traces and saved them in the CSV format. In the Win 10 variant I also closed the program with CTRL + C afterwards to close the connection, in the Win 11 variant I just waited 10 seconds during the freeze and then stopped the measurement.

Hint: The device "Raspberry Pi" is the device I want to connect to. The name of the device is "WarnTag"

Windows_10_Bleak_connects.csv
Windows_11_Bleak_freezes.csv

@dlech
Copy link
Collaborator

dlech commented Apr 5, 2024

How could I make such a Bluetooth Capture?

https://bleak.readthedocs.io/en/latest/troubleshooting.html#windows-10

I recorded now two traces and saved them in the CSV format.

It would be more helpful to have the wireshark file format. You can put these in a .zip file to attach them to the issue.

@dlech
Copy link
Collaborator

dlech commented Apr 5, 2024

If the Windows 11 log is complete, it looks like a Windows bug or driver bug where it just stops querying the peripheral device. The last thing in the log is a response from the peripheral device so Windows shouldn't be waiting for the device to respond.

@Quaditz
Copy link

Quaditz commented Apr 6, 2024

Hello,

I also did another Windows 11 measurement where I pressed CTRL+C during the freeze.

So I upload 3 measurements in the ZIP file in the Wireshark format:

  1. Windows 10: Python does NOT freeze and I can continue as I want. There is no further data in the measurement but the Python Interpreter does not freeze, I can print stuff and I can even start sending data etc.
    In this measurement I pressed CTRL+C at the time when the connection was long finished. Printing etc worked before pressing CTRL + C.

  2. Windows 11. Python freezes. No printing etc happens even when I just call it the first thing after the "async .. with:" command. In one measurement I stop the measurement during the freeze without pressing CTRL +C

  3. Same as 2, but I pressed CTRL + C during the freeze. Then I stopped the measurement. Maybe that is better compareable to the Windows 10 log.

Wireshark_Measurements.zip

@dlech
Copy link
Collaborator

dlech commented Apr 6, 2024

Thanks for the logs. I can see in the WIndows 11 logs, there is an LE Connection Update request that is never answered. So this is likely the cause of the hang.

image

Since you are using a Rapsberry Pi, you should be able to fix the program running there to respond to this.

@dlech dlech added the 3rd party issue The issue is with the Bluetooth stack, the BLE device, or other 3rd party code not with Bleak itself label Apr 6, 2024
@Quaditz
Copy link

Quaditz commented Apr 7, 2024

Thanks for the answer!

I will check how to answer that with aioble on the Raspberry PI Pico.

This might mean that Windows 10 is not sending this Update Request but the same Bleak Version in Windows 11 is then requesting this update request?

In any case, I think Bleak should return some kind of timeout then when this is not answered. Maybe this would be an improvement.

Especially because all the examples for BLE connections for the Aioble Library on the Raspberry PI never mention any need for answering connection updates.

This might be a more common use-case then, if this could correspond to an error or warning etc in Bleak this would be aweseome :)

Edit 1: Or would it even possible to tell the BleakClient: Please don't ask for parameters update. Somehow it doesn't do it anyway in Windows 10.

I think it's not even possible to answer that request that with the aioble library on MicroPython side and answering that request would require to dig deep into the raw bluetooth library.

Edit 2: The connection on Windows 11 also stays frozen when I turn off the peripheral device. At least then Bleak should feedback that something is odd. Somehow Bleak really freezes up and does not even check if the device is reachable anymore.

@dlech
Copy link
Collaborator

dlech commented Apr 7, 2024

This might mean that Windows 10 is not sending this Update Request but the same Bleak Version in Windows 11 is then requesting this update request?

It has nothing to do with Bleak, just Windows OS updates.

In any case, I think Bleak should return some kind of timeout then when this is not answered.

Bleak already has a timeout. I think the problem is that the Windows API calls are not properly handling cancellation after the timeout. So apparently we need a timeout for the timeout. 😢

Or would it even possible to tell the BleakClient: Please don't ask for parameters update.

There is a new Windows 11 API RequestPreferredConnectionParameters but we aren't calling this. So I'm not sure there is a way to tell the Windows Bluetooth stack to not perform this operation implicitly.

So I'm afraid it is up to Microsoft to fix the issue in their code.

I think it's not even possible to answer that request that with the aioble library on MicroPython side and answering that request would require to dig deep into the raw bluetooth library.

This is sounds like a better alternative than waiting for a fix from Microsoft that may never come.

This might be a more common use-case then, if this could correspond to an error or warning etc in Bleak this would be aweseome :)

This might be a more common use-case then, if this could correspond to an error or warning etc in Bleak this would be aweseome :)

Since a timeout or not returning Windows API could be caused by anything, not just this specific problem we have identified, I think the best we can do is add something to the troubleshooting page in the docs.

@Quaditz
Copy link

Quaditz commented Apr 8, 2024

Thank you @dlech for the insights!

As you saw I created also an issue on the micro python side since there is no way in Aioble to answer that Connection Update request (as you said it should be done anyway automatically by the Low Level Bluetooth Libraries).

You mentioned that Microphyton-lib is the wrong place for that issue. I will move that issue to the Micropython repo then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party issue The issue is with the Bluetooth stack, the BLE device, or other 3rd party code not with Bleak itself Backend: WinRT Issues or PRs relating to the WinRT backend more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback
Projects
None yet
Development

No branches or pull requests

7 participants