-
Notifications
You must be signed in to change notification settings - Fork 306
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
New connections can't be made after some time. #255
Comments
Very good analysis of the problem. It is now proven that #133 is real and might need solving. I would be much helped by you installing the |
@LeoBortoni Could you please send me the code you run, along with the memory measurement solution that you have and I will try to make sense of this problem in some way? If you don't want to publish it here then send it via mail to me. |
bleak_approachs.zip |
@LeoBortoni Thank you for this. I will run this during this week and get back to you. I have been experimenting extensively with the These links are related:
I want to do something like this, but it is apparently impossible with the UWP/WinRT solutions. This might be solved by implementing #180. |
@LeoBortoni I have ran your code and tried to modify the However, I solved a different problem: I implemented a different .NET backend, using the In case it solves your memory problem I will merge the Run |
This is me running connect, notify and disconnect with the current This is me running connect, notify and disconnect with the There are comments about C# not collecting memory unless needed, so the graphs might not actually unavailable memory, e.g. here. That is, the In your example, try to remove everything that is not bleak (influxdb and suchlike). I cannot understand why your plot has such a spiky behaviour at the end. It does drop down at the end, so the program does release memory when reaching the end. I want to know if the crash is really due to bleak or due to other components you use in your code. I will run longer sessions during the weekend to see if I can crash my system as well. I've got 16 GB of RAM so it might take a while... |
There are multiple improvements in version 0.9.0 regarding the Windows backend. It might be so that this is solved there, if you want to try it out. |
We dropped the pythonnet backend some time ago. If this issue also exists in the winrt backend, let's start a new issue. |
Description
So this is what I am trying to do since 2 months ago: My goal is to create application which reads some number N of sensors at the same time via bluetooth, collect its data and publish it on my data bank. It is a simple thing: I created a app which load the list of address of the sensors, and split it on another sublists of simultaneos_connections_list, so then for each sensor in the group of simultaneos_connections_list, I create a thread or async task to read this sensor (Yes, i created a wrapper of the bleak lib that allows me to use threads). This thread, or async task, do this: Create a Bleak client, search and try to connect with the sensor, get its data and publish it on the data bank. After this, is time for the other group of simultaneos_connections create its thread and perform the exact same task, but for others sensor's address. Finished all the groups, the while (True) loop restarts and the applications execute the creation of threads again. Every thing works pretty well for some time (both the async and thread version). The problem occours after 3 or 4 hours running the application. After this time, the bleak lib just cant connect with any other sensor. The bleak finds the bluetooth address and try to connect with the sensor, but it aways stops on the "Get Services..." log and than raise a empty exception, which I assume to be a timeout exception. The number of simultaneos connections N that I have tested are 1,2 and 3. I have tested many differents approaches and i will list it bellow, togehter with my assumptions of why this is happening. For each thread, i try to connect with the sensor 20 times, and each time I call the connect method from the bleak client like that: client.connect(timeout = 3). After try the connection 20 times, the thread throws a exception "Could not find the address".
Note > For each measurement on my sensors, i create a thread, a bleak client on this thread, try to connect and read it, publish the result on the data bank and then I delet this client.
What is more weird, is that when this application can't connect with the sensor any more, even if i finalize the app and restart it, itself will not work anymore! It can't connect with the sensor, just like if it had been running for 4 hours and then stoped. Even if I restart the bluetooth module on the windows system, it does not connect with any sensor. I do have one test.py program which just connect with the sensor, reads its data and print on the screen, just to test the bleak library, and even this program dosen't work anymore, can't connect. The only thing that allows me to connect with my sensors again is to restart the computer. Then, i am able to run this test.py or the main application with threads and it works.
One thing to note > Despite after stops to connect with my sensors and cant connect anymore with any sensor unless i restart the computer, I CAN connect with the sensors devices using the windows bluetooth interface to connect with devices. Just the bleak module dosen't work anymore.
One thing interesting, that has led me to some direction of what is happening, is as follow: Once, I left the app with threads running and after 3 hours it stoped to connect with the sensors, ok. nothing new, but i still left it there, running, just to see if after some hours it would connect again. Then, after 2 more hours, I got a memory error from python: Process is terminated due stackoverflow exception. And the application was forced close. And the error box which appeared to me said: 'A new guard page for the stack cannot be created'. That is pretty weird since i am creating the client in a thread, and deleting the client after the try to connect or the successfully connection. The pictures of the errors are atached.
So, let start to describe what i have tryied.
What I Did
The first thing I tryied was to for each cycle of the while (True), execute a subprocess to run a powershell script to turn on and off the bluetooth windows module. Dosen't work.
Then, I tryied to execute some thing more severe. On adm mode, I tryied to execute a program to force restart of all the bluetooth services from windows, for each run of the while (True). Did not work.
The I tryied to track the memory usage of the application, for a situation when the application was left running for 3 hours and then stoped to connect. Atached is the graphic of the memory usage. I have made the calculations, that highest pick of memory use, is the exacly moment when the application can't connect anymore. That picke there is the last sample collected, after that the bleak cant connect.
Then I tryied to force the garbage collector after each while (True) with: import gc, and gc.collect(). Did not work.
I also have tracked the highest memory usage of my application, when it stops to connect with the sensors after 3 hours, atached is the track.
And this result really make sense, because when i try to just collect data from one sensor, i mean, left the whole application running just to collect data from one sensor device (one thread) and then publishind this data in the databank, the application runs for 10 hours and than stops to connect. This is interesting because perhaps some stack at the discovery.py is overloading the bluetooth driver of the OS, and it just get back to normality after restart the machine.
Now prepare your self for the gran finalle.
Ok, i thought, I could try to force the garbage collector to work in another way. I could try to create a process instead of thread with subprocess library. And that i did. some simple stuff. And then i left it running. For each measurement, i create a process, inside this process i create a bleak client, do my reads and publish it on my data bank. IT WORKS. Right now is completing 24 hours of running the application with 3 simoultaneos connections (This mean that in my main process, i create 3 threads per time, on each thread i create a process).
But this is not a good solution, since i am creating a lot of process and this is too much costly. Besides, i need to use the async version of this application, without threads. The use of threads was just to try to find a solution.
So, i think this is everything i can remember right now. Please, let me know if you guys already had tome thing like that.
The text was updated successfully, but these errors were encountered: