Allow reconnection from within exception handlers

This implements the changes suggested in
<https://github.com/ammaraskar/pyCraft/issues/146#issuecomment-738914064>,
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.
This commit is contained in:
joo 2020-12-07 23:35:31 +01:00
parent 93db454cb5
commit 73728957e7

View File

@ -457,7 +457,9 @@ class Connection(object):
while self._pop_packet(): while self._pop_packet():
pass 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 self.networking_thread.interrupt = True
if self.socket is not None: if self.socket is not None:
@ -513,8 +515,14 @@ class Connection(object):
except (TypeError, AttributeError): except (TypeError, AttributeError):
pass pass
# Record the exception and cleanly terminate the connection. # Record the exception.
self.exception, self.exc_info = exc, exc_info self.exception, self.exc_info = exc, exc_info
# 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) self.disconnect(immediate=True)
# If allowed by the final exception handler, re-raise the exception. # If allowed by the final exception handler, re-raise the exception.