aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/internet/base.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-08-25 12:54:32 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-08-25 13:03:33 +0300
commit4a64a813e1d34e732f35d8a65147974f76395a6f (patch)
treea8da0dede5213f85e45b95047cfbdcf5427cf0b7 /contrib/python/Twisted/py3/twisted/internet/base.py
parente9bbee265681b79a9ef9795bdc84cf6996f9cfec (diff)
downloadydb-4a64a813e1d34e732f35d8a65147974f76395a6f.tar.gz
Intermediate changes
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/internet/base.py')
-rw-r--r--contrib/python/Twisted/py3/twisted/internet/base.py83
1 files changed, 40 insertions, 43 deletions
diff --git a/contrib/python/Twisted/py3/twisted/internet/base.py b/contrib/python/Twisted/py3/twisted/internet/base.py
index f039dfe5c4e..c807f418731 100644
--- a/contrib/python/Twisted/py3/twisted/internet/base.py
+++ b/contrib/python/Twisted/py3/twisted/internet/base.py
@@ -56,8 +56,10 @@ from twisted.internet.interfaces import (
_ISupportsExitSignalCapturing,
)
from twisted.internet.protocol import ClientFactory
-from twisted.python import log, reflect
+from twisted.logger import Logger
+from twisted.python import reflect
from twisted.python.failure import Failure
+from twisted.python.log import callWithLogger as _callWithLogger
from twisted.python.runtime import platform, seconds as runtimeSeconds
from ._signals import SignalHandling, _WithoutSignalHandling, _WithSignalHandling
@@ -73,6 +75,14 @@ if platform.supportsThreads():
else:
ThreadPool = None # type: ignore[misc, assignment]
+_log = Logger()
+
+# Pre-allocate some static application-code failure logging handlers so that we
+# do not need to allocate them in performance-sensitive bits of code below.
+_topHandler = _log.failureHandler("Unexpected error in main loop")
+_threadCallHandler = _log.failureHandler("while calling from thread")
+_systemEventHandler = _log.failureHandler("While calling system event trigger handler")
+
@implementer(IDelayedCall)
class DelayedCall:
@@ -494,13 +504,11 @@ class _ThreePhaseEvent:
while self.before:
callable, args, kwargs = self.before.pop(0)
self.finishedBefore.append((callable, args, kwargs))
- try:
+ result = None
+ with _systemEventHandler:
result = callable(*args, **kwargs)
- except BaseException:
- log.err()
- else:
- if isinstance(result, Deferred):
- beforeResults.append(result)
+ if isinstance(result, Deferred):
+ beforeResults.append(result)
DeferredList(beforeResults).addCallback(self._continueFiring)
def _continueFiring(self, ignored: object) -> None:
@@ -512,10 +520,8 @@ class _ThreePhaseEvent:
for phase in self.during, self.after:
while phase:
callable, args, kwargs = phase.pop(0)
- try:
+ with _systemEventHandler:
callable(*args, **kwargs)
- except BaseException:
- log.err()
@implementer(IReactorPluggableNameResolver, IReactorPluggableResolver)
@@ -698,19 +704,13 @@ class ReactorBase(PluggableResolverMixin):
def mainLoop(self) -> None:
while self._started:
- try:
- while self._started:
- # Advance simulation time in delayed event
- # processors.
- self.runUntilCurrent()
- t2 = self.timeout()
- t = self.running and t2
- self.doIteration(t)
- except BaseException:
- log.msg("Unexpected error in main loop.")
- log.err()
- else:
- log.msg("Main loop terminated.") # type:ignore[unreachable]
+ with _topHandler:
+ # Advance simulation time in delayed event processors.
+ self.runUntilCurrent()
+ t2 = self.timeout()
+ t = self.running and t2
+ self.doIteration(t)
+ _log.info("Main loop terminated.")
# override in subclasses
@@ -815,7 +815,7 @@ class ReactorBase(PluggableResolverMixin):
@param number: See handler specification in L{signal.signal}
@param frame: See handler specification in L{signal.signal}
"""
- log.msg("Received SIGINT, shutting down.")
+ _log.info("Received SIGINT, shutting down.")
self.callFromThread(self.stop)
self._exitSignal = number
@@ -826,7 +826,7 @@ class ReactorBase(PluggableResolverMixin):
@param number: See handler specification in L{signal.signal}
@param frame: See handler specification in L{signal.signal}
"""
- log.msg("Received SIGBREAK, shutting down.")
+ _log.info("Received SIGBREAK, shutting down.")
self.callFromThread(self.stop)
self._exitSignal = number
@@ -837,7 +837,7 @@ class ReactorBase(PluggableResolverMixin):
@param number: See handler specification in L{signal.signal}
@param frame: See handler specification in L{signal.signal}
"""
- log.msg("Received SIGTERM, shutting down.")
+ _log.info("Received SIGTERM, shutting down.")
self.callFromThread(self.stop)
self._exitSignal = number
@@ -845,7 +845,7 @@ class ReactorBase(PluggableResolverMixin):
"""Disconnect every reader, and writer in the system."""
selectables = self.removeAll()
for reader in selectables:
- log.callWithLogger(
+ _callWithLogger(
reader, reader.connectionLost, Failure(main.CONNECTION_LOST)
)
@@ -1059,10 +1059,8 @@ class ReactorBase(PluggableResolverMixin):
count = 0
total = len(self.threadCallQueue)
for f, a, kw in self.threadCallQueue:
- try:
+ with _threadCallHandler:
f(*a, **kw)
- except BaseException:
- log.err()
count += 1
if count == total:
break
@@ -1085,21 +1083,20 @@ class ReactorBase(PluggableResolverMixin):
heappush(self._pendingTimedCalls, call)
continue
- try:
+ with _log.failuresHandled(
+ "while handling timed call {previous()}",
+ previous=lambda creator=call.creator: (
+ ""
+ if creator is None
+ else "\n"
+ + (" C: from a DelayedCall created here:\n")
+ + " C:"
+ + "".join(creator).rstrip().replace("\n", "\n C:")
+ + "\n"
+ ),
+ ):
call.called = 1
call.func(*call.args, **call.kw)
- except BaseException:
- log.err()
- if call.creator is not None:
- e = "\n"
- e += (
- " C: previous exception occurred in "
- + "a DelayedCall created here:\n"
- )
- e += " C:"
- e += "".join(call.creator).rstrip().replace("\n", "\n C:")
- e += "\n"
- log.msg(e)
if (
self._cancellations > 50