nteract / testbook

🧪 📗 Unit test your Jupyter Notebooks the right way
https://testbook.readthedocs.io
BSD 3-Clause "New" or "Revised" License
416 stars 37 forks source link

Asyncio errors and intermittent test failure #140

Open demattia-kobold opened 2 years ago

demattia-kobold commented 2 years ago

I am running into extensive AssertionErrors when running a unit test via pytest with testbook. This seems to be related to intermittent test failures.

I see the AssertionErrors even when executing the notebook with all code commented out. The code I am using is:

import nest_asyncio
from testbook import testbook

# Needed because the notebook uses asyncio. Running it from this test is a nested asyncio call.
nest_asyncio.apply()

def test_hyperparameter_tuning_example():
    with testbook("notebooks/Examples/HyperparameterTuningOptunaDaskMLFlow.ipynb", execute=True) as tb:
        best_params = tb.get("best_params")
        assert len(best_params) == 2
        assert best_params["rf_max_depth"] == 32
        assert best_params["rf_n_estimators"] == 100

I see similar errors when executing the notebook via papermill in the same test.

I am running in a container, which may add to the complication.

The output is of my pytest is:

============================= test session starts ==============================
platform linux -- Python 3.9.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /usr/local/bin/python3
cachedir: .pytest_cache
rootdir: MYROOTDIR, configfile: setup.cfg
plugins: anyio-3.3.4, cov-2.12.1, sugar-0.9.4, mock-3.6.1
collecting ... collected 1 item

tests/test_hyperparameter_tuning_example_notebook.py::test_hyperparameter_tuning_example 
-------------------------------- live log setup --------------------------------
INFO     tests.conftest:conftest.py:22 Setting up Dask client...
INFO     tests.conftest:conftest.py:24 Client is ready: <Client: 'tcp://127.0.0.1:36093' processes=2 threads=8, memory=15.64 GiB>
-------------------------------- live log call ---------------------------------
ERROR    asyncio:base_events.py:1738 Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 189, in _handle_events
    handler_func(fileobj, events)
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 726, in _handle_events
    self.io_loop.update_handler(self.fileno(), self._state)
  File "/usr/local/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 173, in update_handler
    self.writers.remove(fd)
KeyError: 83
ERROR    asyncio:base_events.py:1738 Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 189, in _handle_events
    handler_func(fileobj, events)
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 700, in _handle_events
    self._handle_write()
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 974, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 183, in advance
    assert 0 < size <= self._size
AssertionError
ERROR    asyncio:base_events.py:1738 Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 189, in _handle_events
    handler_func(fileobj, events)
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 700, in _handle_events
    self._handle_write()
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 974, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 183, in advance
    assert 0 < size <= self._size
AssertionError
ERROR    asyncio:base_events.py:1738 Exception in callback BaseAsyncIOLoop._handle_events(89, 4)
handle: <Handle BaseAsyncIOLoop._handle_events(89, 4)>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
RuntimeError: cannot enter context: <Context object at 0x7feb7dd0c600> is already entered
ERROR    asyncio:base_events.py:1738 Exception in callback BaseAsyncIOLoop._handle_events(89, 4)
handle: <Handle BaseAsyncIOLoop._handle_events(89, 4)>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
RuntimeError: cannot enter context: <Context object at 0x7feb7dd0c600> is already entered
ERROR    asyncio:base_events.py:1738 Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 189, in _handle_events
    handler_func(fileobj, events)
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 700, in _handle_events
    self._handle_write()
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 974, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "/usr/local/lib/python3.9/site-packages/tornado/iostream.py", line 183, in advance
    assert 0 < size <= self._size
AssertionError