Skip to content

Record successful attach only after attaching in SuspendAttach - #6404

Merged
davidhewitt merged 4 commits into
PyO3:mainfrom
ngoldbaum:fix-shutdown-race
Sep 12, 2026
Merged

Record successful attach only after attaching in SuspendAttach#6404
davidhewitt merged 4 commits into
PyO3:mainfrom
ngoldbaum:fix-shutdown-race

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

SuspendAttach::drop increments ATTACH_COUNT before successfully attaching. This was missed during review of #6085 (ping @anuraaga). The fix is to only increment if we actually successfully attach.

This bug can lead to crashes if the attach happens during interpreter shutdown on pythons older than 3.13.8.

I hit this while working on Quansight/spinningjenny#14. See the workaround there for how I'm avoiding the problem. spinningjenny stores state in Rust TLS, and that pattern does not look like any existing tests in PyO3.

I updated the testing utilities we use in test_finalization.py to trigger this. It unfortunately requires a decent amount of complexity to ensure the race is triggered deterministically.

Adding the soundness label because I think the current behavior is unsound.

@codspeed-hq

codspeed-hq Bot commented Sep 11, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 12.79%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 3 regressed benchmarks
✅ 138 untouched benchmarks
⏩ 6 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
into_u128_small 876.1 ns 1,041.4 ns -15.87%
into_u128_zero 816 ns 925.7 ns -11.85%
extract_bigint_small 1.4 µs 1.6 µs -10.55%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ngoldbaum:fix-shutdown-race (63cb82b) with main (fc4d08c)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Comment thread pytests/src/misc.rs
}

/// Wrapper to mark Receiver as Sync.
struct SyncReceiver<T>(std::sync::mpsc::Receiver<T>);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not needed since Rust 1.73

@ngoldbaum

Copy link
Copy Markdown
Contributor Author

ping for a review here @davidhewitt

@davidhewitt davidhewitt 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.

Yikes, just when I thought we'd finally closed all these finalization bugs!

I think given we're hopefully about to ship 0.30 I'm not rushing to back port this, WDYT?

@davidhewitt
davidhewitt added this pull request to the merge queue Sep 12, 2026
@ngoldbaum

Copy link
Copy Markdown
Contributor Author

Yeah that's fine from my end.

Merged via the queue into PyO3:main with commit 7ed9ac0 Sep 12, 2026
50 of 53 checks passed
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