Remove limits on number of packets read/written per tick.

This addresses possible memory leaks or crashes caused by overflowing packet backlogs.
This commit is contained in:
joo committed 2017-10-10 04:47:50 +01:00
1 parent 88a5fdc637
commit af559e181a
1 file changed
+43 -42
+43 -42
View File
@@ -5,7 +5,6 @@ from threading import RLock
import zlib import zlib
import threading import threading
import socket import socket
import time
import timeit import timeit
import select import select
import sys import sys
@@ -366,7 +365,14 @@ class Connection(object):
self.handle_exception(exc, exc_info) self.handle_exception(exc, exc_info)
def _react(self, packet): def _react(self, packet):
self.reactor.react(packet) try:
for listener in self.early_packet_listeners:
listener.call_packet(packet)
self.reactor.react(packet)
for listener in self.packet_listeners:
listener.call_packet(packet)
except IgnorePacket:
pass
class NetworkingThread(threading.Thread): class NetworkingThread(threading.Thread):
@@ -381,6 +387,11 @@ class NetworkingThread(threading.Thread):
def run(self): def run(self):
try: try:
if self.previous_thread is not None:
if self.previous_thread.is_alive():
self.previous_thread.join()
self.previous_thread = None
self.connection.networking_thread = self
self._run() self._run()
except BaseException as e: except BaseException as e:
self.connection._handle_exception(e, sys.exc_info()) self.connection._handle_exception(e, sys.exc_info())
@@ -388,57 +399,46 @@ class NetworkingThread(threading.Thread):
self.connection.networking_thread = None self.connection.networking_thread = None
def _run(self): def _run(self):
if self.previous_thread is not None:
if self.previous_thread.is_alive():
self.previous_thread.join()
self.previous_thread = None
self.connection.networking_thread = self
while not self.interrupt: while not self.interrupt:
# Attempt to write out as many as 300 packets as possible every # Attempt to write out as many as 300 packets.
# 0.05 seconds (20 ticks per second)
num_packets = 0 num_packets = 0
self.connection._write_lock.acquire() with self.connection._write_lock:
try: try:
while self.connection._pop_packet(): while not self.interrupt and self.connection._pop_packet():
num_packets += 1 num_packets += 1
if num_packets >= 300: if num_packets >= 300:
break break
exc_info = None exc_info = None
except: except IOError:
exc_info = sys.exc_info() exc_info = sys.exc_info()
self.connection._write_lock.release()
# Read and react to as many as 50 packets # If any packets remain to be written, resume writing as soon
num_packets = 0 # as possible after reading any available packets; otherwise,
# wait for up to 50ms (1 tick) for new packets to arrive.
if self.connection._outgoing_packet_queue:
read_timeout = 0
else:
read_timeout = 0.05
# Read and react to as many as 50 packets.
while num_packets < 50 and not self.interrupt: while num_packets < 50 and not self.interrupt:
packet = self.connection.reactor.read_packet( packet = self.connection.reactor.read_packet(
self.connection.file_object) self.connection.file_object, timeout=read_timeout)
if not packet: if not packet:
break break
num_packets += 1 num_packets += 1
self.connection._react(packet)
read_timeout = 0
# Do not raise an IOError if it occurred while a disconnect # Ignore the earlier exception if a disconnect packet is
# packet was received, as this may be part of an orderly # received, as it may have been caused by trying to write to
# disconnection. # thw closed socket, which does not represent a program error.
if packet.packet_name == 'disconnect' and \ if exc_info is not None and packet.name == 'disconnect':
exc_info is not None and isinstance(exc_info[1], IOError):
exc_info = None exc_info = None
try:
for listener in self.connection.early_packet_listeners:
listener.call_packet(packet)
self.connection._react(packet)
for listener in self.connection.packet_listeners:
listener.call_packet(packet)
except IgnorePacket:
pass
if exc_info is not None: if exc_info is not None:
raise_(*exc_info) raise_(*exc_info)
time.sleep(0.05)
class IgnorePacket(Exception): class IgnorePacket(Exception):
""" """
@@ -455,7 +455,6 @@ class PacketReactor(object):
Reads and reacts to packets Reads and reacts to packets
""" """
state_name = None state_name = None
TIME_OUT = 0
# Handshaking is considered the "default" state # Handshaking is considered the "default" state
get_clientbound_packets = staticmethod(clientbound.handshake.get_packets) get_clientbound_packets = staticmethod(clientbound.handshake.get_packets)
@@ -467,8 +466,10 @@ class PacketReactor(object):
packet.get_id(context): packet packet.get_id(context): packet
for packet in self.__class__.get_clientbound_packets(context)} for packet in self.__class__.get_clientbound_packets(context)}
def read_packet(self, stream): def read_packet(self, stream, timeout=0):
ready_to_read = select.select([stream], [], [], self.TIME_OUT)[0] # Block for up to `timeout' seconds waiting for `stream' to become
# readable, returning `None' if the timeout elapses.
ready_to_read = select.select([stream], [], [], timeout)[0]
if ready_to_read: if ready_to_read:
length = VarInt.read(stream) length = VarInt.read(stream)