# HG changeset patch # Parent 3d6b6736174996211f8946f0626c7524fa418c04 Issue #26685: Raise OSError if closing a socket fails diff -r 3d6b67361749 Doc/library/socket.rst --- a/Doc/library/socket.rst Fri Apr 01 06:55:55 2016 +0000 +++ b/Doc/library/socket.rst Mon Apr 04 01:47:32 2016 +0000 @@ -868,6 +868,10 @@ it is recommended to :meth:`close` them explicitly, or to use a :keyword:`with` statement around them. + .. versionchanged:: 3.6 + :exc:`OSError` is now raised if an error occurs when the underlying + :c:func:`close` call is made. + .. note:: :meth:`close()` releases the resource associated with a connection but diff -r 3d6b67361749 Lib/test/test_pty.py --- a/Lib/test/test_pty.py Fri Apr 01 06:55:55 2016 +0000 +++ b/Lib/test/test_pty.py Mon Apr 04 01:47:32 2016 +0000 @@ -277,7 +277,6 @@ socketpair = self._socketpair() masters = [s.fileno() for s in socketpair] - os.close(masters[1]) socketpair[1].close() os.close(write_to_stdin_fd) diff -r 3d6b67361749 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Fri Apr 01 06:55:55 2016 +0000 +++ b/Lib/test/test_socket.py Mon Apr 04 01:47:32 2016 +0000 @@ -1161,6 +1161,16 @@ sock.close() self.assertRaises(OSError, sock.send, b"spam") + def testCloseException(self): + sock = socket.socket() + socket.socket(fileno=sock.fileno()).close() + try: + sock.close() + except OSError as err: + self.assertEqual(err.errno, errno.EBADF) + else: + self.fail("close() should raise EBADF") + def testNewAttributes(self): # testing .family, .type and .protocol diff -r 3d6b67361749 Misc/NEWS --- a/Misc/NEWS Fri Apr 01 06:55:55 2016 +0000 +++ b/Misc/NEWS Mon Apr 04 01:47:32 2016 +0000 @@ -237,6 +237,8 @@ Library ------- +- Issue #26685: Raise OSError if closing a socket fails. + - Issue #26676: Added missing XMLPullParser to ElementTree.__all__. - Issue #22854: Change BufferedReader.writable() and diff -r 3d6b67361749 Modules/socketmodule.c --- a/Modules/socketmodule.c Fri Apr 01 06:55:55 2016 +0000 +++ b/Modules/socketmodule.c Mon Apr 04 01:47:32 2016 +0000 @@ -2576,6 +2576,7 @@ sock_close(PySocketSockObject *s) { SOCKET_T fd; + int res; fd = s->sock_fd; if (fd != -1) { @@ -2586,8 +2587,11 @@ http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html for more details. */ Py_BEGIN_ALLOW_THREADS - (void) SOCKETCLOSE(fd); + res = SOCKETCLOSE(fd); Py_END_ALLOW_THREADS + if (res < 0) { + return s->errorhandler(); + } } Py_INCREF(Py_None); return Py_None;