From 73728957e776514ab055c98b229f5333d9bf049c Mon Sep 17 00:00:00 2001 From: joo Date: Mon, 7 Dec 2020 23:35:31 +0100 Subject: [PATCH] Allow reconnection from within exception handlers This implements the changes suggested in , i.e.: 1. `minecraft.networking.Connection.disconnect' now correctly terminates the new networking thread if it is still waiting to replace the old one, and 2. `minecraft.networking.Connectoin._handle_exception' no longer calls `disconnect' if any exception handler has initiated a new connection. --- minecraft/networking/connection.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/minecraft/networking/connection.py b/minecraft/networking/connection.py index 759416d..79ded2c 100644 --- a/minecraft/networking/connection.py +++ b/minecraft/networking/connection.py @@ -457,7 +457,9 @@ class Connection(object): while self._pop_packet(): pass - if self.networking_thread is not None: + if self.new_networking_thread is not None: + self.new_networking_thread.interrupt = True + elif self.networking_thread is not None: self.networking_thread.interrupt = True if self.socket is not None: @@ -513,9 +515,15 @@ class Connection(object): except (TypeError, AttributeError): pass - # Record the exception and cleanly terminate the connection. + # Record the exception. self.exception, self.exc_info = exc, exc_info - self.disconnect(immediate=True) + + # The following condition being false indicates that an exception + # handler has initiated a new connection, meaning that we should not + # interfere with the connection state. Otherwise, make sure that any + # current connection is completely terminated. + if (self.new_networking_thread or self.networking_thread).interrupt: + self.disconnect(immediate=True) # If allowed by the final exception handler, re-raise the exception. if final_handler is None and not caught: