🌐 AI搜索 & 代理 主页
Skip to content

Commit bbd8582

Browse files
committed
Skip unstashing more aggressively
Also, ensures that if the unstashing fails, we re-initialize the clr module.
1 parent 7d3a6dc commit bbd8582

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/runtime/Runtime.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ internal static void Initialize(bool initSigs = false)
164164
// Initialize modules that depend on the runtime class.
165165
AssemblyManager.Initialize();
166166
OperatorMethod.Initialize();
167-
if (RuntimeData.HasStashData())
168-
{
169-
RuntimeData.RestoreRuntimeData();
170-
}
171-
else
167+
if (!RuntimeData.RestoreRuntimeData())
172168
{
173169
PyCLRMetaType = MetaType.Initialize();
174170
ImportHook.Initialize();

src/runtime/StateSerialization/NoopFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Python.Runtime;
66

77
public class NoopFormatter : IFormatter {
8-
public object Deserialize(Stream s) => throw new NotImplementedException();
8+
public object Deserialize(Stream s) => null; // throw new NotImplementedException();
99
public void Serialize(Stream s, object o) {}
1010

1111
public SerializationBinder? Binder { get; set; }

src/runtime/StateSerialization/RuntimeData.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,47 +112,47 @@ internal static void Stash()
112112
PostStashHook?.Invoke();
113113
}
114114

115-
internal static void RestoreRuntimeData()
115+
internal static bool RestoreRuntimeData()
116116
{
117117
try
118118
{
119-
RestoreRuntimeDataImpl();
119+
return RestoreRuntimeDataImpl();
120120
}
121121
finally
122122
{
123123
ClearStash();
124124
}
125125
}
126126

127-
private static void RestoreRuntimeDataImpl()
127+
private static bool RestoreRuntimeDataImpl()
128128
{
129129
PreRestoreHook?.Invoke();
130130
BorrowedReference capsule = PySys_GetObject("clr_data");
131131
if (capsule.IsNull)
132132
{
133-
return;
133+
return false;
134134
}
135135
IntPtr mem = PyCapsule_GetPointer(capsule, IntPtr.Zero);
136136
int length = (int)Marshal.ReadIntPtr(mem);
137137
byte[] data = new byte[length];
138138
Marshal.Copy(mem + IntPtr.Size, data, 0, length);
139139
var ms = new MemoryStream(data);
140140
var formatter = CreateFormatter();
141-
var storage = (PythonNetState)formatter.Deserialize(ms);
142141

143-
PyCLRMetaType = MetaType.RestoreRuntimeData(storage.Metatype);
142+
if (formatter.Deserialize(ms) is PythonNetState storage)
143+
{
144+
PyCLRMetaType = MetaType.RestoreRuntimeData(storage.Metatype);
144145

145-
TypeManager.RestoreRuntimeData(storage.Types);
146-
ClassManager.RestoreRuntimeData(storage.Classes);
146+
TypeManager.RestoreRuntimeData(storage.Types);
147+
ClassManager.RestoreRuntimeData(storage.Classes);
147148

148-
RestoreRuntimeDataObjects(storage.SharedObjects);
149+
RestoreRuntimeDataObjects(storage.SharedObjects);
149150

150-
ImportHook.RestoreRuntimeData(storage.ImportHookState);
151-
}
151+
ImportHook.RestoreRuntimeData(storage.ImportHookState);
152+
return true;
153+
}
152154

153-
public static bool HasStashData()
154-
{
155-
return !PySys_GetObject("clr_data").IsNull;
155+
return false;
156156
}
157157

158158
public static void ClearStash()

0 commit comments

Comments
 (0)