Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 27905c8

Browse files
author
dse
committed
Runtime/Shutdown loop stores old caches and static variables. It's produces bugs when CPython freeing up enough objects.
1 parent dac5a96 commit 27905c8

9 files changed

+53
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2121

2222
### Fixed
2323

24+
- Fixed secondary PythonEngine.Initialize call, all sensitive static variables now reseted.
25+
This is a hidden bug. Once python cleaning up enough memory, objects from previous engine run becomes corrupted.
2426
- Fixed Visual Studio 2017 compat (#434) for setup.py
2527
- Fixed crash on exit of the Python interpreter if a python class
2628
derived from a .NET class has a `__namespace__` or `__assembly__`

src/runtime/assemblymanager.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ internal class AssemblyManager
1717
{
1818
// modified from event handlers below, potentially triggered from different .NET threads
1919
// therefore this should be a ConcurrentDictionary
20-
private static ConcurrentDictionary<string, ConcurrentDictionary<Assembly, string>> namespaces;
20+
private static ConcurrentDictionary<string, ConcurrentDictionary<Assembly, string>> namespaces =
21+
new ConcurrentDictionary<string, ConcurrentDictionary<Assembly, string>>();
2122
//private static Dictionary<string, Dictionary<string, string>> generics;
2223
private static AssemblyLoadEventHandler lhandler;
2324
private static ResolveEventHandler rhandler;
2425

2526
// updated only under GIL?
26-
private static Dictionary<string, int> probed;
27+
private static Dictionary<string, int> probed = new Dictionary<string, int>(32);
2728

2829
// modified from event handlers below, potentially triggered from different .NET threads
2930
private static AssemblyList assemblies;
@@ -40,9 +41,6 @@ private AssemblyManager()
4041
/// </summary>
4142
internal static void Initialize()
4243
{
43-
namespaces = new ConcurrentDictionary<string, ConcurrentDictionary<Assembly, string>>();
44-
probed = new Dictionary<string, int>(32);
45-
//generics = new Dictionary<string, Dictionary<string, string>>();
4644
assemblies = new AssemblyList(16);
4745
pypath = new List<string>(16);
4846

src/runtime/classderived.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
55
using System.Reflection.Emit;
6+
using System.Resources;
67
using System.Runtime.InteropServices;
78
using System.Threading.Tasks;
89

@@ -32,6 +33,12 @@ static ClassDerivedObject()
3233
moduleBuilders = new Dictionary<Tuple<string, string>, ModuleBuilder>();
3334
}
3435

36+
public static void Reset()
37+
{
38+
assemblyBuilders = new Dictionary<string, AssemblyBuilder>();
39+
moduleBuilders = new Dictionary<Tuple<string, string>, ModuleBuilder>();
40+
}
41+
3542
internal ClassDerivedObject(Type tp) : base(tp)
3643
{
3744
}

src/runtime/classmanager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ static ClassManager()
3434
dtype = typeof(MulticastDelegate);
3535
}
3636

37+
public static void Reset()
38+
{
39+
cache = new Dictionary<Type, ClassBase>(128);
40+
}
41+
3742
/// <summary>
3843
/// Return the ClassBase-derived instance that implements a particular
3944
/// reflected managed type, creating it if it doesn't yet exist.

src/runtime/genericutil.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Resources;
34

45
namespace Python.Runtime
56
{
@@ -20,6 +21,11 @@ static GenericUtil()
2021
mapping = new Dictionary<string, Dictionary<string, List<string>>>();
2122
}
2223

24+
public static void Reset()
25+
{
26+
mapping = new Dictionary<string, Dictionary<string, List<string>>>();
27+
}
28+
2329
/// <summary>
2430
/// Register a generic type that appears in a given namespace.
2531
/// </summary>

src/runtime/moduleobject.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,17 @@ public CLRModule() : base("clr")
328328
}
329329
}
330330

331+
public static void Reset()
332+
{
333+
hacked = false;
334+
interactive_preload = true;
335+
preload = false;
336+
337+
// XXX Test performance of new features //
338+
_SuppressDocs = false;
339+
_SuppressOverloads = false;
340+
}
341+
331342
/// <summary>
332343
/// The initializing of the preload hook has to happen as late as
333344
/// possible since sys.ps1 is created after the CLR module is

src/runtime/pyscope.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,15 @@ public void Dispose()
537537

538538
public class PyScopeManager
539539
{
540-
public readonly static PyScopeManager Global = new PyScopeManager();
540+
public static PyScopeManager Global;
541541

542542
private Dictionary<string, PyScope> NamedScopes = new Dictionary<string, PyScope>();
543543

544+
internal static void Reset()
545+
{
546+
Global = new PyScopeManager();
547+
}
548+
544549
internal PyScope NewScope(string name)
545550
{
546551
if (name == null)

src/runtime/runtime.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ internal static void Initialize()
221221
PyEval_InitThreads();
222222
}
223223

224+
CLRModule.Reset();
225+
GenericUtil.Reset();
226+
PyScopeManager.Reset();
227+
ClassManager.Reset();
228+
ClassDerivedObject.Reset();
229+
TypeManager.Reset();
230+
224231
IntPtr op;
225232
IntPtr dict;
226233
if (IsPython3)

src/runtime/typemanager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Reflection;
@@ -21,6 +21,10 @@ static TypeManager()
2121
cache = new Dictionary<Type, IntPtr>(128);
2222
}
2323

24+
public static void Reset()
25+
{
26+
cache = new Dictionary<Type, IntPtr>(128);
27+
}
2428

2529
/// <summary>
2630
/// Given a managed Type derived from ExtensionType, get the handle to

0 commit comments

Comments
 (0)