quarto-dev / quarto-dev/quarto-cli

quarto render failing to close jupyter kernel in `pystan2` code?

Open
#3,466 14 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

bug engines-jupyter
Dominant language
JavaScript
Stars
6k
Forks
458
Avg merge
1d 9h
Merged PRs (30d)
41

Description

Bug description

When trying to render a quarto notebook with Python code from the command line I'm seeing behavior consistent with a persistent jupyter kernel. I would expect that each call to quarto render would run in a new, clean kernel.

For example consider the file test.qmd with the text

---
title: "Test"
author: "Test"
jupyter: python3
---

Test test test.

```{python}
import multiprocessing
multiprocessing.set_start_method("fork")
```

If I run

quarto render test.qmd --to html

from the command line once then everything renders fine, but I do see some Python threads that persist even after the command line prompt has returned.

If I run the same command again then quarto fails with the error

Executing 'test.ipynb'
  Cell 1/1...ERROR: 

An error occurred while executing the following cell:
------------------
import multiprocessing
multiprocessing.set_start_method("fork")
------------------

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [3], in <cell line: 2>()
      1 import multiprocessing
----> 2 multiprocessing.set_start_method("fork")

File /usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py:243, in DefaultContext.set_start_method(self, method, force)
    241 def set_start_method(self, method, force=False):
    242     if self._actual_context is not None and not force:
--> 243         raise RuntimeError('context has already been set')
    244     if method is None and force:
    245         self._actual_context = None

RuntimeError: context has already been set
RuntimeError: context has already been set

Force quitting those dangling Python threads does allow me to render again without problem.

If I include a cell like

```{python}
print(locals())
a = 2
print(locals())
```

then I also see the locals() dictionary growing from call to call, even holding variables from previous renders that are no longer in the notebook, consistent with the kernel persisting from call to call.

Operating System: MacOS 10.15.7 (19H2026)
Python Version: Python 3.9.6 (default, Jun 29 2021, 06:20:32) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Quarto Version: 1.1.251

Any help would be appreciated, and apologies if I'm missing something obvious. Many thanks for a great project.

Checklist
  • Please include a minimal, fully reproducible example in a single .qmd file? Please provide the whole file rather than the snippet you believe is causing the issue.
  • Please format your issue so it is easier for us to read the bug report.
  • Please document the RStudio IDE version you're running (if applicable), by providing the value displayed in the "About RStudio" main menu dialog?
  • Please document the operating system you're running. If on Linux, please provide the specific distribution.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.