Skip to content

Kernels that start at the same time can die before replying to kernel_info #232

Open

Description

@julian-belina

When we run our example notebooks in CI with pytest -n 2 --dist loadscope --nbval-lax --nbval-current-env, a notebook now and then fails before its first cell runs:

RuntimeError: Kernel died before replying to kernel_info

Every cell of that notebook then errors, and a rerun passes.

nbval sends the kernel's stderr to os.devnull, so I started kernels the way nbval does, but with their stderr kept, on one of our CI machines (Ubuntu 24.04, Python 3.10, ipykernel 6.31.0, IPython 8.37.0, jupyter_client 8.10.0). In 40 rounds, six kernels started at the same moment in a fresh home directory, on two CPU cores. Two kernels died, for two different reasons.

1. IPython's history database. Each IPython kernel creates and opens ~/.ipython/profile_default/history.sqlite when it starts. When several kernels do this at once, some of them get database is locked. IPython takes that for a corrupt database and renames the file, and a kernel dies when another kernel has renamed it first:

[IPKernelApp] ERROR | Failed to open SQLite history .../profile_default/history.sqlite (database is locked).
...
  File "IPython/core/history.py", line 105, in catch_corrupt_db
    self.hist_file.rename(newpath)
FileNotFoundError: [Errno 2] No such file or directory: '.../profile_default/history.sqlite' -> '.../profile_default/history-corrupt-2026-11-03T01.54.45.926961.sqlite'

Kernels that collide like this and survive took up to 33 s to start instead of 2–4 s.

This fits the failures in our CI. The jobs run in fresh containers, and in all six failing jobs we found (three repositories, two or four xdist workers), the dead kernel was one of the first kernels of the session, which all workers start at the same moment. No kernel died later in a session, when the file already exists.

nbclient avoids this. It starts IPython kernels with --HistoryManager.hist_file=:memory: by default (NotebookClient.ipython_hist_file, introduced in jupyter/nbconvert#1017), because "when running kernels simultaneously (e.g. via multiprocessing) saving history a single SQLite file can result in database errors". nbval runs cells with store_history=False, so the file holds nothing from the tests anyway.

2. Ports. The other kernel died binding one of the ports that jupyter_client had chosen for it:

  File "ipykernel/kernelapp.py", line 352, in init_control
    self.control_port = self._bind_socket(self.control_socket, self.control_port)
...
zmq.error.ZMQError: Address already in use (addr='tcp://127.0.0.1:53805')

jupyter_client picks the kernel's five TCP ports by binding to port 0 and closing the socket again, and the kernel binds them only after it has started up. Another process can take one of the ports in between, and ipykernel tries a given port only once. jupyter_client avoids duplicate ports only within one process (LocalPortCache, see jupyter/jupyter_client#487). Its KernelRestarter handles this case by starting the kernel again on new ports until it is alive (random_ports_until_alive).

Proposal. #233 does two things:

  • It starts IPython kernels with --HistoryManager.hist_file=:memory:, as nbclient does. This prevents the first failure.
  • It adds --nbval-kernel-startup-retries=N, with a default of 0, which starts a new kernel with new ports when a kernel dies or does not reply during startup. This handles the second failure, which nbval cannot prevent.

Activity

  1. changed the title [-]Option to start a new kernel when a kernel fails to start[/-] [+]Kernels that start at the same time can die before replying to kernel_info[/+] on Nov 3, 2026
  2. julian-belina commented on Nov 3, 2026

    @julian-belina
    Author

    I've rewritten the description above. With the kernels' stderr kept, I caught two kernels dying at startup: one on IPython's history.sqlite and one on a port. The failures in our CI fit the first. #233 now also starts IPython kernels with --HistoryManager.hist_file=:memory:, as nbclient does, in addition to the retry option.

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions