From 38deda0d3b85ede6eac3be0f6a43ee13c6d47854 Mon Sep 17 00:00:00 2001 From: joo Date: Fri, 2 Sep 2016 00:26:12 +0100 Subject: [PATCH] Fix race condition causing failed Travis builds #75.3 and #81.1. --- minecraft/networking/connection.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/minecraft/networking/connection.py b/minecraft/networking/connection.py index caa7af8..659a6e2 100644 --- a/minecraft/networking/connection.py +++ b/minecraft/networking/connection.py @@ -184,21 +184,25 @@ class Connection(object): Attempt to begin connecting to the server. May safely be called multiple times after the first, i.e. to reconnect. """ - self.spawned = False with self._write_lock: + # We hold the lock throughout, as connect() may be called by both + # the network thread and a parent thread simultaneously, during + # automatic version negotiation. + + self.spawned = False self._outgoing_packet_queue = deque() - self._connect() - self._handshake() + self._connect() + self._handshake() + login_start_packet = packets.LoginStartPacket() + if self.auth_token: + login_start_packet.name = self.auth_token.profile.name + else: + login_start_packet.name = self.username + self.write_packet(login_start_packet) - self.reactor = LoginReactor(self) - self._start_network_thread() - login_start_packet = packets.LoginStartPacket() - if self.auth_token: - login_start_packet.name = self.auth_token.profile.name - else: - login_start_packet.name = self.username - self.write_packet(login_start_packet) + self.reactor = LoginReactor(self) + self._start_network_thread() def _connect(self): # Connect a socket to the server and create a file object from the