bleak icon indicating copy to clipboard operation
bleak copied to clipboard

Fix BleakClient leak on macOS

Open Jc2k opened this issue 3 years ago • 1 comments

Please see https://github.com/hbldh/bleak/issues/762 which contains my "working out" for this problem.

Previously the disconnect handler was only called if the peripheral wasn't already disconnected. This is a problem when devices might disconnect before you are done with them.

It could lead to a leak where the handler was never disconnected and you could end up with duplicate disconnect callbacks for the same device.

The client objects themselves also leaked.

The existing code had the effect of masking a duplicate callback. corebluetooth seems to send a notification with no error when we disconnect the disconnect handler. So users previously only saw disconnect events caused by errors (the device going away).

Now that we disconnect from the handler we get an extra notification. To avoid an API break, we now only invoke the users callback if there was an error condition. This simulates the old behaviour.

Jc2k avatar Feb 11 '22 15:02 Jc2k

Thanks for the pull request. I'm not sure yet that this is the right way to fix the problem without breaking other things. I thought I was seeing some similar issues with disconnecting on macOS in my own program last week. Unfortunately, I'm no longer able to reproduce this issue today. I also tried the example from #762 but was not able to reproduce the problem.

While looking into it, though, I found some other problems. Could you try #778 and see if it makes any difference with this problem?

dlech avatar Mar 05 '22 03:03 dlech

Closing due to no activity.

dlech avatar Mar 17 '23 21:03 dlech