From b38a15d25c12b630c0212cb0b346772ba006f7dc Mon Sep 17 00:00:00 2001 From: Eduardo Born Date: Thu, 21 Oct 2021 12:12:23 -0500 Subject: [PATCH] Fixing memory leak that leaves values and callbacks in memory indefinitely --- eel/__init__.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/eel/__init__.py b/eel/__init__.py index a630e1e1..3059a92f 100644 --- a/eel/__init__.py +++ b/eel/__init__.py @@ -22,6 +22,7 @@ _eel_js = open(_eel_js_file, encoding='utf-8').read() _websockets = [] _call_return_values = {} +_call_return_msgs = {} _call_return_callbacks = {} _call_number = 0 _exposed_functions = {} @@ -305,6 +306,7 @@ def _process_message(message, ws): elif message['status'] == 'error' and error_callback is not None: error_callback(message['error'], message['stack']) else: + _call_return_msgs[call_id] = message _call_return_values[call_id] = message['value'] else: @@ -353,12 +355,23 @@ def _call_return(call): def return_func(callback=None, error_callback=None): if callback is not None: - _call_return_callbacks[call_id] = (callback, error_callback) + if call_id in _call_return_msgs: + _call_return_values.pop(call_id) + message = _call_return_msgs.pop(call_id) + if message['status'] == 'ok': + callback(message['value']) + elif message['status'] == 'error' and error_callback is not None: + error_callback(message['error'], message['stack']) + else: + _call_return_callbacks[call_id] = (callback, error_callback) + else: for w in range(_js_result_timeout): if call_id in _call_return_values: + _call_return_msgs.pop(call_id, None) return _call_return_values.pop(call_id) sleep(0.001) + return return_func