← Winnitude! Search  ·  Reader Mode (1495 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 8 LINKS

[Bug?] When `Runtime` initialization is interrupted by an exception, the `_isInitialized` flag is not cleared · Issue #2620 · pythonnet/pythonnet

Repository navigation

[Bug?] When Runtime initialization is interrupted by an exception, the _isInitialized flag is not cleared  #2620


New issue

New issue

Open

Open
[Bug?] When Runtime initialization is interrupted by an exception, the _isInitialized flag is not cleared #2620

Description

@SolarianZ's profile SolarianZ opened on Sep 23, 2025

When calling PythonEngine.Initialize, an exception is occasionally encountered:

ArgumentException: An item with the same key has already been added. Key: missing type: GUITable`1+Col[T], Assembly-CSharp-Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].OnDeserialization (System.Object sender) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.Serialization.ObjectManager.RaiseDeserializationEvent () (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize (System.Runtime.Remoting.Messaging.HeaderHandler handler, System.Runtime.Serialization.Formatters.Binary.__BinaryParser serParser, System.Boolean fCheck, System.Boolean isCrossAppDomain, System.Runtime.Remoting.Messaging.IMethodCallMessage methodCallMessage) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, System.Boolean fCheck, System.Boolean isCrossAppDomain, System.Runtime.Remoting.Messaging.IMethodCallMessage methodCallMessage) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, System.Boolean fCheck, System.Runtime.Remoting.Messaging.IMethodCallMessage methodCallMessage) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, System.Boolean fCheck) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Python.Runtime.RuntimeData.RestoreRuntimeDataImpl () (at <4d967fa352254671bac5c8512970d45e>:0)
Python.Runtime.RuntimeData.RestoreRuntimeData () (at <4d967fa352254671bac5c8512970d45e>:0)
Python.Runtime.Runtime.Initialize (System.Boolean initSigs) (at <4d967fa352254671bac5c8512970d45e>:0)
Python.Runtime.PythonEngine.Initialize (System.Collections.Generic.IEnumerable`1[T] args, System.Boolean setSysArgv, System.Boolean initSigs) (at <4d967fa352254671bac5c8512970d45e>:0)
Python.Runtime.PythonEngine.Initialize (System.Boolean setSysArgv, System.Boolean initSigs) (at <4d967fa352254671bac5c8512970d45e>:0)
Python.Runtime.PythonEngine.Initialize () (at <4d967fa352254671bac5c8512970d45e>:0)
UnityEditor.Scripting.Python.PythonRunner.DoEnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:677)
UnityEditor.Scripting.Python.PythonRunner.EnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:312)
UnityEditor.Scripting.Python.PythonRunner.RunFile (System.String pythonFileToExecute, System.String scopeName) (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:175)
UnityEditor.Scripting.Python.PythonScriptsWindow.ExecutePythonScript () (at Packages/com.unity.scripting.python/Editor/PythonScriptsWindow.cs:129)
UnityEngine.UIElements.Clickable.Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:81)
UnityEngine.UIElements.Clickable.OnMouseUp (UnityEngine.UIElements.MouseUpEvent evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:158)
UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallback.cs:64)
UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallbackRegistry.cs:332)
UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:95)
UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:77)
UnityEngine.UIElements.MouseCaptureDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/Events/MouseCaptureDispatchingStrategy.cs:92)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:310)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:275)
UnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:238)
UnityEngine.UIElements.EventDispatcher.OpenGate () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:203)
UnityEngine.UIElements.EventDispatcherGate.Dispose () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:44)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:301)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:159)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/Panel.cs:257)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:404)
UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:194)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Int32& ret) (at d:/unityengine/Modules/IMGUI/GUIUtility.cs:198)

But Runtime._isInitialized has already been set to true and wasn't cleared:

pythonnet/src/runtime/Runtime.cs

Line 113 in a21c797

_isInitialized = true ;

After the above exception occurs, if you try to re-initialize the Python Engine, another exception will be encountered:

InvalidOperationException: This property must be set before runtime is initialized
Python.Runtime.Runtime.set_PythonDLL (System.String value) (at <4d967fa352254671bac5c8512970d45e>:0)
UnityEditor.Scripting.Python.PythonRunner.InstallAndLoadPython () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:419)
UnityEditor.Scripting.Python.PythonRunner.DoEnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:674)
UnityEditor.Scripting.Python.PythonRunner.EnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:312)
UnityEditor.Scripting.Python.PythonRunner.RunFile (System.String pythonFileToExecute, System.String scopeName) (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:175)
UnityEditor.Scripting.Python.PythonScriptsWindow.ExecutePythonScript () (at Packages/com.unity.scripting.python/Editor/PythonScriptsWindow.cs:129)
UnityEngine.UIElements.Clickable.Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:81)
UnityEngine.UIElements.Clickable.OnMouseUp (UnityEngine.UIElements.MouseUpEvent evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:158)
UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallback.cs:64)
UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallbackRegistry.cs:332)
UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:95)
UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:77)
UnityEngine.UIElements.MouseCaptureDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/Events/MouseCaptureDispatchingStrategy.cs:92)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:310)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:275)
UnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:238)
UnityEngine.UIElements.EventDispatcher.OpenGate () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:203)
UnityEngine.UIElements.EventDispatcherGate.Dispose () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:44)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:301)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:159)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/Panel.cs:257)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:404)
UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:194)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Int32& ret) (at d:/unityengine/Modules/IMGUI/GUIUtility.cs:198)

At this point, it is impossible to clean up the Python environment through PythonEngine.Shutdown, and the entire .NET environment can only be restarted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions