Skip to content

Keep IPython history in memory and add --nbval-kernel-startup-retries - #233

Open
julian-belina wants to merge 3 commits into
computationalmodelling:masterfrom
julian-belina:kernel-startup-retries
Open

julian-belina wants to merge 3 commits into
computationalmodelling:masterfrom
julian-belina:kernel-startup-retries

Conversation

@julian-belina

@julian-belina julian-belina commented Nov 2, 2026 •

Copy link
Copy Markdown

Closes #232.

This PR builds on #234 and includes its commit, so that CI passes here. Merging #234 first keeps the diff of this PR to its own two commits.

Kernels that nbval starts at the same time, e.g. under pytest-xdist, can die before they reply to kernel_info, and every cell of the notebook then errors. #232 has tracebacks for the two causes. This PR addresses both.

Keep the history of IPython kernels in memory. nbval now starts IPython kernels with --HistoryManager.hist_file=:memory:, as nbclient does by default. Otherwise every kernel creates and opens the profile's history.sqlite. IPython takes a database that another kernel has locked for a corrupt one and renames it, and a kernel dies when another kernel has renamed the file first. nbval runs cells with store_history=False, so the file never held anything from the tests. The argument is passed for the kernel names that jupyter_client treats as IPython (KernelManager.ipykernel, which nbclient uses too) and for :nbval-parent-env.

--nbval-kernel-startup-retries=N. If a kernel dies or does not reply within --nbval-kernel-startup-timeout, nbval shuts it down and starts a new one, up to N times, and raises the last error if all of them fail. Each new KernelManager picks new ports, which gets around a port that another process took before the kernel could bind it. The default is 0, so nothing changes unless the option is set.

Tests (tests/test_kernel_startup.py):

  • With mocked kernel managers, a kernel that dies is shut down and replaced, and the error is raised when all kernels die.
  • A notebook run through pytester checks that the kernel's hist_file is :memory:.

I ran the test suite as dodo.py test does on Windows with Python 3.12, pytest 9.2, jupyter_client 8.10 and ipykernel 7.4. It gives the same results as master, apart from the 3 new tests, which pass. Four tests fail on master there too: test_nbdime, which #234 fixes, and three LaTeX and stream samples, which pass on AppVeyor.

The two changes are independent, so I can split them into two PRs if you prefer.

🤖 Generated with Claude Code

@julian-belina julian-belina changed the title Add --nbval-kernel-startup-retries Keep IPython history in memory and add --nbval-kernel-startup-retries Nov 3, 2026
julian-belina and others added 3 commits November 3, 2026 02:48
pytest 9 has a built-in pytest_report_teststatus implementation (in
_pytest/subtests.py) that takes config. NbdimeReporter calls the hook
without it, so pluggy raises

    HookCallError: hook call must provide argument 'config'

and a run with --nbdime ends with an internal error. This is why
test_nbdime fails with pytest 9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nbval starts each notebook's kernel once. If that kernel dies or does
not reply within the startup timeout, every cell of the notebook
errors. With --nbval-kernel-startup-retries=N, nbval shuts the failed
kernel down and starts a new one, up to N times, and raises the last
error if all of them fail. Each new kernel gets new ports, so a port
that another process took in the meantime does not stop it. The
default of 0 keeps the current behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kernels that start at the same time, e.g. the first kernels of a
pytest-xdist session in a fresh home directory, all create and open the
profile's history.sqlite. IPython takes the locked database for a
corrupt one and renames it, and a kernel dies when another kernel has
renamed the file first:

    File "IPython/core/history.py", line 105, in catch_corrupt_db
      self.hist_file.rename(newpath)
    FileNotFoundError: [Errno 2] No such file or directory

Start IPython kernels with --HistoryManager.hist_file=:memory:, as
nbclient does by default. nbval runs cells with store_history=False, so
the file never held anything from the tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

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

1 participant