jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.58k stars 4.85k forks source link

False empty notebook list #4057

Open mhooreman opened 5 years ago

mhooreman commented 5 years ago

Hello,

I have a "keepalive" script for jupyter notebook, which uses jupyter notebook list --jsonlist to search for running notebooks.

Sometimes, my notebook is running, but the returned list if empty. I was unable to find a scenario to reproduce it, but it's happening quite often.

Environment information

Command line to start jupyter

        cmd = (
            f"nohup jupyter-notebook --notebook-dir {self._directory} "
            f"--port {self._port} --ip '{_NOTEBOOK_IP_ADDRESS}' "
            f"--no-browser "
            f">>{self._logFile} 2>&1 &"
        )

Workaround The following code, which search for the "good notebook" PID, seems to work correctly.

    def _getPid(self):
        """Returns the PID of the notebook running with the instance
        parameters, or None if not running"""
        # pylint: disable=too-many-nested-blocks
        processName = 'jupyter-notebook'
        ret = None
        for p in psutil.process_iter():
            if p.username() == self._userName:
                if p.name() == processName[:len(p.name())]:
                    for c in p.connections():
                        if c.laddr.port == self._port:
                            if c.status == 'LISTEN':
                                if re.split(
                                    '--notebook-dir ',
                                    ' '.join(p.cmdline())
                                )[1].split(' ')[0] == self._directory:
                                    if ret is None:
                                        ret = p.pid
                                    else:
                                        raise ValueError("Found two processes")
        return ret
takluyver commented 5 years ago

When the notebook server starts, it writes a JSON file recording its details, which it will clean up when it shuts down. jupyter notebook list uses those. Unfortunately the runtime directory we use can be cleaned up after several hours; there's meant to be a way to prevent this, but I'm not sure if we're using it, and it's almost impossible to test.

I think I'll make a PR to just avoid using XDG_RUNTIME_DIR.

takluyver commented 5 years ago

https://github.com/jupyter/jupyter_core/pull/143