Improve readability and exception comments

Signed-off-by: Loren Eteval <loren.eteval@proton.me>
This commit is contained in:
Loren Eteval
2026-09-09 12:18:34 +08:00
parent 61d18651a2
commit d8a38c93bf
13 changed files with 67 additions and 1 deletions
+1 -1
View File
@@ -173,7 +173,7 @@ class XrayAssetListView(Mixins.ThemeAware, AppQListView):
# Same file imported. Do nothing
pass
except Exception as ex:
# Any non-exit exception
# Any non-exit exceptions
_mbox = AppQMessageBox(icon=AppQMessageBox.Icon.Critical)
_mbox.setText(_('Error import asset file'))
+2
View File
@@ -76,6 +76,8 @@ class MsgQueue(multiprocessing.queues.Queue):
try:
return self.get_nowait()
except Exception:
# Any non-exit exceptions
return ''
def getTimeout(self) -> int:
+2
View File
@@ -129,6 +129,8 @@ class _Win32Session:
try:
thread.start()
except Exception:
# Any non-exit exceptions
if self._daemonThread is thread:
self._daemonThread = None
+2
View File
@@ -58,6 +58,8 @@ def _currentTheme():
try:
return AppStyleSheet.normalizeTheme(themeGetter())
except Exception:
# Any non-exit exceptions
# A partially initialized application falls back to the safe default.
pass
+3
View File
@@ -56,7 +56,10 @@ class UserRoutings(Mixins.CleanupOnExit, StorageBackend):
raise TypeError('routing repository root must be an object')
except Exception:
# Any non-exit exceptions
self._restoreFailed = True
logger.exception('failed to restore persisted routings')
return {}
+3
View File
@@ -136,7 +136,10 @@ class UserServers(Mixins.CleanupOnExit, StorageBackend):
raise TypeError('server repository root must contain a model list')
except Exception:
# Any non-exit exceptions
self._restoreFailed = True
logger.exception('failed to restore persisted server configurations')
return {'model': []}
+3
View File
@@ -187,7 +187,10 @@ class UserSubs(Mixins.CleanupOnExit, StorageBackend):
raise TypeError('subscription repository root must be an object')
except Exception:
# Any non-exit exceptions
self._restoreFailed = True
logger.exception('failed to restore persisted subscriptions')
return {}
+2
View File
@@ -56,6 +56,8 @@ class UserTUNSettings(Mixins.CleanupOnExit, StorageBackend):
raise TypeError('TUN settings repository root must be an object')
except Exception:
# Any non-exit exceptions
self._restoreFailed = True
logger.exception('failed to restore persisted TUN settings')
+4
View File
@@ -265,11 +265,13 @@ class EndpointInfoService(QtCore.QObject):
self.controller = controller or AppConnectionController()
self.httpClient = httpClient or ProxyEndpointHttpClient(self)
self.proxyResolver = proxyResolver or Storage.Extras.UserHttpProxy
self._enabled = (
AppSettings.isStateON_(PROXY_ENDPOINT_INFO_SETTING)
if enabled is None
else bool(enabled)
)
self.state = EndpointInfoState.Disabled
self.result = EndpointInfo()
self._generation = 0
@@ -578,6 +580,8 @@ class EndpointInfoService(QtCore.QObject):
organization=str(payload.get('org') or '').strip(),
)
except Exception as ex:
# Any non-exit exceptions
logger.warning(f'approximate endpoint geolocation failed: {ex}')
self._publishResult(
+6
View File
@@ -76,6 +76,8 @@ class PluginNavigationManager:
try:
descriptors = tuple(provider.pageDescriptors())
except Exception as ex:
# Any non-exit exceptions
logger.error(
f'plugin navigation provider '
f'{provider.capabilityId!r} failed: {ex}'
@@ -102,6 +104,8 @@ class PluginNavigationManager:
try:
page = descriptor.factory(parent=navigationView)
except Exception as ex:
# Any non-exit exceptions
logger.error(f'failed to create plugin page {pageId!r}: {ex}')
continue
@@ -123,6 +127,8 @@ class PluginNavigationManager:
translatable=descriptor.translatable,
)
except Exception as ex:
# Any non-exit exceptions
logger.error(f'failed to register plugin page {pageId!r}: {ex}')
page.deleteLater()
+8
View File
@@ -259,6 +259,8 @@ class _PingWorker(QtCore.QRunnable):
interval=1,
)
except Exception as ex:
# Any non-exit exceptions
latency = classname(ex)
else:
if response.address and response.is_alive:
@@ -393,6 +395,8 @@ class _LatencyScheduler(QtCore.QObject):
try:
endpointKey, address, port = self.tcpingEndpoint(job.target.snapshot)
except Exception as ex:
# Any non-exit exceptions
self.completeJob(job, classname(ex))
continue
@@ -693,6 +697,7 @@ class _DownloadSpeedWorker(HttpGetManager):
self.profile = profile
self.port = port
self.options = options
self.result = ProfileTestResult(
ProfileTestField.DownloadSpeed,
'',
@@ -701,6 +706,7 @@ class _DownloadSpeedWorker(HttpGetManager):
self.hasSpeedResult = False
self.totalBytesRead = 0
self.hasDataCounter = 0
self.cancelled = False
self._startInProgress = False
self._completionInProgress = False
@@ -1029,6 +1035,8 @@ class _DownloadSpeedWorker(HttpGetManager):
try:
value = networkReply.error().name
except Exception:
# Any non-exit exceptions
value = 'UnknownError'
if isinstance(value, bytes):
+4
View File
@@ -1094,6 +1094,8 @@ class SettingsPage(Mixins.QTranslatable, QMainWindow):
for section in sections:
self._addPluginDescriptorSection(section)
except Exception as ex:
# Any non-exit exceptions
logger.error(
f'failed to create settings from plugin '
f'{metadata.id!r}: {ex}'
@@ -1103,6 +1105,8 @@ class SettingsPage(Mixins.QTranslatable, QMainWindow):
try:
self._addPluginActionSection(plugin, metadata, registry)
except Exception as ex:
# Any non-exit exceptions
logger.error(
f'failed to create management settings from plugin '
f'{metadata.id!r}: {ex}'
+27
View File
@@ -396,6 +396,7 @@ class GenerationLogManagerContractTest(unittest.TestCase):
def testSeededModelBasedStateMachine(self):
"""Compare arbitrary public transitions to a flat reference model."""
seeds = (0, 1, 7, 19, 41, 97, 313, 997)
for seed in seeds:
with self.subTest(seed=seed):
randomizer = random.Random(seed)
@@ -407,6 +408,7 @@ class GenerationLogManagerContractTest(unittest.TestCase):
for operationIndex in range(350):
operation = randomizer.randrange(100)
try:
if operation < 66:
categoryId = randomizer.choice(categories)
@@ -414,6 +416,7 @@ class GenerationLogManagerContractTest(unittest.TestCase):
('', 'x', 'line\n', '😀é', '\0', 'z' * 93)
)
history.append(('append', categoryId, len(message)))
self.appendBoth(manager, model, message, categoryId)
elif operation < 73:
categoryId = randomizer.choice(categories)
@@ -1028,30 +1031,41 @@ class GenerationLogManagerContractTest(unittest.TestCase):
autoClearEnabled=False,
)
manager.RetiredCleanupBudget = 64
for index in range(size):
manager.append(f'entry {index}', CORE_LOG_CATEGORY)
historical = manager.entries(CORE_LOG_CATEGORY)
self.assertEqual(len(historical), size)
references = tuple(weakref.ref(entry) for entry in historical)
externallyOwned = historical[-1]
manager.clear(runtimeOnly=True)
self.assertTrue(
all(reference() is not None for reference in references)
)
while manager.retiredEntryCount:
with manager._lock:
manager._cleanupRetiredLocked()
self.assertTrue(
all(reference() is not None for reference in references)
)
del historical
gc.collect()
self.assertTrue(
all(reference() is None for reference in references[:-1])
)
self.assertIs(references[-1](), externallyOwned)
del externallyOwned
gc.collect()
self.assertIsNone(references[-1]())
def testRetentionThreeWayMergeAndLargeSequences(self):
@@ -1195,9 +1209,11 @@ class GenerationLogManagerContractTest(unittest.TestCase):
def observedGeneration(category):
generation = original(category)
if threading.get_ident() in appendThreadId and not selected.is_set():
selected.set()
self.assertTrue(release.wait(5))
return generation
manager._generationForCategoryLocked = observedGeneration
@@ -1205,6 +1221,7 @@ class GenerationLogManagerContractTest(unittest.TestCase):
def append():
appendThreadId.append(threading.get_ident())
try:
manager.append('racing', CORE_LOG_CATEGORY)
except Exception as error:
@@ -1212,14 +1229,18 @@ class GenerationLogManagerContractTest(unittest.TestCase):
producer = threading.Thread(target=append)
clearer = threading.Thread(target=lambda: manager.clear(runtimeOnly=True))
producer.start()
self.assertTrue(selected.wait(5))
clearer.start()
time.sleep(0.01)
self.assertTrue(clearer.is_alive())
release.set()
producer.join(5)
clearer.join(5)
self.assertFalse(producer.is_alive())
self.assertFalse(clearer.is_alive())
self.assertEqual(errors, [])
@@ -1579,11 +1600,13 @@ class VeryHeavyGenerationLogManagerTest(unittest.TestCase):
autoClearMaximumEntries=100,
)
manager.RetiredCleanupBudget = 64
ordinary = []
rollover = []
retention = []
maximumRetired = 0
maximumBatches = 0
for index in range(50_000):
categoryId = CORE_LOG_CATEGORY if index % 3 else APPLICATION_LOG_CATEGORY
triggersRollover = (
@@ -1597,15 +1620,18 @@ class VeryHeavyGenerationLogManagerTest(unittest.TestCase):
manager.entryCount() + 1 > manager.maximumEntries
or manager.retainedCharacters + len(message) > manager.maximumCharacters
)
started = time.perf_counter_ns()
manager.append(message, categoryId)
elapsed = time.perf_counter_ns() - started
if triggersRollover:
rollover.append(elapsed)
elif atRetention or causesRetention:
retention.append(elapsed)
else:
ordinary.append(elapsed)
maximumRetired = max(maximumRetired, manager.retiredEntryCount)
maximumBatches = max(maximumBatches, len(manager._retiredBatches))
@@ -1623,6 +1649,7 @@ class VeryHeavyGenerationLogManagerTest(unittest.TestCase):
self.assertTrue(rollover)
self.assertLessEqual(maximumRetired, manager.maximumEntries)
_assertManagerInvariants(self, manager)
self.report(
'append-latency',
ordinary=distribution(ordinary),