Skip to content

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

Closed
Wulian233 wants to merge 1 commit into
python:mainfrom
Wulian233:fix-multiple-init/fini-cleanup-bugs
Closed

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

Conversation

@Wulian233

@Wulian233 Wulian233 commented May 10, 2026

Copy link
Copy Markdown
Contributor

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)

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

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.)

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 10, 2026
@Wulian233 Wulian233 closed this Aug 10, 2026
@Wulian233
Wulian233 deleted the fix-multiple-init/fini-cleanup-bugs branch August 10, 2026 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants