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: I/O operation on closed socket: improve the error message
Type: Stage:
Components: Versions: Python 3.0
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: patch

Created on 2009-01-06 00:31 by vstinner, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
socket_error_closed.patch vstinner, 2009-01-06 00:31
Messages (2)
msg79217 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-01-06 00:31
I don't like the current behaviour of Python on closed socket:

>>> import socket
>>> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.close()
>>> s.fileno()
-1
>>> s.getsockname()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: [Errno 9] Bad file descriptor

Some operations are still allowed whereas the other raise an ugly 
error message. Most (or all) operation on a closed socket should be 
blocked by a socket.error.

My patch raises a socket.error("I/O operation on closed socket") for 
most operations except:
 - close(): it call be called twice (or more) (keep current behaviour)
 - gettimeout(): should we raise an error?
 - setblocking(), settimeout(): should we raise an error? Maybe yes 
for setter but no for getter which would be inconsistent with 
getpeername()...

The io library already has this behaviour: read(), write(), flush(), 
etc. are blocked by a ValueError("I/O operation on closed file") (see 
IOBase._check_closed in the io library).

Issue #4791 changes the behaviour of closed SocketIO object: fileno() 
method raise a socket.error if it's closed even if the socket is still 
open.
msg96246 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-12-11 11:42
Well, this issue is not really important. The current behaviour is already 
correct (raising an error if the file is closed).
History
Date User Action Args
2022-04-11 14:56:43adminsetgithub: 49103
2009-12-11 11:42:38vstinnersetstatus: open -> closed
resolution: wont fix
messages: + msg96246
2009-01-06 00:31:02vstinnercreate