Visitar URL original
Skip publishing sys.clr_data when BinaryFormatter is unavailable (#2282) by AtwoodTM · Pull Request #2749 · pythonnet/pythonnet · GitHub
Skip to content

Skip publishing sys.clr_data when BinaryFormatter is unavailable (#2282) - #2749

Closed
AtwoodTM wants to merge 2 commits into
pythonnet:masterfrom
AtwoodTM:fix/develop/2282-noop-stash
Closed

AtwoodTM wants to merge 2 commits into
pythonnet:masterfrom
AtwoodTM:fix/develop/2282-noop-stash

Conversation

@AtwoodTM

@AtwoodTM AtwoodTM commented Oct 6, 2026

Copy link
Copy Markdown

What does this implement/fix? Explain your changes.

When BinaryFormatter cannot run, RuntimeData.Stash falls back to NoopFormatter and still published a zero-length sys.clr_data capsule. NoopFormatter.Serialize writes nothing. The next PythonEngine.Initialize saw that capsule and called Deserialize, which throws.

Stash now returns before building or publishing the capsule when the formatter is a NoopFormatter. An existing capsule is cleared so a later Initialize builds a fresh runtime instead of restoring an empty one. Runtimes where BinaryFormatter still works, including .NET Framework, keep the real stash.

Does this close any currently open issues?

#2282

Any other comments?

#2639 already catches the BinaryFormatter failure and returns NoopFormatter, so Shutdown itself no longer throws NotSupportedException. This change stops that fallback from leaving a capsule the next Initialize cannot read.

The new embed test calls Py_Finalize after the assertion, then Initialize. A second Shutdown in the same process deallocates types from the previous runtime and hits MetaType.tp_dealloc in a debug build. GlobalTestsSetup would do that second shutdown.

Local run, with PYTHONNET_PYDLL set:

dotnet test src/embed_tests/Python.EmbeddingTest.csproj -f net10.0

242 passed, 5 skipped, 0 failed. net472 was not run here. This machine has no mono.

Checklist

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Ensure you have signed the .NET Foundation CLA
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

…honnet#2282)

NoopFormatter serializes nothing. An empty capsule makes the next
Initialize call Deserialize and throw. Leave sys.clr_data unset.
A single fix does not need a new credits entry.
@filmor

filmor commented Oct 6, 2026

Copy link
Copy Markdown
Member

As it stands, we can't safely re-Initialize after a shutdown.

@AtwoodTM

AtwoodTM commented Oct 6, 2026

Copy link
Copy Markdown
Author

You're right, thanks for taking a look. I went back and tested master on net8.0, net9.0 and net10.0. Shutdown() works fine there now (looks like #2639 fixed it), and the only thing still failing is calling Initialize() again afterwards, which isn't supported. All this PR did was let the second Initialize() start fresh on top of the old interpreter state, which is worse than the exception. Closing.

Side question: is there anything left on #2282, or can that one be closed too? It still shows up as open when people search for the BinaryFormatter error.

@AtwoodTM AtwoodTM closed this Oct 6, 2026
@AtwoodTM
AtwoodTM deleted the fix/develop/2282-noop-stash branch October 6, 2026 09:38
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