_PyInterpreterState_DeleteExceptMain() May Cause Problems During Fork

Open
#101,437 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python

Research direction

Start in Python/pystate.c around lines 930-932 and trace PyInterpreterState_DeleteExceptMain() as called by PyOS_AfterFork_Child(). Reproduce or investigate the suspected failure with test__xxsubinterpreters and the test_multiprocessing* tests. Done means fork with multiple interpreters no longer causes the reported crash or intermittent failures, with regression coverage if the behavior is confirmed.

Written by the indexing model from the issue text.

Description

3.12 interpreter-core topic-subinterpreters type-bug

_PyInterpreterState_DeleteExceptMain() first sets the "current" thread state to NULL (while still holding the GIL) but then calls PyInterpreterState_Clear() on any other interpreter, even though it depends on there being a "current" thread set. I would expect this to cause crashes when a process with multiple interpreters if forked, since PyOS_AfterFork_Child() calls _PyInterpreterState_DeleteExceptMain(). This might explain some intermittent CI failures (e.g. with test__xxsubinterpreters or test_multiprocessing_*)

See https://github.com/python/cpython/blob/main/Python/pystate.c#L930-L932.

Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

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.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.