- Notifications
You must be signed in to change notification settings - Fork768
Add soft shutdown#958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
d1928dcf000e08f882400b7715ee41ac665c6dae9e4e19a4f657452e91f64b9b07b8447db724e2a88be42940973d108913cc9e7e51cb8e8c653a263b3e889b04d6dfb5150e6165e209ecad95da0dee5da00a0b3277da6df2039e69fe5050d593fb001ce83fcfba616abdc0f7249d98e8433d0f69b4864b1b466df49130c476ba5101ff21ac0b01378631bb43bf3d9f8da97502e8b316080d4fa09874cd18da561b9499c64431d6442b843945ade0698dabed7f4bb77a670bd743cb56f13c9a83c992c469924b217df84e298b5162197c8c2a39f47c8aa63f0b5b2f3d435cbe55f23cae6183f9d89d57a8208fad26bcfdcc766ab7191428af38e3c028b387e9ef85999ecb65af3498fc8ccc2219e8c8d66e4f0016539e20e3da7c15002b1adaa8840b2dec7a74e877b336d738bfff5edc35ac75ba65cb22e73865d41a75f519b6d140d9d55629b62a614f0420e4ab9f1c32bcb3a0077ea838ea0b606a656e09f82817ec9a6c802a43a0fdf9693a8c72db52bc01bfbf2c3883c4ce7e0d56d82034dc4ba50a71ecdce8b35f441ce8ee904c4bcb0f575bd3d1799aad2408b9fd2b6622b7bcac0af3504ba1df6e639ba1f7e5ab528075f48eb8cb8a308f0f21ae0bfe07aefe65387b05b20367419d137925a40641272b898c133e3d9b21a5c8dee53598cb7783e8dd56db3181e38a363b409a89f5c24b00d6c645fa89b4880a764412c02066a3cfc898da1fad7d44e86aa75c52343f89cc6b8e4d5fcfa4fa47957ff956e4c7b134c97e61a50b9d2c1178cbc83a17f3632034578d00e4cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
* Refactor Save/Load* Fix refcnt error for moduleobject.cs
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -233,26 +233,33 @@ public static void RunPython() | ||
| mode = ShutdownMode.Soft; | ||
| } | ||
| PythonEngine.Initialize(mode: mode); | ||
| try | ||
| { | ||
| using (Py.GIL()) | ||
| { | ||
| try | ||
| { | ||
| var pyScript = string.Format("import clr\n" | ||
| + "print('[{0} in python] imported clr')\n" | ||
| + "clr.AddReference('System')\n" | ||
| + "print('[{0} in python] allocated a clr object')\n" | ||
| + "import gc\n" | ||
| + "gc.collect()\n" | ||
| + "print('[{0} in python] collected garbage')\n", | ||
| name); | ||
| PythonEngine.Exec(pyScript); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine(string.Format("[{0} in .NET] Caught exception: {1}", name, e)); | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| PythonEngine.BeginAllowThreads(); | ||
| ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -56,12 +56,14 @@ internal static void RemoveClasses() | ||
| cls.CallTypeTraverse(OnVisit, visitedPtr); | ||
| // XXX: Force release instance resources but not dealloc itself. | ||
| cls.CallTypeClear(); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. NIT: Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Should this implement MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||
| cls.DecrRefCount(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| visitedHandle.Free(); | ||
| } | ||
| cache.Clear(); | ||
| } | ||
| private static int OnVisit(IntPtr ob, IntPtr arg) | ||
| ||
| @@ -81,15 +83,25 @@ private static int OnVisit(IntPtr ob, IntPtr arg) | ||
| return 0; | ||
| } | ||
| internal static void StashPush(RuntimeDataStorage storage) | ||
| ||
| { | ||
| storage.PushValue(cache); | ||
| foreach (var cls in cache.Values) | ||
| { | ||
| // This incref is for cache to hold the cls, | ||
| // thus no need for decreasing it at StashPop. | ||
| Runtime.XIncref(cls.pyHandle); | ||
| cls.Save(); | ||
| } | ||
| } | ||
| internal static void StashPop(RuntimeDataStorage storage) | ||
| { | ||
| cache = storage.PopValue<Dictionary<Type, ClassBase>>(); | ||
| foreach (var cls in cache.Values) | ||
| { | ||
| cls.Load(); | ||
| } | ||
| } | ||
| /// <summary> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -128,11 +128,5 @@ public static int tp_clear(IntPtr ob) | ||
| Runtime.Py_CLEAR(ref self.target); | ||
| return 0; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,7 +6,7 @@ namespace Python.Runtime | ||
| /// <summary> | ||
| /// Implements the "import hook" used to integrate Python with the CLR. | ||
| /// </summary> | ||
| internalstaticclass ImportHook | ||
| { | ||
| private static IntPtr py_import; | ||
| private static CLRModule root; | ||
| @@ -130,6 +130,22 @@ internal static void Shutdown() | ||
| CLRModule.Reset(); | ||
| } | ||
| internal static void StashPush(RuntimeDataStorage storage) | ||
| { | ||
| Runtime.XIncref(py_clr_module); | ||
| Runtime.XIncref(root.pyHandle); | ||
Comment on lines +129 to +130 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Don't yo need a corresponding decref in Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I added a comment in the code for that one. The shutdown process decrefs these objects, so we need to incref them here to keep them alive through the domain reload. | ||
| storage.AddValue("py_clr_module", py_clr_module); | ||
| storage.AddValue("root", root.pyHandle); | ||
| } | ||
| internal static void StashPop(RuntimeDataStorage storage) | ||
| { | ||
| InitImport(); | ||
| storage.GetValue("py_clr_module", out py_clr_module); | ||
| var rootHandle = storage.GetValue<IntPtr>("root"); | ||
| root = (CLRModule)ManagedType.GetManagedObject(rootHandle); | ||
| } | ||
| /// <summary> | ||
| /// Return the clr python module (new reference) | ||
| /// </summary> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -103,6 +103,7 @@ public ManagedType GetAttribute(string name, bool guess) | ||
| { | ||
| m = new ModuleObject(qname); | ||
| StoreAttribute(name, m); | ||
| m.DecrRefCount(); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It seems wasteful to incref in It is also hard to check where do you need vs not need to decref after MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It's due to the sole behavior of | ||
| return m; | ||
| } | ||
| @@ -118,6 +119,7 @@ public ManagedType GetAttribute(string name, bool guess) | ||
| } | ||
| c = ClassManager.GetClass(type); | ||
| StoreAttribute(name, c); | ||
| c.DecrRefCount(); | ||
| return c; | ||
| } | ||
| @@ -132,6 +134,7 @@ public ManagedType GetAttribute(string name, bool guess) | ||
| { | ||
| m = new ModuleObject(qname); | ||
| StoreAttribute(name, m); | ||
| m.DecrRefCount(); | ||
| return m; | ||
| } | ||
| @@ -144,6 +147,7 @@ public ManagedType GetAttribute(string name, bool guess) | ||
| } | ||
| c = ClassManager.GetClass(type); | ||
| StoreAttribute(name, c); | ||
| c.DecrRefCount(); | ||
| return c; | ||
| } | ||
| } | ||
| @@ -239,6 +243,7 @@ internal void InitializeModuleMembers() | ||
| mi[0] = method; | ||
| var m = new ModuleFunctionObject(type, name, mi, allow_threads); | ||
| StoreAttribute(name, m); | ||
| m.DecrRefCount(); | ||
| } | ||
| } | ||
| @@ -251,6 +256,7 @@ internal void InitializeModuleMembers() | ||
| string name = property.Name; | ||
| var p = new ModulePropertyObject(property); | ||
| StoreAttribute(name, p); | ||
| p.DecrRefCount(); | ||
| } | ||
| } | ||
| type = type.BaseType; | ||