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

Exception raised in KernelClient.del when shutting down kernel after executing code using kc.execute_interactive() #1026

Open
vivekvedant opened this issue May 30, 2024 · 3 comments

Comments

@vivekvedant
Copy link

vivekvedant commented May 30, 2024

Description
When executing the provided code snippet to start a new kernel and execute a Python statement using kc.execute_interactive, the code executes successfully, but it raises an exception related to KernelClient.del upon exiting.

Steps to Reproduce
Run the following Python code:

from jupyter_client.manager import KernelManager, start_new_kernel
from IPython.utils.capture import capture_output

km, kc = start_new_kernel(kernel_name="python3")

TIMEOUT = 30
with capture_output() as io:
    reply = kc.execute_interactive("print('hello world')", timeout=TIMEOUT)
km.shutdown_kernel()

print(io.stdout)
print(io.stderr)

Expected Behavior
The code should execute the provided Python statement (print('hello world')) in the newly started kernel and print "hello world" to the standard output. After execution, it should cleanly exit without raising any exceptions.

Actual Behavior
The code executes successfully and prints "hello world" as expected, but upon exiting, it raises the following exception:

hello world

Exception ignored in: <function KernelClient.__del__ at 0x7f706835dea0>
Traceback (most recent call last):
  File "/home/vivek/zeza/AIDEN_experiment/sandbox/jupyter_client/jupyter_client/client.py", line 121, in __del__
  File "/home/vivek/miniconda3/envs/test/lib/python3.10/site-packages/traitlets/traitlets.py", line 687, in __get__
  File "/home/vivek/miniconda3/envs/test/lib/python3.10/site-packages/traitlets/traitlets.py", line 666, in get
TypeError: 'NoneType' object is not callable

Environment
Python version: 3.10.14
Operating System: Ubuntu 22.04
jupyter_client version: 8.6.2

@mwohlf
Copy link

mwohlf commented Jul 5, 2024

same issue here

@trentmaetzold
Copy link

Getting this while running km, kc = jupyter_client.manager.start_new_kernel().

@Deepmie
Copy link

Deepmie commented Jan 11, 2025

我发现这似乎是一个无害的报错,我直接将__del__的功能去掉后,目前还未发现任何不妥。

  def __del__(self) -> None:
      """Handle garbage collection.  Destroy context if applicable."""
      return
      # print("context: ", self.context)
      if (
          self._created_context
          and self.context is not None  # type:ignore[redundant-expr]
          and not self.context.closed
      ):
          if self.channels_running:
              if self.log:
                  self.log.warning("Could not destroy zmq context for %s", self)
          else:
              if self.log:
                  self.log.debug("Destroying zmq context for %s", self)
              self.context.destroy()
      try:
          super_del = super().__del__  # type:ignore[misc]
      except AttributeError:
          pass
      else:
          super_del()

当我添加了注释# print("context:", self.context)直接就报错了,事实上它会认为self是NoneType所以才会报错,有可能在调用__del__方法之前该类就被垃圾回收机制回收了,具体原因我并没有进行深究,但是注释掉它在我这里是可行的。

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

4 participants