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

Prevent exception spam during interpreter teardown #24

Merged
merged 2 commits into from
Jan 2, 2024

Conversation

apmorton
Copy link
Contributor

Minimal reproducer:

import sys

from ipykernel.comm import Comm

# simply construct a Comm object
Comm(show_warning=False)

# someone somewhere in some other module somehow takes a copy of the modules dict
MODULES_COPY = dict(sys.modules)

Slightly more real-world example (how we actually got here):

import sys

# someone somewhere is using dash
import dash

# someone somewhere in some other module somehow takes a copy of the modules dict
MODULES_COPY = dict(sys.modules)

These will both output:

Exception ignored in: <function BaseComm.__del__ at 0x7f6608e32480>
Traceback (most recent call last):
  File "comm/base_comm.py", line 68, in __del__
  File "comm/base_comm.py", line 104, in close
  File "ipykernel/comm/comm.py", line 26, in publish_msg
AttributeError: 'NoneType' object has no attribute 'initialized'

The relevant line is:

if not Kernel.initialized():
    return

where Kernel is:

from ipykernel.kernelbase import Kernel

Due to a series of unfortunate events, the __del__ for Comm is being called after the partial teardown of the ipykernel.kernelbase module, as can be seen from the output of python -v:

# cleanup[3] wiping ipykernel.kernelbase
...
# cleanup[3] wiping comm
Exception ignored in: <function BaseComm.__del__ at 0x7f6608e32480>
Traceback (most recent call last):
  File "comm/base_comm.py", line 68, in __del__
  File "comm/base_comm.py", line 104, in close
  File "ipykernel/comm/comm.py", line 26, in publish_msg
AttributeError: 'NoneType' object has no attribute 'initialized'

The above testing was done in a conda environment with:

comm-0.1.4-pyhd8ed1ab_0
dash-2.14.2-pyhd8ed1ab_0
ipykernel-6.26.0-pyhf8b6a83_0
python-3.11.6-hab00c5b_0_cpython

@apmorton apmorton marked this pull request as ready for review December 30, 2023 05:24
Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@blink1073 blink1073 enabled auto-merge (squash) January 2, 2024 13:39
@blink1073 blink1073 merged commit 2aad950 into ipython:main Jan 2, 2024
18 checks passed
@apmorton apmorton deleted the am/fix-exception-during-teardown branch January 8, 2024 16:49
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

Successfully merging this pull request may close these issues.

None yet

2 participants