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

Testbook giving timeout error because of nbclient #136

Open spongyMongy opened 2 years ago

spongyMongy commented 2 years ago

I am using testbook for the purpose of testing jupyter notebook as usual. While all seems working fine on local computer , on gitlab pipeline it gives the error below no matter what I tried. Could you kindly advice on it please? As I understand problem is some timeout from nbclient , but I am not able to make any improvement since this is in test class.

self = <testbook.client.TestbookNotebookClient object at 0x7f52650de6a0>
msg_id = 'bf73de8f-fb86eef23c0ff0a3be79c742_12_7'
cell = {'id': 'ff53c670', 'cell_type': 'code', 'metadata': {'execution': {'iopub.status.busy': '2021-10-13T14:44:07.148157Z',...b[0;34m\x1b[0m\x1b[0m\n\x1b[1;32m     88\x1b[0m \x1b[0;34m\x1b[0m\x1b[0m\n', '\x1b[0;31mKeyboardInterrupt\x1b[0m: ']}]}
timeout = 60
task_poll_output_msg = <Task finished name='Task-120' coro=<NotebookClient._async_poll_output_msg() done, defined at /builds/205941/pre-canned-queries/venv/lib/python3.9/site-packages/nbclient/client.py:653> result=None>
task_poll_kernel_alive = <Task cancelled name='Task-119' coro=<NotebookClient._async_poll_kernel_alive() done, defined at /builds/205941/pre-canned-queries/venv/lib/python3.9/site-packages/nbclient/client.py:669>>

    async def _async_poll_for_reply(
            self,
            msg_id: str,
            cell: NotebookNode,
            timeout: t.Optional[int],
            task_poll_output_msg: asyncio.Future,
            task_poll_kernel_alive: asyncio.Future) -> t.Dict:

        assert self.kc is not None
        new_timeout: t.Optional[float] = None
        if timeout is not None:
            deadline = monotonic() + timeout
            new_timeout = float(timeout)
        while True:
            try:
>               msg = await ensure_async(self.kc.shell_channel.get_msg(timeout=new_timeout))

../../venv/lib/python3.9/site-packages/nbclient/client.py:627: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

obj = <coroutine object ZMQSocketChannel.get_msg at 0x7f5263a188c0>

    async def ensure_async(obj: Union[Awaitable, Any]) -> Any:
        """Convert a non-awaitable object to a coroutine if needed,
        and await it if it was not already awaited.
        """
        if inspect.isawaitable(obj):
            try:
>               result = await obj

../../venv/lib/python3.9/site-packages/nbclient/util.py:89: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <jupyter_client.channels.ZMQSocketChannel object at 0x7f5263a48df0>
timeout = 60000.0

    async def get_msg(self, timeout: t.Optional[float] = None) -> t.Dict[str, t.Any]:
        """ Gets a message if there is one that is ready. """
        assert self.socket is not None
        if timeout is not None:
            timeout *= 1000  # seconds to ms
        ready = await self.socket.poll(timeout)

        if ready:
            res = await self._recv()
            return res
        else:
>           raise Empty
E           _queue.Empty

../../venv/lib/python3.9/site-packages/jupyter_client/channels.py:230: Empty

During handling of the above exception, another exception occurred:

    def test_getting_and_comparing_output():
        """Checking (by comparing dictionaries) if expected yaml output\
        from graphql matches jupyter notebook."""
        # pylint: disable=no-value-for-parameter
>       assert comparing_yamls() is None

test_jupyter_notebook.py:86: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../venv/lib/python3.9/site-packages/testbook/testbook.py:63: in wrapper
    func(self.client, *args, **kwargs)
test_jupyter_notebook.py:35: in comparing_yamls
    yaml.dump(yaml.safe_load(getting_output()), file)
../../venv/lib/python3.9/site-packages/testbook/reference.py:85: in __call__
    return self.tb.value(code)
../../venv/lib/python3.9/site-packages/testbook/client.py:273: in value
    result = self.inject(code, pop=True)
../../venv/lib/python3.9/site-packages/testbook/client.py:237: in inject
    cell = TestbookNode(self.execute_cell(inject_idx)) if run else TestbookNode(code_cell)
../../venv/lib/python3.9/site-packages/testbook/client.py:133: in execute_cell
    cell = super().execute_cell(self.nb['cells'][idx], idx, **kwargs)
../../venv/lib/python3.9/site-packages/nbclient/util.py:78: in wrapped
    return just_run(coro(*args, **kwargs))
../../venv/lib/python3.9/site-packages/nbclient/util.py:57: in just_run
    return loop.run_until_complete(coro)
/usr/local/lib/python3.9/asyncio/base_events.py:642: in run_until_complete
    return future.result()
../../venv/lib/python3.9/site-packages/nbclient/client.py:846: in async_execute_cell
    exec_reply = await self.task_poll_for_reply
../../venv/lib/python3.9/site-packages/nbclient/client.py:651: in _async_poll_for_reply
    await self._async_handle_timeout(timeout, cell)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <testbook.client.TestbookNotebookClient object at 0x7f52650de6a0>
timeout = 60
cell = {'id': 'ff53c670', 'cell_type': 'code', 'metadata': {'execution': {'iopub.status.busy': '2021-10-13T14:44:07.148157Z',...b[0;34m\x1b[0m\x1b[0m\n\x1b[1;32m     88\x1b[0m \x1b[0;34m\x1b[0m\x1b[0m\n', '\x1b[0;31mKeyboardInterrupt\x1b[0m: ']}]}

    async def _async_handle_timeout(
            self,
            timeout: int,
            cell: t.Optional[NotebookNode] = None) -> None:

        self.log.error("Timeout waiting for execute reply (%is)." % timeout)
        if self.interrupt_on_timeout:
            self.log.error("Interrupting kernel")
            assert self.km is not None
            await ensure_async(self.km.interrupt_kernel())
        else:
>           raise CellTimeoutError.error_from_timeout_and_cell(
                "Cell execution timed out", timeout, cell
            )
E           nbclient.exceptions.CellTimeoutError: A cell timed out while it was being executed, after 60 seconds.
E           The message was: Cell execution timed out.
E           Here is a preview of the cell contents:
E           -------------------
E           
E           getting_output(*[], **{})
E           
E           -------------------

../../venv/lib/python3.9/site-packages/nbclient/client.py:701: CellTimeoutError
------------------------------ Captured log call -------------------------------
ERROR    traitlets:client.py:695 Timeout waiting for execute reply (60s).
=================== 1 failed, 12 passed in 67.15s (0:01:07) ====================
section_end:1634136309:step_script
section_start:1634136309:cleanup_file_variables
Cleaning up project directory and file based variables
section_end:1634136310:cleanup_file_variables
ERROR: Job failed: exit code 1
 
loichuder commented 2 years ago

Did you try to increase the timeout for your test ?

You can do this in the @testbook decorator: @testbook('/path/to/notebook.ipynb', execute=True, timeout=240)