◐ Shell
reader mode source ↗
Skip to content

fix finalizing and atexit timing#6626

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:atexit
Jan 17, 2026
Merged

fix finalizing and atexit timing#6626
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:atexit

Conversation

@youknowone

@youknowone youknowone commented Jan 2, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Improved interpreter shutdown handling and suppressed unraisable hook invocations during VM finalization to ensure cleaner interpreter shutdown.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The interpreter finalization sequence is restructured to dynamically import the threading module and call threading._shutdown() during shutdown. Additionally, unraisable exception handling is suppressed when the VM is finalizing by checking the finalizing flag.

Changes

Cohort / File(s) Summary
Finalization Sequencing
crates/vm/src/vm/interpreter.rs
Replaced direct non-daemon thread wait with conditional dynamic threading module import and threading._shutdown() invocation. Updated finalization flag timing and documentation to reflect Py_FinalizeEx semantics.
Exception Handling During Shutdown
crates/vm/src/vm/mod.rs
Added early-return guard in run_unraisable() to suppress unraisable hook handling when VM is finalizing.

Sequence Diagram

sequenceDiagram
    participant VM as Virtual Machine
    participant Threading as Threading Module
    participant Atexit as Atexit System

    VM->>Threading: Attempt dynamic import
    alt Import Successful
        VM->>Threading: Call threading._shutdown()
        Threading-->>VM: Shutdown complete
    else Import Failed
        VM-->>VM: Continue
    end
    VM->>VM: Set finalizing flag = true
    VM->>Atexit: Run atexit functions
    Atexit-->>VM: Cleanup complete
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly Related PRs

Suggested Reviewers

  • fanninpm

🐰 The VM winds down with grace and care,
Threading module bids farewell in the air,
Atexit functions tidy up the floor,
Finalization flags mark the door,
A shutdown sequence, crisp and fair!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix atexit' directly relates to the main changes in the PR, which involve fixing atexit and finalization timing issues in the interpreter shutdown sequence.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@fanninpm

fanninpm commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

You may want to take a look in test_bdb for 31 tests that can be un-skipped.

@youknowone

Copy link
Copy Markdown
Member Author

let me check this again when #6718 is done

@youknowone

Copy link
Copy Markdown
Member Author

@fanninpm they are downgraded to expectedFailure (@terryluan12 thanks!)
And this patch doesn't seem to be enough for that

@youknowone youknowone marked this pull request as ready for review January 17, 2026 12:25

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hide comment

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@crates/vm/src/vm/mod.rs`:
- Around line 552-562: The finalizing flag is set before calling
atexit::_run_exitfuncs, causing run_unraisable to early-return and suppress
atexit handler exceptions; either move the store to state.finalizing to after
atexit::_run_exitfuncs(vm) so finalization suppression happens post-atexit, or
add a small atexit bypass: introduce an atexit-running marker on VM state (e.g.,
vm.state.atexit_running) set around atexit::_run_exitfuncs and change
run_unraisable to allow reporting when that marker is true (while still
suppressing in other finalization contexts). Ensure references are to
run_unraisable, atexit::_run_exitfuncs, and state.finalizing (or the new
state.atexit_running) so reviewers can find and apply the change.

@youknowone youknowone marked this pull request as draft January 17, 2026 14:31
@youknowone youknowone marked this pull request as ready for review January 17, 2026 16:25
Hide details View details @youknowone youknowone merged commit 11c9b0e into RustPython:main Jan 17, 2026
13 checks passed
@youknowone youknowone deleted the atexit branch January 17, 2026 16:54
@youknowone youknowone changed the title fix atexit Jan 17, 2026
@fanninpm

Copy link
Copy Markdown
Contributor

they are downgraded to expectedFailure

That's better than nothing!

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.

2 participants