Is a socket in an error state ready for reading according to select()?
(I'm using CPython v2.6 (on Linux, Windows, and OSX), but I don't think my question is specific to Python or my target systems.)
If my application is blocked in a select([sock], [], [], None)
call and the remote side is killed abruptly, can I rely on my select call unblocking even though I'm not selecting for errors?
I know that the proper way to do these things is to also check for errors in the socket (e.g. select([sock], [], [sock], None)
), but the documentation I read for se开发者_如何学Pythonlect says that the definition of error varies from system to system.
I am not sure how much of this may be "implementation dependent" since there is a little fuzziness to this area of select() depending on how you read the man page, but the systems I have worked on would return the socket as readable and then your read would return an error (this assumes TCP of course and the remote side has RST the connection.) The key part is "descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking." which if the read returned immediately with an error would be a true statement. I would recommend verifying this behavior on your target systems by writing a simple client server, have the client connect, and then select for read. Have the server accept, sleep for a second, and then exit itself. If what I have typically seen is standard to you, the select will return with the socket as readable, and your read will not block but will return an error of connection reset.
Oh yes, and just to make matters worse (from linux man pages)
Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to use O_NONBLOCK on sockets that should not block.
精彩评论