Title: [Bug?] When `Runtime` initialization is interrupted by an exception, the `_isInitialized` flag is not cleared · Issue #2620 · pythonnet/pythonnet · GitHub
Open Graph Title: [Bug?] When `Runtime` initialization is interrupted by an exception, the `_isInitialized` flag is not cleared · Issue #2620 · pythonnet/pythonnet
X Title: [Bug?] When `Runtime` initialization is interrupted by an exception, the `_isInitialized` flag is not cleared · Issue #2620 · pythonnet/pythonnet
Description: 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=n...
Open Graph Description: 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-C...
X Description: 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-C...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/2620
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug?] When `Runtime` initialization is interrupted by an exception, the `_isInitialized` flag is not cleared","articleBody":"When calling `PythonEngine.Initialize`, an exception is occasionally encountered:\n\n```c\nArgumentException: 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\nSystem.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.Collections.Generic.Dictionary`2[TKey,TValue].OnDeserialization (System.Object sender) (at \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.Runtime.Serialization.ObjectManager.RaiseDeserializationEvent () (at \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.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 \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.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 \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.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 \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, System.Boolean fCheck) (at \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at \u003ceae584ce26bc40229c1b1aa476bfa589\u003e:0)\nPython.Runtime.RuntimeData.RestoreRuntimeDataImpl () (at \u003c4d967fa352254671bac5c8512970d45e\u003e:0)\nPython.Runtime.RuntimeData.RestoreRuntimeData () (at \u003c4d967fa352254671bac5c8512970d45e\u003e:0)\nPython.Runtime.Runtime.Initialize (System.Boolean initSigs) (at \u003c4d967fa352254671bac5c8512970d45e\u003e:0)\nPython.Runtime.PythonEngine.Initialize (System.Collections.Generic.IEnumerable`1[T] args, System.Boolean setSysArgv, System.Boolean initSigs) (at \u003c4d967fa352254671bac5c8512970d45e\u003e:0)\nPython.Runtime.PythonEngine.Initialize (System.Boolean setSysArgv, System.Boolean initSigs) (at \u003c4d967fa352254671bac5c8512970d45e\u003e:0)\nPython.Runtime.PythonEngine.Initialize () (at \u003c4d967fa352254671bac5c8512970d45e\u003e:0)\nUnityEditor.Scripting.Python.PythonRunner.DoEnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:677)\nUnityEditor.Scripting.Python.PythonRunner.EnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:312)\nUnityEditor.Scripting.Python.PythonRunner.RunFile (System.String pythonFileToExecute, System.String scopeName) (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:175)\nUnityEditor.Scripting.Python.PythonScriptsWindow.ExecutePythonScript () (at Packages/com.unity.scripting.python/Editor/PythonScriptsWindow.cs:129)\nUnityEngine.UIElements.Clickable.Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:81)\nUnityEngine.UIElements.Clickable.OnMouseUp (UnityEngine.UIElements.MouseUpEvent evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:158)\nUnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallback.cs:64)\nUnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallbackRegistry.cs:332)\nUnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:95)\nUnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:77)\nUnityEngine.UIElements.MouseCaptureDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/Events/MouseCaptureDispatchingStrategy.cs:92)\nUnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:310)\nUnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:275)\nUnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:238)\nUnityEngine.UIElements.EventDispatcher.OpenGate () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:203)\nUnityEngine.UIElements.EventDispatcherGate.Dispose () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:44)\nUnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:301)\nUnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:159)\nUnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/Panel.cs:257)\nUnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:404)\nUnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:194)\nUnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Int32\u0026 ret) (at d:/unityengine/Modules/IMGUI/GUIUtility.cs:198)\n```\n\nBut `Runtime._isInitialized` has already been set to `true` and wasn’t cleared:\n\nhttps://github.com/pythonnet/pythonnet/blob/a21c7972809500edd6364003c37695193ac1a5d4/src/runtime/Runtime.cs#L113\n\nAfter the above exception occurs, if you try to re-initialize the Python Engine, another exception will be encountered:\n\n```c\nInvalidOperationException: This property must be set before runtime is initialized\nPython.Runtime.Runtime.set_PythonDLL (System.String value) (at \u003c4d967fa352254671bac5c8512970d45e\u003e:0)\nUnityEditor.Scripting.Python.PythonRunner.InstallAndLoadPython () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:419)\nUnityEditor.Scripting.Python.PythonRunner.DoEnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:674)\nUnityEditor.Scripting.Python.PythonRunner.EnsureInitialized () (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:312)\nUnityEditor.Scripting.Python.PythonRunner.RunFile (System.String pythonFileToExecute, System.String scopeName) (at Packages/com.unity.scripting.python/Editor/PythonRunner.cs:175)\nUnityEditor.Scripting.Python.PythonScriptsWindow.ExecutePythonScript () (at Packages/com.unity.scripting.python/Editor/PythonScriptsWindow.cs:129)\nUnityEngine.UIElements.Clickable.Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:81)\nUnityEngine.UIElements.Clickable.OnMouseUp (UnityEngine.UIElements.MouseUpEvent evt) (at d:/unityengine/Modules/UIElements/Clickable.cs:158)\nUnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallback.cs:64)\nUnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventCallbackRegistry.cs:332)\nUnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:95)\nUnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at d:/unityengine/Modules/UIElements/Events/EventHandler.cs:77)\nUnityEngine.UIElements.MouseCaptureDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/Events/MouseCaptureDispatchingStrategy.cs:92)\nUnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:310)\nUnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:275)\nUnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:238)\nUnityEngine.UIElements.EventDispatcher.OpenGate () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:203)\nUnityEngine.UIElements.EventDispatcherGate.Dispose () (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:44)\nUnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:301)\nUnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/EventDispatcher.cs:159)\nUnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at d:/unityengine/Modules/UIElements/Panel.cs:257)\nUnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:404)\nUnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at d:/unityengine/Modules/UIElements/UIElementsUtility.bindings.cs:194)\nUnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Int32\u0026 ret) (at d:/unityengine/Modules/IMGUI/GUIUtility.cs:198)\n```\n\nAt this point, it is impossible to clean up the Python environment through `PythonEngine.Shutdown`, and the entire .NET environment can only be restarted.\n","author":{"url":"https://github.com/SolarianZ","@type":"Person","name":"SolarianZ"},"datePublished":"2025-09-23T07:26:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/2620/pythonnet/issues/2620"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:05ae5f12-8f63-ce70-f0f4-15dd19d25215 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CDE2:22F246:3D274B:571BDC:696F801E |
| html-safe-nonce | 6b6798519058aa73bd2a3fcc0a68f236fe5e524c0e06f7aab8c6c883e2fa2506 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDREUyOjIyRjI0NjozRDI3NEI6NTcxQkRDOjY5NkY4MDFFIiwidmlzaXRvcl9pZCI6IjM5NTE0MTAyNzgxOTE1NjI3ODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 45005f089001d0b201998f1722e92a39dc2d06082d5172bc025f4b6971a2e6ee |
| hovercard-subject-tag | issue:3444046788 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/2620/issue_layout |
| twitter:image | https://opengraph.githubassets.com/28db8b1a2ac05ff5f385d1b43d4e9ba4c7f7c8ac5fbdb7a94511a8a0a2d7f2aa/pythonnet/pythonnet/issues/2620 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/28db8b1a2ac05ff5f385d1b43d4e9ba4c7f7c8ac5fbdb7a94511a8a0a2d7f2aa/pythonnet/pythonnet/issues/2620 |
| og:image:alt | 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-C... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SolarianZ |
| hostname | github.com |
| expected-hostname | github.com |
| None | 759c36b32159cc867ba926786bbd5ac69b0804c9cfc86536a67b3567e8bbbb5c |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 08632b85cc9aa97742e56978eb25cc43ca37e51b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width