[pypy-issue] Issue #2824: Sockets are left blocking when a positive default timeout is set (pypy/pypy)

Filip Salomonsson issues-reply at bitbucket.org
Fri May 4 15:51:37 EDT 2018


New issue 2824: Sockets are left blocking when a positive default timeout is set
https://bitbucket.org/pypy/pypy/issues/2824/sockets-are-left-blocking-when-a-positive

Filip Salomonsson:

After a positive default socket timeout is set using `socket.setdefaulttimeout()`, the timeout value is set on new socket objects, and their `gettimeout()` method returns the correct value. But (as I understand it) since it isn't applied using the `settimeout()` method, the socket is never set to be non-blocking, and the timeout will not be properly applied during socket operations.

The script below results in the expected `socket.timeout` when running in CPython, but goes on to be interrupted by the alarm signal in PyPy.


```
#!python

from __future__ import print_function

import errno
import signal
import socket
import sys
import time


signal.signal(signal.SIGALRM, lambda signum, frame: None)
socket.setdefaulttimeout(0.5)

signal.alarm(3)
t0 = time.time()
sock = socket.socket()

# Uncommenting this works around the issue
# sock.settimeout(sock.gettimeout())

print(sys.version)

try:
    # blackhole silently drops all incoming traffic
    sock.connect(("blackhole.webpagetest.org", 80))
except socket.timeout:
    print("Yes! Got socket.timeout", end="")
except socket.error as e:
    if e.errno == errno.EINTR:
        print("Noo! Interrupted by signal", end="")
    else:
        raise

print(" after {:.2f} seconds\n".format((time.time() - t0)))

```


```
$ python2 sockettimeout.py
2.7.15 (default, May  1 2018, 16:44:08)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]
Yes! Got socket.timeout after 0.51 seconds
```
```
$ python3 sockettimeout.py
3.6.5 (default, Apr 25 2018, 14:23:58)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]
Yes! Got socket.timeout after 0.51 seconds
```
```
$ pypy/goal/pypy-c sockettimeout.py
2.7.13 (0525720a751e, May 03 2018, 20:55:08)
[PyPy 6.1.0-alpha0 with GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]
Noo! Interrupted by signal after 3.00 seconds
```




More information about the pypy-issue mailing list