Skip to content

Registered atexit function not called when closing busy IPython Notebook with Ctrl + C #9061

Open

Description

@Py4JQuestionAsker

Simply put, I need to perform some cleanup under the assumption that a user may decide to CTRL + C to shutdown the IPython Notebooks at any time. The following script demonstrates the problem:

import atexit
from subprocess import Popen
def myFunc():
    Popen(["mkdir","dummyDir"]).communicate()
atexit.register(myFunc)
while True:
    pass

If I run this code without the while True block and then CTRL + C twice, I see that the directory dummyDir has been created in the notebook directory, whereas if I run this code with the while True block, the directory is not created. Is there a way that I can guarantee that the cleanup will be performed? I have also tried using try/finally and I encounter the same problem:

try:
    while(True):
        pass
finally:
    myFunc()

The output of import IPython; print(IPython.sys_info()) is

{'commit_hash': u'f534027',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/homes/breese/testroot/share/parameter_server/iPythonEnv/Python/lib/python2.7/site-packages/IPython',
 'ipython_version': '4.0.0',
 'os_name': 'posix',
 'platform': 'Linux-2.6.32-431.23.3.el6.YAHOO.20140804.x86_64-x86_64-with-redhat-6.5-Santiago',
 'sys_executable': '/homes/breese/testroot/share/parameter_server/iPythonEnv/Python/bin/python',
 'sys_platform': 'linux2',
 'sys_version': '2.7.10 (default, Nov 20 2015, 23:24:22) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]'}

Activity

  1. takluyver commented on Dec 1, 2015

    @takluyver
    Member

    It may be a race condition - when the notebook server shuts down kernels, it gives them a short time to shut down gracefully before it forcibly terminates them. Though I would have thought it would be enough time for that to run.

    In general, however, there's no way to reliably run code just before a process exits - if it segfaults, or receives an unexpected SIGKILL, you have no chance to run code at all.

  2. minrk commented on Dec 2, 2015

    @minrk
    Member

    @Py4JQuestionAsker can you create a situation where this reliably fails? How many kernels do you typically have? Does this fail with only one kernel that is not busy? I could imagine having many and/or busy kernels could increase the likelihood that kernels don't exit cleanly before the notebook gives up and kills them.

  3. Py4JQuestionAsker commented on Dec 2, 2015

    @Py4JQuestionAsker
    Author

    Since I had thought that it was just a problem with atexit and IPython notebook I didn't mention that the notebook is running on Spark, which appears to have its own cleanup routines. In this scenario, the situation I described will always fail even if only 1 kernel is running and busy (though not if it is not busy), which I now see is probably the result of the extra cleanup introduced by Spark.

  4. added this to the not ipython milestone on Jan 18, 2016
  5. Carpique commented on May 10, 2016

    @Carpique

    I'm running jupyter 3.2.1 (simple, no Spark) and I can definitely verify that registering a shutdown hook with python's atexit() works well when the notebook is not busy and the hook is not invoked when the notebook is busy.

    Although my use case involves performing some cleanup precisely when notebooks are busy (monitoring execution of a sub-process), I tried also a simple scenario of registering the hook with and without an infinite while loop and I can confirm there's a difference in behavior.

  6. Pierre-Bartet commented on Mar 1, 2019

    @Pierre-Bartet

    I can confirm, even a simpler example with a single TemporaryDirectory fails cleaning when stopping kernel or restarting it. The problem only arise when kernel is busy when stopped.

    By the way it is not only an atexit problem, as TemporaryDirectory rely only on weakref. All cleaning (using with, weakref.finalize, del and atexit) consistently fail in the same manner.

    For example, despite using with this example doesn't clean if stopped during the sleep :

    import time
    import tempfile
    
    with tempfile.TemporaryDirectory() as tmp:    
        time.sleep(10)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions