@@ -344,7 +344,7 @@ def __init__(self, loop, sock, protocol, extra, server=None):
344
344
self ._protocol = protocol
345
345
self ._server = server
346
346
self ._buffer = collections .deque ()
347
- self ._conn_lost = 0
347
+ self ._conn_lost = 0 # Set when call to connection_lost scheduled.
348
348
self ._closing = False # Set when close() called.
349
349
if server is not None :
350
350
server .attach (self )
@@ -356,27 +356,27 @@ def close(self):
356
356
if self ._closing :
357
357
return
358
358
self ._closing = True
359
- self ._conn_lost += 1
360
359
self ._loop .remove_reader (self ._sock_fd )
361
360
if not self ._buffer :
361
+ self ._conn_lost += 1
362
362
self ._loop .call_soon (self ._call_connection_lost , None )
363
363
364
364
def _fatal_error (self , exc ):
365
- # should be called from exception handler only
366
- logger .exception ('Fatal error for %s' , self )
365
+ # Should be called from exception handler only.
366
+ if not isinstance (exc , (BrokenPipeError , ConnectionResetError )):
367
+ logger .exception ('Fatal error for %s' , self )
367
368
self ._force_close (exc )
368
369
369
370
def _force_close (self , exc ):
371
+ if self ._conn_lost :
372
+ return
370
373
if self ._buffer :
371
374
self ._buffer .clear ()
372
375
self ._loop .remove_writer (self ._sock_fd )
373
-
374
- if self ._closing :
375
- return
376
-
377
- self ._closing = True
376
+ if not self ._closing :
377
+ self ._closing = True
378
+ self ._loop .remove_reader (self ._sock_fd )
378
379
self ._conn_lost += 1
379
- self ._loop .remove_reader (self ._sock_fd )
380
380
self ._loop .call_soon (self ._call_connection_lost , exc )
381
381
382
382
def _call_connection_lost (self , exc ):
@@ -424,8 +424,6 @@ def _read_ready(self):
424
424
data = self ._sock .recv (self .max_size )
425
425
except (BlockingIOError , InterruptedError ):
426
426
pass
427
- except ConnectionResetError as exc :
428
- self ._force_close (exc )
429
427
except Exception as exc :
430
428
self ._fatal_error (exc )
431
429
else :
@@ -453,17 +451,15 @@ def write(self, data):
453
451
try :
454
452
n = self ._sock .send (data )
455
453
except (BlockingIOError , InterruptedError ):
456
- n = 0
457
- except (BrokenPipeError , ConnectionResetError ) as exc :
458
- self ._force_close (exc )
459
- return
460
- except OSError as exc :
454
+ pass
455
+ except Exception as exc :
461
456
self ._fatal_error (exc )
462
457
return
463
458
else :
464
459
data = data [n :]
465
460
if not data :
466
461
return
462
+
467
463
# Start async I/O.
468
464
self ._loop .add_writer (self ._sock_fd , self ._write_ready )
469
465
@@ -478,9 +474,6 @@ def _write_ready(self):
478
474
n = self ._sock .send (data )
479
475
except (BlockingIOError , InterruptedError ):
480
476
self ._buffer .append (data )
481
- except (BrokenPipeError , ConnectionResetError ) as exc :
482
- self ._loop .remove_writer (self ._sock_fd )
483
- self ._force_close (exc )
484
477
except Exception as exc :
485
478
self ._loop .remove_writer (self ._sock_fd )
486
479
self ._fatal_error (exc )
@@ -493,7 +486,6 @@ def _write_ready(self):
493
486
elif self ._eof :
494
487
self ._sock .shutdown (socket .SHUT_WR )
495
488
return
496
-
497
489
self ._buffer .append (data ) # Try again later.
498
490
499
491
def write_eof (self ):
@@ -622,8 +614,6 @@ def _on_ready(self):
622
614
except (BlockingIOError , InterruptedError ,
623
615
ssl .SSLWantReadError , ssl .SSLWantWriteError ):
624
616
pass
625
- except ConnectionResetError as exc :
626
- self ._force_close (exc )
627
617
except Exception as exc :
628
618
self ._fatal_error (exc )
629
619
else :
@@ -644,10 +634,6 @@ def _on_ready(self):
644
634
except (BlockingIOError , InterruptedError ,
645
635
ssl .SSLWantReadError , ssl .SSLWantWriteError ):
646
636
n = 0
647
- except (BrokenPipeError , ConnectionResetError ) as exc :
648
- self ._loop .remove_writer (self ._sock_fd )
649
- self ._force_close (exc )
650
- return
651
637
except Exception as exc :
652
638
self ._loop .remove_writer (self ._sock_fd )
653
639
self ._fatal_error (exc )
@@ -726,12 +712,12 @@ def sendto(self, data, addr=None):
726
712
else :
727
713
self ._sock .sendto (data , addr )
728
714
return
715
+ except (BlockingIOError , InterruptedError ):
716
+ self ._loop .add_writer (self ._sock_fd , self ._sendto_ready )
729
717
except ConnectionRefusedError as exc :
730
718
if self ._address :
731
719
self ._fatal_error (exc )
732
720
return
733
- except (BlockingIOError , InterruptedError ):
734
- self ._loop .add_writer (self ._sock_fd , self ._sendto_ready )
735
721
except Exception as exc :
736
722
self ._fatal_error (exc )
737
723
return
@@ -746,13 +732,13 @@ def _sendto_ready(self):
746
732
self ._sock .send (data )
747
733
else :
748
734
self ._sock .sendto (data , addr )
735
+ except (BlockingIOError , InterruptedError ):
736
+ self ._buffer .appendleft ((data , addr )) # Try again later.
737
+ break
749
738
except ConnectionRefusedError as exc :
750
739
if self ._address :
751
740
self ._fatal_error (exc )
752
741
return
753
- except (BlockingIOError , InterruptedError ):
754
- self ._buffer .appendleft ((data , addr )) # Try again later.
755
- break
756
742
except Exception as exc :
757
743
self ._fatal_error (exc )
758
744
return
@@ -765,5 +751,4 @@ def _sendto_ready(self):
765
751
def _force_close (self , exc ):
766
752
if self ._address and isinstance (exc , ConnectionRefusedError ):
767
753
self ._protocol .connection_refused (exc )
768
-
769
754
super ()._force_close (exc )
0 commit comments