This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: closed sockets don't raise EBADF anymore
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2, Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: colinmarc, exarkun, giampaolo.rodola, martin.panter, pconnell, pitrou
Priority: low Keywords:

Created on 2011-11-04 21:14 by pitrou, last changed 2022-04-11 14:57 by admin.

Messages (3)
msg147039 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-11-04 21:14
This decrepancy between 2.x and 3.x is witnessed under Windows:

Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> sock = socket.create_connection(("www.python.org", 80))
>>> sock.close()
>>> sock.send(b"x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python27\lib\socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

Python 3.2.1 (default, Jul 10 2011, 20:02:51) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> sock = socket.socket(); sock.connect(("www.python.org", 80))
>>> sock.close()
>>> sock.send(b"x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: [Errno 10038] An operation was attempted on something that is not
a socket


I'm not sure this is worth fixing, though.
msg147041 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-11-04 21:21
discrepancy, not decrepancy :S

(10038 is WSAENOTSOCK, by the way)
msg269148 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-06-24 02:20
According to strace, Python 3 is calling send(-1, ...):

sendto(-1, "x", 1, 0, NULL, 0)          = -1 EBADF (Bad file descriptor)

A related discrepancy between Python 2 and 3 is how the socket.makefile() objects affect the original socket. In Python 2:

>>> f = sock.makefile("wb")
>>> sock.close()  # Should “close” Python’s sock object, but not f
>>> sock.send(b"x")
socket.error: [Errno 9] Bad file descriptor

In Python 3:
>>> sock.send(b"x")  # Actually sent to remote end!
1
History
Date User Action Args
2022-04-11 14:57:23adminsetgithub: 57553
2016-06-24 02:20:45martin.pantersetnosy: + martin.panter
messages: + msg269148
2013-11-23 13:53:53pconnellsetnosy: + pconnell
2012-04-20 17:51:22colinmarcsetnosy: + colinmarc
2011-11-04 21:29:20giampaolo.rodolasetnosy: + giampaolo.rodola
2011-11-04 21:21:25pitrousetmessages: + msg147041
2011-11-04 21:14:43pitroucreate