Skip to content

Instantly share code, notes, and snippets.

@mattbennett
Last active March 9, 2018 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbennett/888e42af88c2733918ad1810956abbde to your computer and use it in GitHub Desktop.
Save mattbennett/888e42af88c2733918ad1810956abbde to your computer and use it in GitHub Desktop.
Kombu unsafe default socket timeout
from kombu import Connection
import time
import threading
import socket
AMQP_URI = "pyamqp://guest:guest@localhost:5672/"
DEFAULT_TIMEOUT = 1.0
run = threading.Event()
socket.setdefaulttimeout(DEFAULT_TIMEOUT)
def one():
while run.is_set():
with Connection(AMQP_URI) as conn:
conn.collect(socket_timeout=0.1)
def two():
while run.is_set():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
assert sock.gettimeout() == DEFAULT_TIMEOUT
except AssertionError:
run.clear()
raise
run.set()
t1 = threading.Thread(target=one)
t2 = threading.Thread(target=two)
try:
t1.start()
t2.start()
while run.is_set():
time.sleep(1)
except KeyboardInterrupt:
run.clear()
t1.join()
t2.join()
@mattbennett
Copy link
Author

$ python repro.py
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/Users/mattbennett/.pyenv/versions/2.7.10/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/Users/mattbennett/.pyenv/versions/2.7.10/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "repro.py", line 26, in two
    assert sock.gettimeout() == DEFAULT_TIMEOUT
AssertionError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment