[New-bugs-announce] [issue23351] socket.settimeout(5.0) does not have any effect

Piotr Jurkiewicz report at bugs.python.org
Fri Jan 30 02:28:12 CET 2015


New submission from Piotr Jurkiewicz:

After setting socket.settimeout(5.0), socket.send() returns immediately, instead of returning after specified timeout.

Steps to reproduce:

Open two python interpreters.

In the first one (the receiver) execute:

>>> import socket
>>> r = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
>>> r.bind("test.sock")

In the second one (the sender) execute:

>>> import socket
>>> s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)

Then run the following command 11 times:

>>> s.sendto("msg", "test.sock")

On the 12 run command will block. This happens because datagram sockets queue on Linux is 11 messages long. Interrupt the command.

So far so good.

Then set sender socket timeout:

>>> s.settimeout(5.0)

Expected behavior:

s.sendto() should block for a 5 seconds and THEN raise error 11 (EAGAIN/EWOULDBLOCK).

Actual behavior:

s.sendto() raises the error IMMEDIATELY.

>>> s.sendto("msg", "test.sock")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: [Errno 11] Resource temporarily unavailable

So, in fact, s.settimeout(5.0) does not have any effect.

I think that problem is that settimeout() sets the socket to the non-blocking mode (docs say: "Timeout mode internally sets the socket in non-blocking mode.").

As described [here](http://stackoverflow.com/q/13556972/2178047) setting timeout on non-blocking sockets is impossible.

In fact, when I set timeout manually with setsockopt(), everything works as expected:

>>> s.setblocking(1)              #go back to blocking mode
>>> tv = struct.pack("ll", 5, 0)
>>> s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDTIMEO, tv)

Now s.sendto() raises the error after 5 seconds, as expected.

----------
components: IO, Library (Lib)
messages: 235013
nosy: piotrjurkiewicz
priority: normal
severity: normal
status: open
title: socket.settimeout(5.0) does not have any effect
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23351>
_______________________________________


More information about the New-bugs-announce mailing list