Skip to content

Instantly share code, notes, and snippets.

@khamidou
Last active February 28, 2022 08:57
Show Gist options
  • Save khamidou/6b7e8702f8fc0e8dd251 to your computer and use it in GitHub Desktop.
Save khamidou/6b7e8702f8fc0e8dd251 to your computer and use it in GitHub Desktop.
Ever got a mysterious "IOError: cannot send to <Channel id=1 closed>" from pytest with xdist and gevent? Scroll to the comments to see how to solve it.
vagrant@precise64:/vagrant/tests$ py.test -n 2 general/
================================================= test session starts ==================================================
platform linux2 -- Python 2.7.3 -- py-1.4.26 -- pytest-2.6.4
plugins: xdist, httpretty
gw0 [24] / gw1 okINTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 84, in wrap_session
INTERNALERROR> doit(config, session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 122, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 413, in __call__
INTERNALERROR> return self._docall(methods, kwargs)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 424, in _docall
INTERNALERROR> res = mc.execute()
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 315, in execute
INTERNALERROR> res = method(**kwargs)
INTERNALERROR> File "<remote exec>", line 47, in pytest_runtestloop
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 696, in receive
INTERNALERROR> x = itemqueue.get(timeout=timeout)
INTERNALERROR> File "/usr/lib/python2.7/Queue.py", line 168, in get
INTERNALERROR> self.not_empty.wait()
INTERNALERROR> File "/usr/lib/python2.7/threading.py", line 243, in wait
INTERNALERROR> waiter.acquire()
INTERNALERROR> File "_semaphore.pyx", line 112, in gevent._semaphore.Semaphore.acquire (gevent/gevent._semaphore.c:3004)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 331, in switch
INTERNALERROR> return greenlet.switch(self)
INTERNALERROR> LoopExit: This operation would block forever
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 7, in <module>
File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 1459, in serve
SlaveGateway(io=io, id=id, _startcount=2).serve()
File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 1004, in serve
self._execpool.integrate_as_primary_thread()
File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 232, in integrate_as_primary_thread
primary_thread_task_ready.wait()
File "/usr/lib/python2.7/threading.py", line 403, in wait
self.__cond.wait(timeout)
File "/usr/lib/python2.7/threading.py", line 243, in wait
waiter.acquire()
File "_semaphore.pyx", line 112, in gevent._semaphore.Semaphore.acquire (gevent/gevent._semaphore.c:3004)
File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 331, in switch
return greenlet.switch(self)
gevent.hub.LoopExit: This operation would block forever
Exception KeyError: KeyError(34141520,) in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 84, in wrap_session
INTERNALERROR> doit(config, session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 122, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 413, in __call__
INTERNALERROR> return self._docall(methods, kwargs)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 424, in _docall
INTERNALERROR> res = mc.execute()
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 315, in execute
INTERNALERROR> res = method(**kwargs)
INTERNALERROR> File "<remote exec>", line 47, in pytest_runtestloop
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 696, in receive
INTERNALERROR> x = itemqueue.get(timeout=timeout)
INTERNALERROR> File "/usr/lib/python2.7/Queue.py", line 168, in get
INTERNALERROR> self.not_empty.wait()
INTERNALERROR> File "/usr/lib/python2.7/threading.py", line 243, in wait
INTERNALERROR> waiter.acquire()
INTERNALERROR> File "_semaphore.pyx", line 112, in gevent._semaphore.Semaphore.acquire (gevent/gevent._semaphore.c:3004)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 331, in switch
INTERNALERROR> return greenlet.switch(self)
INTERNALERROR> LoopExit: This operation would block forever
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 7, in <module>
File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 1459, in serve
SlaveGateway(io=io, id=id, _startcount=2).serve()
File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 1004, in serve
self._execpool.integrate_as_primary_thread()
File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 232, in integrate_as_primary_thread
primary_thread_task_ready.wait()
File "/usr/lib/python2.7/threading.py", line 403, in wait
self.__cond.wait(timeout)
File "/usr/lib/python2.7/threading.py", line 243, in wait
waiter.acquire()
File "_semaphore.pyx", line 112, in gevent._semaphore.Semaphore.acquire (gevent/gevent._semaphore.c:3004)
File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 331, in switch
return greenlet.switch(self)
gevent.hub.LoopExit: This operation would block forever
Exception KeyError: KeyError(34202960,) in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
gw0 [24] / gw1 [24]
scheduling tests via LoadScheduling
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 84, in wrap_session
INTERNALERROR> doit(config, session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/main.py", line 122, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 413, in __call__
INTERNALERROR> return self._docall(methods, kwargs)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 424, in _docall
INTERNALERROR> res = mc.execute()
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/_pytest/core.py", line 315, in execute
INTERNALERROR> res = method(**kwargs)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/xdist/dsession.py", line 504, in pytest_runtestloop
INTERNALERROR> self.loop_once()
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/xdist/dsession.py", line 522, in loop_once
INTERNALERROR> call(**kwargs)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/xdist/dsession.py", line 597, in slave_collectionfinish
INTERNALERROR> self.sched.init_distribute()
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/xdist/dsession.py", line 367, in init_distribute
INTERNALERROR> self._send_tests(node, node_chunksize)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/xdist/dsession.py", line 374, in _send_tests
INTERNALERROR> node.send_runtest_some(tests_per_node)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/xdist/slavemanage.py", line 240, in send_runtest_some
INTERNALERROR> self.sendcommand("runtests", indices=indices)
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/xdist/slavemanage.py", line 255, in sendcommand
INTERNALERROR> self.channel.send((name, kwargs))
INTERNALERROR> File "/usr/local/lib/python2.7/dist-packages/execnet/gateway_base.py", line 680, in send
INTERNALERROR> raise IOError("cannot send to %r" %(self,))
INTERNALERROR> IOError: cannot send to <Channel id=1 closed>
=============================================== 3 error in 7.92 seconds ================================================
@khamidou
Copy link
Author

I'm leaving this here in case it helps someone: this was a bug caused by execnet 1.2, the library xdist uses to schedule tests. Reverting to v1.1 solved the bug. Hoped this helped and don't forget to pay it forward!

@TimurNurlygayanov
Copy link

The same issue was with execnet 1.4.1, everything works fine with execnet 1.1.

How to install:

pip install -U execnet==1.1

@LuckyKoala
Copy link

thanks, it worked. Came across this issue while using eth-brownie test.

@tserg
Copy link

tserg commented Feb 28, 2022

I encountered this issue in the context of working with Python and non-Python test files. When using a metafunc to parametrize values in Python tests for a specific test run, this error is thrown due to the presence of non-Python test files.

The workaround is to exclude the non-Python test files by using pytest_ignore_collect.

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