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

client.disconnect causes timeout even with example code #807

Closed
savejeff opened this issue Apr 16, 2022 · 2 comments · Fixed by #974
Closed

client.disconnect causes timeout even with example code #807

savejeff opened this issue Apr 16, 2022 · 2 comments · Fixed by #974
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend bug Something isn't working

Comments

@savejeff
Copy link

savejeff commented Apr 16, 2022

  • bleak version: 0.14.2
  • Python version: 3.8
  • Operating System: Windows 10

Description

I'm unable to close a connection to a client without a timeout.

What I Did

I Connect to a ESP32 with a BLE Server running the "standard" UART Characteristic. Even just connecting and then trying to disconnect causes this behavior.

I tried several methods, all end in a timeout error.

I've used the example code from this documentation: https://buildmedia.readthedocs.org/media/pdf/bleak/stable/bleak.pdf Page 10

address = "94:B9:7E:FE:76:8E"

async def main(address):
	client = BleakClient(address)
	try:
		await client.connect()

		await client.write_gatt_char(CHARACTERISTIC_UUID_UART_RX, bytes("Hallo, World!", 'ascii'))
		print(f"write done")

	except Exception as e:
		print(e)
	finally:
		await client.disconnect()

asyncio.run(main(address))


causes this


Traceback (most recent call last):
  File "XXX/BLEHelp.py", line 475, in <module>
    asyncio.run(main(address))
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "XXX/BLEHelp.py", line 473, in main
    await client.disconnect()
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python38\lib\site-packages\bleak\backends\winrt\client.py", line 313, in disconnect
    await asyncio.wait_for(event.wait(), timeout=10)
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py", line 490, in wait_for
    raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError

Update

it seems to work more or less reliably when connecting after being connected for longer periods of time.
it never works if I connect and then disconnect only a few seconds later

@dlech dlech added the Backend: WinRT Issues or PRs relating to the WinRT backend label Apr 16, 2022
@dlech
Copy link
Collaborator

dlech commented Apr 16, 2022

I've noticed this recently too. I think we just need to extend the default timeout value to 30 seconds or so.

For background, Windows doesn't actually let us tell the device to disconnect. We can only say that we aren't using it any more and it is up to the OS to decide when to actually disconnect.

@goldwake
Copy link

for what it's worth, I am often seeing the same issue with disconnection too. Sometimes it works, sometimes it doesn't

bleak version: 0.14.2
Python version: 3.9
Operating System: Windows 10

dlech added a commit that referenced this issue Sep 2, 2022
This changes the disconnect timeout on the WinRT backend to 120s.

Sometimes it can take up to a minute for Windows to actually close
the GATT session/disconnect the device. This behavior is also seen
in other apps, like WebBluetooth in Chrome. Furthermore, the device
remains connected even after the app is closed, so clearly this is
an issue with the Windows Bluetooth stack and not something that
can be fixed in userspace code.

Fixes #807.
@dlech dlech added the bug Something isn't working label Sep 2, 2022
@dlech dlech closed this as completed in #974 Sep 5, 2022
@dlech dlech mentioned this issue Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants