Skip to content

gh-149630: Fix pylifecycle multiple init/fini cleanup bugs #149631

Open
Wulian233 wants to merge 1 commit intopython:mainfrom
Wulian233:fix-multiple-init/fini-cleanup-bugs
Open

gh-149630: Fix pylifecycle multiple init/fini cleanup bugs #149631
Wulian233 wants to merge 1 commit intopython:mainfrom
Wulian233:fix-multiple-init/fini-cleanup-bugs

Conversation

@Wulian233
Copy link
Copy Markdown
Contributor

@Wulian233 Wulian233 commented May 10, 2026

Fix multiple resource-management and error-path bugs in the interpreter lifecycle.

Release the builtins module reference on builtin initialization failures and keep it owned until default import initialization succeeds, avoiding an extra NULL assignment. Also DECREF results from non-dict sys.modules clear calls, route subinterpreter initialization failures through cleanup after thread-state detach, release __main__ setup references on PyDict_SetItemString failures, and check stdin/stdout sys object setup failures consistently with stderr.

Tests run:

- ./configure --with-pydebug

- git diff --check

- make -j2 Python/pylifecycle.o

- make -j2 python Programs/_testembed

- make -j2

- Programs/_testembed test_repeated_init_and_subinterpreters

- ./python - <<'PY'
import _interpreters
interp = _interpreters.create()
_interpreters.destroy(interp)
print('ok')
PY

- make patchcheck (fails because this checkout has no upstream remote matching https://github.com/python/cpython)
Copy link
Copy Markdown
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This looks user-facing, so let's add a news entry. You don't have to be specific; just something like "Fix some memory leaks upon interpreter initialization failing."
  2. It's hard to write tests for things like this, but I'd appreciate something small that stresses one of these code paths. (I'm less concerned about the memory leaks in this regard -- I just want to make sure we have something testing the error path.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants