kivy / oscpy

An efficient OSC implementation compatible with python2.7 and 3.5+
MIT License
109 stars 27 forks source link

CPU usage getting high after opening/closing server many times #44

Open JPfeP opened 5 years ago

JPfeP commented 5 years ago

Hi,

This issue might be related or not to issue #43.

In my new Blender add-on project, I implemented an automatic retry function for the OSC server that fires every 500ms. It's quite handy for the user.

Lately I observed Blender taking all of my CPUs and, after investigation, it happens when the server is not able to connect (due to wrong settings) for a long time and therefore is still trying to connect again and again. I can reproduce the problem, the CPU usage rises very slowly and steadily until the app becomes unresponsive. It doesn't happen when the add-on connects successfully at the beginning and keeps the connection running, that's why I only discover the problem now.

If after all the failed attempts the server can at least connects it doesn't solve the issue, the CPU usage stays high and the only solution is to close Blender.

Here is an extract of my code:

def retry_server():
    global osc_server, osc_in_ok
    bcw = bpy.context.window_manager
    ip = bcw.mom_osc_udp_in
    port = bcw.mom_osc_port_in

    if bcw.mom_osc_in_enable is True and osc_in_ok is False:
        # try closing a previous instance
        if osc_server is not None:
            osc_server.stop_all()
            osc_server = None
            time.sleep(.01)

        # try opening
        try:
            osc_server = OSCThreadServer(default_handler=OSC_callback)
            osc_server.listen(address=ip, port=port, default=True)
            bcw.mom_osc_alert = False
            osc_in_ok = True

        except:
            bcw.mom_osc_alert = True
            osc_server = None

    if bcw.mom_osc_in_enable is False and osc_in_ok is True:
        # try closing a previous instance
        if osc_server is not None:
            osc_server.stop_all()
            osc_server = None

    return .5

As you see I tried to clean the reference "osc_server", setting it to "None", but it doesn't solve the issue.

This is with python 3.7.0 under Ubuntu 18.04 LTS and a fresh "oscpy" copy from github.

tshirtman commented 5 years ago

Indeed, creating an OSCThreadServer instance starts a thread that never ends, and that will listen to all sockets the server has created through calls to listen(). The solution there would be to only create one server, and in the case the address can't be bind to (i assume that's the error you catch in your except?), instead of recreating an osc_server, just call listen again on it, with the correct parameters. calling stop() shouldn't be necessary in this case, as the socket is only created and added to the list of sockets to listen to if the bind is successful.

edit: maybe adding a __del__ method to OSCThreadServer that would stop the thread could be useful though.

Julian-O commented 9 months ago

Just a trivial note that __del__ alone would not be a good solution, because it relies on the garbage collector getting around to it.