From a92bbbbcbf030d7aed4d6e2e21fdfeee416992c5 Mon Sep 17 00:00:00 2001 From: emdee Date: Wed, 12 Oct 2022 19:51:08 +0000 Subject: [PATCH] Bugfixes --- toxygen/app.py | 7 ++++-- toxygen/history/database.py | 1 - toxygen/history/history.py | 4 ++++ toxygen/middleware/threads.py | 36 +++++++++++++++++++++++++++---- toxygen/middleware/tox_factory.py | 23 ++++++++++++++++---- 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/toxygen/app.py b/toxygen/app.py index 6d4a0f2..9ff6a76 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -488,7 +488,7 @@ class App: LOG.debug(f"_start_threads init: {te()!r}") # starting threads for tox iterate and toxav iterate - self._main_loop = threads.ToxIterateThread(self._tox) + self._main_loop = threads.ToxIterateThread(self._tox, self) self._main_loop.start() self._av_loop = threads.ToxAVIterateThread(self._tox.AV) @@ -849,7 +849,7 @@ class App: self.test_net() self._ms.log_console() - def test_net(self, lElts=None, oThread=None, iMax=4): + def test_net(self, oThread=None, iMax=4): LOG.debug("test_net " +self._oArgs.network) # bootstrap @@ -905,6 +905,9 @@ class App: LOG.trace(f"Connected status #{i}: {status!r}") self.loop(2) + global iLAST_CONN + iLAST_CONN = time.time() + def _test_env(self): _settings = self._settings if 'proxy_type' not in _settings or _settings['proxy_type'] == 0 or \ diff --git a/toxygen/history/database.py b/toxygen/history/database.py index e7709d6..f91e4cc 100644 --- a/toxygen/history/database.py +++ b/toxygen/history/database.py @@ -3,7 +3,6 @@ from sqlite3 import connect import os.path import utils.util as util -# LOG=util.log global LOG import logging LOG = logging.getLogger('app.db') diff --git a/toxygen/history/history.py b/toxygen/history/history.py index ba5680e..caaf91f 100644 --- a/toxygen/history/history.py +++ b/toxygen/history/history.py @@ -1,6 +1,10 @@ # -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*- from history.history_logs_generators import * +global LOG +import logging +LOG = logging.getLogger('app.db') + class History: def __init__(self, contact_provider, db, settings, main_screen, messages_items_factory): diff --git a/toxygen/middleware/threads.py b/toxygen/middleware/threads.py index e1f156c..c5cf964 100644 --- a/toxygen/middleware/threads.py +++ b/toxygen/middleware/threads.py @@ -6,6 +6,7 @@ from PyQt5 import QtCore from bootstrap.bootstrap import * from bootstrap.bootstrap import download_nodes_list +from wrapper.toxcore_enums_and_consts import TOX_USER_STATUS, TOX_CONNECTION import wrapper_tests.support_testing as ts from utils import util @@ -36,6 +37,9 @@ def LOG_INFO(l): print('INFO+ '+l) def LOG_DEBUG(l): print('DBUG+ '+l) def LOG_TRACE(l): pass # print('TRACE+ '+l) +iLAST_CONN = 0 +iLAST_DELTA = 60 + # ----------------------------------------------------------------------------------------------------------------- # Base threads # ----------------------------------------------------------------------------------------------------------------- @@ -117,7 +121,7 @@ class InitThread(BaseThread): threading.Timer(1.0, self._app.test_net, args=list(), - kwargs=dict(lElts=None, oThread=self, iMax=2) + kwargs=dict(oThread=self, iMax=4) ).start() if self._is_first_start: @@ -136,10 +140,11 @@ class InitThread(BaseThread): class ToxIterateThread(BaseQThread): - def __init__(self, tox): + def __init__(self, tox, app=None): super().__init__() self._tox = tox - + self._app = app + def run(self): LOG_DEBUG('ToxIterateThread run: ') while not self._stop_thread: @@ -150,8 +155,31 @@ class ToxIterateThread(BaseQThread): # Fatal Python error: Segmentation fault LOG_ERROR(f"ToxIterateThread run: {e}") else: - sleep(iMsec / 1000) + sleep(iMsec / 1000.0) + + global iLAST_CONN + if not iLAST_CONN: + iLAST_CONN = time.time() + # TRAC> TCP_common.c#203:read_TCP_packet recv buffer has 0 bytes, but requested 10 bytes + # and segv + if \ + time.time() - iLAST_CONN > iLAST_DELTA and \ + ts.bAreWeConnected() and \ + self._tox.self_get_status() == TOX_USER_STATUS['NONE'] and \ + self._tox.self_get_connection_status() == TOX_CONNECTION['NONE']: + iLAST_CONN = time.time() + LOG_INFO(f"ToxIterateThread calling test_net") + if True: + invoke_in_main_thread( + self._app.test_net, oThread=self, iMax=2) + else: + threading.Timer(1.0, + self._app.test_net, + args=list(), + kwargs=dict(lElts=None, oThread=self, iMax=2) + ).start() + class ToxAVIterateThread(BaseQThread): def __init__(self, toxav): diff --git a/toxygen/middleware/tox_factory.py b/toxygen/middleware/tox_factory.py index 60535c1..1b4d119 100644 --- a/toxygen/middleware/tox_factory.py +++ b/toxygen/middleware/tox_factory.py @@ -17,6 +17,20 @@ from ctypes import * from utils import util from utils import ui as util_ui +# callbacks can be called in any thread so were being careful +# tox.py can be called by callbacks +def LOG_ERROR(a): print('EROR> '+a) +def LOG_WARN(a): print('WARN> '+a) +def LOG_INFO(a): + bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel <= 20 + if bVERBOSE: print('INFO> '+a) +def LOG_DEBUG(a): + bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel <= 10-1 + if bVERBOSE: print('DBUG> '+a) +def LOG_TRACE(a): + bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel < 10 + if bVERBOSE: print('TRAC> '+a) + def tox_log_cb(iTox, level, file, line, func, message, *args): """ * @param level The severity of the log message. @@ -42,7 +56,7 @@ def tox_factory(data=None, settings=None, args=None, app=None): :return: new tox instance """ if not settings: - LOG.warn("tox_factory using get_default_settings") + LOG_WARN("tox_factory using get_default_settings") settings = user_data.settings.Settings.get_default_settings() else: user_data.settings.clean_settings(settings) @@ -88,13 +102,14 @@ def tox_factory(data=None, settings=None, args=None, app=None): tox_options._options_pointer, tox_options.self_logger_cb) else: - logging.warn("No tox_options._options_pointer to add self_logger_cb" ) + logging_WARN("No tox_options._options_pointer to add self_logger_cb" ) retval = wrapper.tox.Tox(tox_options) except Exception as e: if app and hasattr(app, '_log'): - app._log(f"ERROR: wrapper.tox.Tox failed: {e}") - LOG.warn(traceback.format_exc()) + pass + LOG_ERROR(f"wrapper.tox.Tox failed: {e}") + LOG_WARN(traceback.format_exc()) raise if app and hasattr(app, '_log'):