Skip to content

Fix Python.NET GIL shutdown finalizer crash - #9811

Open
jassim1977 wants to merge 1 commit into
QuantConnect:masterfrom
jassim1977:fix/v1-pythonnet-gil-shutdown
Open

jassim1977 wants to merge 1 commit into
QuantConnect:masterfrom
jassim1977:fix/v1-pythonnet-gil-shutdown

Conversation

@jassim1977

Copy link
Copy Markdown

Description

Removes an undisposed Py.GIL() acquisition immediately before PythonEngine.Shutdown().

With Python.Runtime 2.0.66 and Python 3.11.16 on Windows, the retained GIL state reaches finalization after engine shutdown and raises:

System.InvalidOperationException: GIL must always be released, and it must be released from the same thread that acquired it.

Reproduction before fix

  • Python algorithm initializes and completes its backtest.
  • Shutdown reaches PythonInitializer.Shutdown().
  • Process terminates from Py.GILState.Finalize().
  • Exit code: -532462766.

Validation after fix

  • Full LEAN Release build: 0 errors.
  • Five Python algorithm scenarios completed with exit code 0.
  • Five of five runs reached PythonInitializer.Shutdown(): ended.
  • Repeated allow scenario completed successfully across five runs.
  • No GIL must always be released failure occurred.
  • Allow scenario submitted and filled exactly one order.
  • Four rejection scenarios submitted no orders.

This change is intentionally limited to one line in PythonInitializer.Shutdown().

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.

1 participant