Skip to content

Change socket.close() to ignore ECONNRESET #74504

Closed

Description

@vstinner
BPO 30319
Nosy @pitrou, @vstinner, @tiran, @vadmium, @dstufft
PRs
  • bpo-30319: socket.close() now ignores ECONNRESET #2565
  • [3.6] bpo-30319: socket.close() now ignores ECONNRESET (#2565) #2566
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-07-05.07:36:24.789>
    created_at = <Date 2017-05-09.16:00:13.568>
    labels = ['3.7', 'type-bug', 'tests']
    title = 'Change socket.close() to ignore ECONNRESET'
    updated_at = <Date 2017-07-06.08:50:25.105>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2017-07-06.08:50:25.105>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-07-05.07:36:24.789>
    closer = 'pitrou'
    components = ['Tests']
    creation = <Date 2017-05-09.16:00:13.568>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30319
    keywords = ['buildbot', '3.6regression']
    message_count = 28.0
    messages = ['293331', '293409', '293438', '295109', '295264', '297463', '297537', '297538', '297539', '297544', '297545', '297551', '297643', '297644', '297645', '297646', '297649', '297650', '297651', '297657', '297659', '297660', '297664', '297708', '297709', '297746', '297790', '297817']
    nosy_count = 6.0
    nosy_names = ['janssen', 'pitrou', 'vstinner', 'christian.heimes', 'martin.panter', 'dstufft']
    pr_nums = ['2565', '2566']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue30319'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. vstinner commented on May 9, 2017

      @vstinner
      MemberAuthor

      http://buildbot.python.org/all/builders/AMD64%20FreeBSD%209.x%203.6/builds/128/steps/test/logs/stdio

      test_invalid_authentication (test.test_imaplib.NewIMAPSSLTests) ... SENT: b'* OK IMAP4rev1'
      GOT: b'OMOH0 CAPABILITY'
      SENT: b'* CAPABILITY IMAP4rev1'
      SENT: b'OMOH0 OK CAPABILITY completed'
      GOT: b'OMOH1 AUTHENTICATE MYAUTH'
      SENT: b'+'
      GOT: b'ZmFrZQ=='
      SENT: b'OMOH1 NO [AUTHENTICATIONFAILED] invalid'
      ERROR

      ======================================================================
      ERROR: test_invalid_authentication (test.test_imaplib.NewIMAPSSLTests)
      ----------------------------------------------------------------------

      Traceback (most recent call last):
        File "/usr/home/buildbot/python/3.6.koobs-freebsd9/build/Lib/test/test_imaplib.py", line 223, in _cleanup
          self.client.shutdown()
        File "/usr/home/buildbot/python/3.6.koobs-freebsd9/build/Lib/imaplib.py", line 326, in shutdown
          self.sock.close()
        File "/usr/home/buildbot/python/3.6.koobs-freebsd9/build/Lib/socket.py", line 417, in close
          self._real_close()
        File "/usr/home/buildbot/python/3.6.koobs-freebsd9/build/Lib/ssl.py", line 1052, in _real_close
          socket._real_close(self)
        File "/usr/home/buildbot/python/3.6.koobs-freebsd9/build/Lib/socket.py", line 411, in _real_close
          _ss.close(self)
      ConnectionResetError: [Errno 54] Connection reset by peer

      Ran 95 tests in 32.694s

      FAILED (errors=1, skipped=2)
      Warning -- threading._dangling was modified by test_imaplib
      Before: <_weakrefset.WeakSet object at 0x805e2b608>
      After: <_weakrefset.WeakSet object at 0x8014a25a0>
      test test_imaplib failed

    2. added
      tests
      on May 9, 2017
    3. changed the title [-]test_invalid_authentication() of test_imaplib: random failure on AMD64 FreeBSD 9.x 3.6[/-] [+]test_invalid_authentication() of test_imaplib: ConnectionResetError: [Errno 54] Connection reset by peer on AMD64 FreeBSD 9.x 3.6[/+] on May 10, 2017
    4. vstinner commented on May 10, 2017

      @vstinner
      MemberAuthor

      See also bpo-30328 (test_ssl) and bpo-30315 (test_ftplib).

    5. vstinner commented on May 10, 2017

      @vstinner
      MemberAuthor

      @our SSL experts: any idea why sock.close() fails with "ConnectionResetError: [Errno 54] Connection reset by peer" in imaplib? Is that a new error? Should imaplib catch ConnectionResetError on sock.close()?

      By the way, see also bpo-30329: I proposed a patch to catch the Windows socket error 10022 on shutdown() for poplib and imaplib SSL connections. No idea why this error only occurs on SSL connections.

    6. changed the title [-]test_invalid_authentication() of test_imaplib: ConnectionResetError: [Errno 54] Connection reset by peer on AMD64 FreeBSD 9.x 3.6[/-] [+]test_invalid_authentication() of test_imaplib: ConnectionResetError: [Errno 54] Connection reset by peer in shutdown() on AMD64 FreeBSD 9.x 3.6[/+] on May 10, 2017
    7. vadmium commented on Jun 3, 2017

      @vadmium
      Member

      Making this an index of related reports:

      bpo-30319: test_imap
      bpo-30315: test_ftplib
      bpo-30543: test_timeout
      bpo-30328: test_ssl
      bpo-27784: test_asyncore.TestAPI_UseIPv6Select.test_handle_accept, test_socketserver
      bpo-30106: test_asyncore.TestAPI_UseIPv6Poll.test_handle_write

      These all look like a side effect of my change to raise an error from the OS as an exception when closing a socket, bpo-26685. Only 3.6+ is affected. According to <https://www.freebsd.org/cgi/man.cgi?close%282%29\>, ECONNRESET means “The underlying object was a stream socket that was shut down by the peer before all pending data was delivered”.

      It seems this is specific to Free BSD. See bug report about Posix compliance: <https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=159179\>. According to <https://forums.zeroc.com/discussion/5569/patch-to-network-cpp-for-freebsd-for-econnreset-on-close-2-problem\> this started in Free BSD 6.3 in 2006.

      I suppose the options are:

      1. Completely revert bpo-26685 and ignore all “socket.close” errors (my least preferred option)
      2. Ignore ECONNRESET in “socket.close” (backwards compatible, could use “os.close” if you really want to check for ECONNRESET)
      3. Ignore ECONNRESET in the various higher-level libraries
      4. Adjust the tests to ignore the error or otherwise avoid the problem
    8. changed the title [-]test_invalid_authentication() of test_imaplib: ConnectionResetError: [Errno 54] Connection reset by peer in shutdown() on AMD64 FreeBSD 9.x 3.6[/-] [+]ConnectionResetError: [Errno 54] Connection reset by peer in socket.close on FreeBSD, Py 3.6[/+] on Jun 3, 2017
    9. added
      type-bug
      on Jun 3, 2017
    10. vstinner commented on Jun 6, 2017

      @vstinner
      MemberAuthor

      Before discussing revert, I would like experimenting fixing calls to
      sock.close(). How much changes do we need to ignore exeptions on
      sock.close()?

    11. vadmium commented on Jul 1, 2017

      @vadmium
      Member

      I think fixing all affected calls to socket.close in the world (option 3) would be too much. I just added two new reports (bpo-30652 and bpo-30391) as dependencies. These are about testing socketserver.TCPServer. As an example, to fix the socket.close call there, I think the change would look like

      class TCPServer:
          def close_request(self, request):
              try:
                  request.close()
              except ConnectionError:
                  # Suppress asynchronous errors such as ECONNRESET on Free BSD
                  pass

      Instead of that change all over the place, I am thinking option 2 would be safest. In Modules/socketmodule.c, something like

      sock_close(PySocketSockObject *s)
      {
          Py_BEGIN_ALLOW_THREADS
          res = SOCKETCLOSE(fd);
          Py_END_ALLOW_THREADS
          /* ECONNRESET can occur on Free BSD */
          if (res < 0 && errno != ECONNRESET) {
              return s->errorhandler();
          }
      }
    12. pitrou commented on Jul 3, 2017

      @pitrou
      Member
      1. Ignore ECONNRESET in “socket.close” (backwards compatible, could use “os.close” if you really want to check for ECONNRESET)

      +1 from me.

    13. pitrou commented on Jul 3, 2017

      @pitrou
      Member

      Please, let's not use the "dependencies" field for issues which are not actual dependencies.

    14. pitrou commented on Jul 3, 2017

      @pitrou
      Member

      None of "27784,30106,30315,30328,30391,30652,30543" are dependencies of this issue.

    15. vstinner commented on Jul 3, 2017

      @vstinner
      MemberAuthor

      tl; dr I agree to ignore ECONNRESET-like errors on socket.shutdown() and sock.close().

      I'm not sure that I understand *exactly* the problem here. In which case closing a socket fails with ECONNRESET? Can it mean that the last write() was buffered and failed?

      If socket.send() is blocking, I expect that send() gets the ECONNRESET error.

      If socket.send() is non-blocking, the application has to be very carefully written to handle many kinds of errors.

      From a high lever point of view, I don't think that ECONNRESET is interested. I expect that a protocol at the application level doesn't rely on ECONNRESET, but a command like "QUIT". No?

      For a file on disk, it's different, since write() is always buffered and close() has to flush pending write on disk.

      For a network protocol, loosing the connection, loosing data, etc. is something "normal", not something execptional. That's why there are application-level commands to close cleanly a connection.

      Ok, now for SSL sockets... Is it also ok to ignore ECONNRESET on sock.close() for an SSL socket?

      --

      ECONNRESET can occur on sock.close(), but not only: see bpo-30329, shutdown() fails can ENOTCONN on UNIX or WSAEINVAL on Windows. I modified poplib and imaplib recently to handle WSAEINVAL:

      commit 83a2c28
      Author: Victor Stinner <victor.stinner@gmail.com>
      Date: Mon May 15 17:33:45 2017 +0200

      bpo-30329: Catch Windows error 10022 on shutdown() (bpo-1538)
      
      Catch the Windows socket WSAEINVAL error (code 10022) in imaplib and
      poplib on shutdown(SHUT_RDWR): An invalid operation was attempted
      
      This error occurs sometimes on SSL connections.
      
    16. 7 remaining items

    17. vstinner commented on Jul 4, 2017

      @vstinner
      MemberAuthor

      I wrote #2565

      • socket.close() now ignores ECONNRESET error
      • socket.shutdown() now ignores ENOTCONN and WSAEINVAL errors
    18. pitrou commented on Jul 4, 2017

      @pitrou
      Member

      Le 04/07/2017 à 14:00, STINNER Victor a écrit :

      Antoine Pitrou: "shutdown() is not the same thing as close()."

      I consider that the bug is similar to socket.close() error,
      [...]

      First, let's not call it a bug when an error is reported to the user. A
      bug would be to silence all errors just because they annoy one core
      developer.

      Second, close() and shutdown() are different functions operating at
      different levels. close() operates at the OS level and releases
      resources associated with the given file descriptor. It might, but
      might not, do any I/O (such as flush buffers or send a TCP RST) -- for
      example, if you duplicated a fd after fork(), calling close() on only
      one of them will do absolutely nothing at the I/O level.

      shutdown() operates at the transport level. Someone who calls
      shutdown() *must* be notified that the shutdown went wrong, because
      that's the only thing shutdown() does. shutdown() does nothing at the
      OS level.

      It makes sense to silence some errors in close() because, most of the
      time, people call close() to release resources and they don't care
      whether the other end of the connection was notified in time. It
      doesn't make sense to do the same for shutdown().

    19. vstinner commented on Jul 4, 2017

      @vstinner
      MemberAuthor

      Ok, I rewrote my PR to only ignore ECONNRESET in socket.close().

    20. vstinner commented on Jul 4, 2017

      @vstinner
      MemberAuthor

      I wrote #2565 because the ConnectionResetError error on socket.close() is more and more a cause of failures on FreeBSD buildbots. I mean that I fixed enough other bugs to start to only see one specific bug more often ;-) (Before we had dozens of random bugs.)

    21. vstinner commented on Jul 4, 2017

      @vstinner
      MemberAuthor

      New changeset 67e1478 by Victor Stinner in branch 'master':
      bpo-30319: socket.close() now ignores ECONNRESET (bpo-2565)
      67e1478

    22. vstinner commented on Jul 4, 2017

      @vstinner
      MemberAuthor

      New changeset 580cd5c by Victor Stinner in branch '3.6':
      bpo-30319: socket.close() now ignores ECONNRESET (bpo-2565) (bpo-2566)
      580cd5c

    23. vstinner commented on Jul 4, 2017

      @vstinner
      MemberAuthor

      Ok. I modified socket.close() to ignore ECONNRESET in Python 3.6 and 3.7. I will leave the issue open a few days to see if it helps to reduce buildbot failure rate.

    24. vstinner commented on Jul 4, 2017

      @vstinner
      MemberAuthor

      I don't see the link between this issue and bpo-30652 or bpo-30391, I removed these dependencies.

      I marked bpo-27784, bpo-30328, bpo-30543 and bpo-30315 as duplicates of this issue.

      bpo-30106 was already fixed, differently, but I mentioned this issue in it.

    25. vstinner commented on Jul 5, 2017

      @vstinner
      MemberAuthor

      Antoine: FYI I abandonned my idea of ignoring errors on socket.shutdown(), since I agree with your rationale. An application may rely on shutdown() exception to trigger some events, and a socket can still be used after a shutdown().

    26. pitrou commented on Jul 5, 2017

      @pitrou
      Member

      Thank you Victor :-)

    27. vstinner commented on Jul 5, 2017

      @vstinner
      MemberAuthor

      New changeset 8207c17 by Victor Stinner in branch 'master':
      Revert "bpo-30822: Fix testing of datetime module." (bpo-2588)
      8207c17

    28. vadmium commented on Jul 6, 2017

      @vadmium
      Member

      Thanks for handling this Victor. To answer some of your earlier questions, this is my understanding of the Free BSD behaviour (although I don't have Free BSD to experiment with):

      When writing to TCP sockets, I believe the data is buffered by the local OS (as well as the network, remote peer, etc). The send call will normally return straight away. In the background, the OS might combine the data with existing buffers, send it to the network, wait for acknowledgements, retransmit it, etc.

      On Free BSD, steps to trigger ECONNRESET might be:

      1. Establish a TCP connection.
      2. Send some data to the remote peer. OS returns immediately without indicating if data will successfully be sent.
      3. Remote receives data packet, but decides the connection is not valid, so responds with reset message. Maybe its socket was shut down, or the OS rebooted.
      4. Close the local socket. If TCP reset message was received in time, Free BSD raises ECONNRESET.

      I understand ECONNRESET is an _indication_ that not all pending data was delivered. But this is asynchronous, and a lack of ECONNRESET does not guarantee that all pending data was delivered. Imagine if steps 3 and 4 were swapped above. I doubt Free BSD will block the close call until the data is acknowledged, so it won't know if the peer will reset the connection in the future.

      To guarantee the data was delivered to the application (not just the remote OS), you do need an application-level acknowledgement.

      For SSL, when you call the top-level SSLSocket.close, I don't think that does much at the SSL level. Again, if you need delivery indication, I would use an app-level acknowledgement. Also beware that by default, Python doesn't report a secure EOF signal sent from the remote peer, so I think you either need a specific app-level message, or should disable the suppress_ragged_eofs mode (see bpo-27815).

      Antoine: sorry for abusing the dependencies list; I will try to avoid that in the future. It seemed the easiest way to get a two-way link to a bunch of other bugs that could be duplicates, but I wasn't sure at the time. My theory was if this bug was fixed, someone could review those other bugs and see if they could also be closed.

    29. vstinner commented on Jul 6, 2017

      @vstinner
      MemberAuthor

      Thanks Martin for the long explanation. To simplify a lot, there is
      and was never any warranty that a successful sock.send() call
      delivered data to the peer. Each layer does its best, but the data can
      be lost at any layer, and the peer is free to close the connection
      *before* getting the data. Yeah, I agree that application level
      signaling is required.

    30. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)teststype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions