Skip to content

Make cwd thread-safe by passing it to the kernel instead of os.chdir (#473) - #909

Open
codechrl wants to merge 1 commit into
nteract:mainfrom
codechrl:fix-branch
Open

codechrl wants to merge 1 commit into
nteract:mainfrom
codechrl:fix-branch

Conversation

@codechrl

Copy link
Copy Markdown

Bug

execute_notebook(cwd=...) sets the working directory with utils.chdir, a process-global os.chdir context manager wrapping the whole engine run. Concurrent executions with different cwd race on the single process working directory, so a kernel can launch in another run's directory. See #473.

Fix

Drop the global chdir in execute.py and thread cwd through execute_notebook_with_engine to PapermillNotebookClient, which passes it to nbclient as resources={'metadata': {'path': cwd}}. The kernel starts in cwd without mutating the papermill process. This is the approach endorsed in the #473 thread. The public execute_notebook(cwd=...) signature is unchanged.

Verification

Added test_concurrent_cwd_isolation: two concurrent runs with different cwd, held at notebook_start with a threading.Barrier to force the interleave. Fails on main (kernels launch in the wrong dir), passes with this change. Existing TestCWD and the test_engines/test_clientwrap/test_utils suites pass. ruff check and ruff format clean.

execute_notebook(cwd=...) set the working directory via a process-global
os.chdir context manager wrapping the whole engine run, so concurrent
executions with different cwd raced on the single process working directory
and a kernel could launch in the wrong directory.

Thread cwd through execute_notebook_with_engine to PapermillNotebookClient,
which passes it to nbclient as resources={'metadata': {'path': cwd}} so the
kernel starts in cwd without mutating the papermill process. The public
execute_notebook(cwd=...) signature is unchanged. Fixes nteract#473.
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.

1 participant