requests.{get,post} timeout

Marko Rauhamaa marko at pacujo.net
Wed Aug 23 12:59:43 EDT 2017


Chris Angelico <rosuav at gmail.com>:

> But also, this honestly isn't as big an issue as you might think. If
> the user thinks a program has been running for too long, s/he can hit
> Ctrl-C. Voila! Signal is sent, which aborts a socket read,

Well, no, it doesn't. First run:

========================================================================
nc -l -p 12345
========================================================================

in one window. Then, execute this program in another one:

========================================================================
import threading, socket

def f():
    s = socket.socket()
    try:
        s.connect(("localhost4", 12345))
        s.recv(1000)
    finally:
        s.close()

t = threading.Thread(target=f)
t.start()
t.join()
========================================================================

After you hit Ctrl-C once (under Linux), you get this trace:

========================================================================
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    t.join()
  File "/usr/lib64/python3.5/threading.py", line 1054, in join
    self._wait_for_tstate_lock()
  File "/usr/lib64/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
========================================================================

The program hangs, though, and "nc" doesn't terminate indicating that
the socket hasn't closed.

Then, press Ctrl-C again to get:

========================================================================
Exception ignored in: <module 'threading' from '/usr/lib64/python3.5/threading.py'>
Traceback (most recent call last):
  File "/usr/lib64/python3.5/threading.py", line 1288, in _shutdown
    t.join()
  File "/usr/lib64/python3.5/threading.py", line 1054, in join
    self._wait_for_tstate_lock()
  File "/usr/lib64/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
========================================================================

and the program terminates.


Marko



More information about the Python-list mailing list