diff --git a/Lib/asyncore.py b/Lib/asyncore.py --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -221,6 +221,7 @@ connected = False accepting = False closing = False + pending_close_evt = False addr = None ignore_log_types = frozenset(['warning']) @@ -380,6 +381,7 @@ if not data: # a closed connection is indicated by signaling # a read condition, and having recv() return 0. + self.pending_close_evt = True self.handle_close() return b'' else: @@ -535,6 +537,7 @@ num_sent = 0 num_sent = dispatcher.send(self, self.out_buffer[:512]) self.out_buffer = self.out_buffer[num_sent:] + return num_sent def handle_write(self): self.initiate_send() @@ -548,6 +551,14 @@ self.out_buffer = self.out_buffer + data self.initiate_send() + def handle_close(self): + if self.pending_close_evt: + self.pending_close_evt = False + # try to drain the output buffer + while self.writable() and self.initiate_send() > 0: + pass + self.close() + # --------------------------------------------------------------------------- # used for debugging. # ---------------------------------------------------------------------------