Commit e7f914b
committed
gh-130141: clean up asyncio._SelectorTransport in __del__
__del__ closed the socket but stopped there: the file descriptor stayed
registered with the event loop and self._sock_fd kept pointing at a
number the OS is now free to hand out to anything else. So the loop
could go on polling a descriptor the transport no longer owned, and a
transport resurrected during garbage collection could later call
close() and unregister an unrelated descriptor from the selector,
hanging whoever was waiting on it.
Leave the transport in the state that _force_close() and
_call_connection_lost() would instead: clear the buffer and drop the
writer, mark the transport as closing and drop the reader, invalidate
_sock_fd before closing the socket, then unlink the protocol, the loop
and the server. All of it stays under "if self._sock is not None", so
a finalizer running on a transport that has already lost its socket
still does nothing. That matters for the server link especially:
_call_connection_lost() calls server._detach() before it clears
self._server, so a _detach() that raises leaves _server set on a
transport whose socket is gone, and an unguarded finalizer would then
detach a second time.
The ResourceWarning is issued first, while repr() can still report the
fd and the selector state. _call_connection_lost() also sets _sock_fd
to -1 now, so the "_sock_fd is valid only while _sock is open"
invariant holds on every path that closes the socket; as a result
repr() of a closed transport reports fd=-1.
This is the patch Guido van Rossum worked out in the issue thread,
rebased, with the cleanup kept inside the socket guard and extended
with the _buffer_size reset that current main needs, plus the unit
tests the earlier attempt was missing.1 parent 0546b5f commit e7f914b
3 files changed
Lines changed: 132 additions & 3 deletions
File tree
- Lib
- asyncio
- test/test_asyncio
- Misc/NEWS.d/next/Library
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
878 | 878 | | |
879 | 879 | | |
880 | 880 | | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
881 | 885 | | |
882 | | - | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
883 | 905 | | |
884 | | - | |
885 | | - | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
886 | 914 | | |
887 | 915 | | |
888 | 916 | | |
| |||
914 | 942 | | |
915 | 943 | | |
916 | 944 | | |
| 945 | + | |
917 | 946 | | |
918 | 947 | | |
| 948 | + | |
919 | 949 | | |
920 | 950 | | |
921 | 951 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
542 | 542 | | |
543 | 543 | | |
544 | 544 | | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
545 | 614 | | |
546 | 615 | | |
547 | 616 | | |
| |||
599 | 668 | | |
600 | 669 | | |
601 | 670 | | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
602 | 696 | | |
603 | 697 | | |
604 | 698 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
0 commit comments