Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflict with unittest : unclosed event loop #1856

Open
patquem opened this issue Sep 6, 2022 · 2 comments
Open

Conflict with unittest : unclosed event loop #1856

patquem opened this issue Sep 6, 2022 · 2 comments

Comments

@patquem
Copy link

patquem commented Sep 6, 2022

Hi,

When executing a nbconvert.ExecutePreprocessor() outside a unittest, all works perfectly.
But when launching* the same function embeded in a unittest class (*to execute as a unittest), the following message appears:

c:\dev\sw\python_3.7.7\lib\asyncio\base_events.py:626: ResourceWarning: unclosed event loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
source=self)
ResourceWarning: Enable tracemalloc to get the object allocation traceback

... with sometimes, a python crash :(

Assertion failed: pfd.revents & POLLIN (C:\projects\libzmq\src\signaler.cpp:265)
(I work on a Windows PC and I have no C:\projects !!!)

python kernel: 3.7.7
virtual env created simply with just >> pip install notebook
Imported nbconvert version: 7.0.0

To reproduce the problems:

import json
import unittest

from nbconvert.preprocessors import ExecutePreprocessor
from nbformat import read, NO_CONVERT

def notebook_convert(lines):
    cell_type = 'code'
    cell = {'cell_type': cell_type,
            'metadata': {},
            'source': lines.splitlines(True)}
    cell.update({'outputs': [], 'execution_count': None})

    return {'cells': [cell],
            'metadata': {},
            'nbformat': 4,
            'nbformat_minor': 4}

def test():
    fname = 'test.ipynb'
    with open(fname, 'w', encoding='utf-8') as fid:
        json.dump(notebook_convert("1/2"), fid)

    exproc = ExecutePreprocessor()

    with open(fname) as fid:
        nbn = read(fid, as_version=NO_CONVERT)
        exproc.preprocess(nbn)

class TestNotebook(unittest.TestCase):
    def test_notebook():
        test()

test()

Any idea to fix this and then, to avoid hundreds of ResourceWarning messages in a big test suite ?
Thanks.

Patrick

NB : note that creating the virtual env just passing by >> pip install nbconvert engenders problem related to kernel-name='python3' during the execution of the previous script - maybe another bug to report ?

@akx
Copy link
Contributor

akx commented Sep 7, 2022

I think this could be a bug in nbclient: jupyter/nbclient#249

A workaround for your case would be to ensure there is an asyncio loop so nbclient's bits and pieces don't need to create one for every .preprocess invocation.

@patquem
Copy link
Author

patquem commented Sep 7, 2022

Thank you @akx for your comment and the issue you opened in jupyter/nbclient.
I don't know how to handle the asyncio loop properly in practice, but my (dirty) idea is presently just to close the BaseEventLoop object (in asyncio/base_events.py) before warnings message management, like so:

    def __del__(self):
        self.close()         # ADDED LINE
        if not self.is_closed():
            warnings.warn(f"unclosed event loop {self!r}", ResourceWarning,
                          source=self)
            if not self.is_running():
                self.close() 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants