From 351a212ccd0cbd2d6f8a5c420a9032604e2d76ac Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Mon, 10 Jul 2023 12:11:05 +0200 Subject: [PATCH] Fixed issue with silent timeouts in scopes --- .idea/misc.xml | 2 +- structio/core/context.py | 4 ---- structio/core/kernels/fifo.py | 10 +++++++--- structio/io/socket.py | 10 +++++----- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 806b876..df4a621 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/structio/core/context.py b/structio/core/context.py index 26b48e6..ca38484 100644 --- a/structio/core/context.py +++ b/structio/core/context.py @@ -70,10 +70,6 @@ class TaskScope: def __exit__(self, exc_type: type, exc_val: BaseException, exc_tb): current_loop().close_scope(self) - if exc_val and isinstance(exc_val, structio.TimedOut): - self.cancelled = True - return self.silent - return False # Just a recursive helper def _get_children(self, lst=None): diff --git a/structio/core/kernels/fifo.py b/structio/core/kernels/fifo.py index ab0af88..cba0cb6 100644 --- a/structio/core/kernels/fifo.py +++ b/structio/core/kernels/fifo.py @@ -249,9 +249,13 @@ class FIFOKernel(BaseKernel): def check_scopes(self): for scope in self.pool.scope.children: if scope.get_actual_timeout() <= self.clock.current_time(): - error = TimedOut("timed out") - error.scope = scope - self.throw(scope.owner, error) + if scope.silent: + self.cancel_scope(scope) + self.reschedule(scope.owner) + else: + error = TimedOut("timed out") + error.scope = scope + self.throw(scope.owner, error) def wakeup(self): while ( diff --git a/structio/io/socket.py b/structio/io/socket.py index 147dc79..f8a1b95 100644 --- a/structio/io/socket.py +++ b/structio/io/socket.py @@ -160,7 +160,7 @@ async def connect_socket( # RFC 8305 specifies that if we get addresses of different families, # our first two connection attempts should be using different ones # (in english: if getaddrinfo() returns, say, 2 IPV4 addresses and one IPV6 - # addr, then we have to make sure our first and second attempt use one + # address, then we have to make sure our first and second attempt use one # of each type) for i in range(1, len(hosts)): # If the family of the ith socket (skipping @@ -188,10 +188,10 @@ async def connect_socket( sockets.append(attempt_sock) if source_address: # This trick (again stolen from Trio), lets us - # bind to a given addr without actually busying + # bind to a given address without actually busying # up a local port up until the moment where we actually # need to connect. That way, we can perform as many connection - # attempts as we want from a given source addr without ever + # attempts as we want from a given source address without ever # worrying about running out of local ports try: attempt_sock.setsockopt( @@ -208,7 +208,7 @@ async def connect_socket( except OSError: # Almost hit the 120 character line, phew... raise OSError( - f"Source addr {source_address!r} is incompatible with remote addr {addr!r}" + f"Source address {source_address!r} is incompatible with remote address {addr!r}" ) await attempt_sock.connect(addr) # Hooray! Connection was successful. Record the socket @@ -218,7 +218,7 @@ async def connect_socket( sockets.remove(attempt_sock) scope.cancel() except OSError: - # Welp, this attempt failed. Right now, we just ignore the error (we'll + # Well, this attempt failed. Right now, we just ignore the error (we'll # fail with OSError later if all connection attempts fail), but we should # really have support for ExceptionGroups (coming soon btw), so we can # keep track of all the errors and use fancy stuff like the new except*