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
1 changed files with 11 additions and 3 deletions

View File

@ -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: