IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.58k stars 490 forks source link

Correctly process invalid IO #446

Closed fsaris closed 3 years ago

fsaris commented 3 years ago

After the BLE Connection is established, a L2CAP socket will be added the GLib Poll list via "g_io_add_watch()". But unfortunately the "G_IO_NVAL" is missing in the Condition Parameter of the "g_io_add_watch()".

Therefore if Disconnection happens (no matter it's triggered by the BluePy side, or another side), namely the L2CAP Socket is closed by the Kernel Bluetooth Subsystem . The missing condition "G_IO_NVAL" causes that the cleanup callback "channel_watcher" will not be called. In this case, the GIO Event Source will not be removed from the GLib Poll list properly. And the following infinite Loop happens and raises CPU Usage to 100%.

poll([{fd=0, events=POLLIN}, {fd=4, events=POLLIN}, {fd=5, events=0}], 3, -1) = 2 ([{fd=4, revents=POLLIN}, {fd=5, revents=POLLNVAL}]) // cleanup should happen, but not poll([{fd=0, events=POLLIN}, {fd=4, events=POLLIN}, {fd=5, events=0}], 3, -1) = 1 ([{fd=5, revents=POLLNVAL}]) poll([{fd=0, events=POLLIN}, {fd=4, events=POLLIN}, {fd=5, events=0}], 3, -1) = 1 ([{fd=5, revents=POLLNVAL}])

Fixes: #332

fsaris commented 3 years ago

This is the patch provided by @xsun1213 in https://github.com/IanHarvey/bluepy/issues/239#issuecomment-549021485

Also related to #332

fsaris commented 3 years ago

@IanHarvey tnx for taking care!

fsaris commented 3 years ago

@IanHarvey could you maybe release a new version so all other projects that rely on this project can easily profit from all the fixes and improvements that where added over the last 2 years.

Tnx in advanced 😃