Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d89411

Browse files
committed
dropped WaitForFullGCComplete, as GC.Collect is blocking
1 parent002d730 commit8d89411

File tree

3 files changed

+47
-53
lines changed

3 files changed

+47
-53
lines changed

‎src/embed_tests/TestFinalizer.cs

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,16 @@ public void TearDown()
2828
PythonEngine.Shutdown();
2929
}
3030

31-
privatestaticboolFullGCCollect()
31+
privatestaticvoidFullGCCollect()
3232
{
33-
GC.Collect(GC.MaxGeneration,GCCollectionMode.Forced);
34-
try
35-
{
36-
returnGC.WaitForFullGCComplete()==GCNotificationStatus.Succeeded;
37-
}
38-
catch(NotImplementedException)
39-
{
40-
// Some clr runtime didn't implement GC.WaitForFullGCComplete yet.
41-
returnfalse;
42-
}
43-
finally
44-
{
45-
GC.WaitForPendingFinalizers();
46-
}
33+
Thread.MemoryBarrier();
34+
GC.Collect();
35+
GC.WaitForPendingFinalizers();
36+
Thread.MemoryBarrier();
4737
}
4838

4939
[Test]
40+
[Obsolete("GC tests are not guaranteed")]
5041
publicvoidCollectBasicObject()
5142
{
5243
Assert.IsTrue(Finalizer.Instance.Enable);
@@ -103,17 +94,13 @@ public void CollectBasicObject()
10394
}
10495

10596
[Test]
97+
[Obsolete("GC tests are not guaranteed")]
10698
publicvoidCollectOnShutdown()
10799
{
108100
IntPtrop=MakeAGarbage(outvarshortWeak,outvarlongWeak);
109-
inthash=shortWeak.Target.GetHashCode();
110-
List<WeakReference>garbage;
111-
if(!FullGCCollect())
112-
{
113-
Assert.IsTrue(WaitForCollected(op,hash,10000));
114-
}
101+
FullGCCollect();
115102
Assert.IsFalse(shortWeak.IsAlive);
116-
garbage=Finalizer.Instance.GetCollectedObjects();
103+
List<WeakReference>garbage=Finalizer.Instance.GetCollectedObjects();
117104
Assert.IsNotEmpty(garbage,"The garbage object should be collected");
118105
Assert.IsTrue(garbage.Any(r=>ReferenceEquals(r.Target,longWeak.Target)),
119106
"Garbage should contains the collected object");
@@ -123,13 +110,29 @@ public void CollectOnShutdown()
123110
Assert.IsEmpty(garbage);
124111
}
125112

126-
[MethodImpl(MethodImplOptions.NoInlining)]// ensure lack of references to obj
113+
[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.NoOptimization)]// ensure lack of references to obj
114+
[Obsolete("GC tests are not guaranteed")]
127115
privatestaticIntPtrMakeAGarbage(outWeakReferenceshortWeak,outWeakReferencelongWeak)
128116
{
129-
PyLongobj=newPyLong(1024);
130-
shortWeak=newWeakReference(obj);
131-
longWeak=newWeakReference(obj,true);
132-
returnobj.Handle;
117+
IntPtrhandle=IntPtr.Zero;
118+
WeakReference@short=null,@long=null;
119+
// must create Python object in the thread where we have GIL
120+
IntPtrval=PyLong.FromLong(1024);
121+
// must create temp object in a different thread to ensure it is not present
122+
// when conservatively scanning stack for GC roots.
123+
// see https://xamarin.github.io/bugzilla-archives/17/17593/bug.html
124+
vargarbageGen=newThread(()=>
125+
{
126+
varobj=newPyObject(val,skipCollect:true);
127+
@short=newWeakReference(obj);
128+
@long=newWeakReference(obj,true);
129+
handle=obj.Handle;
130+
});
131+
garbageGen.Start();
132+
Assert.IsTrue(garbageGen.Join(TimeSpan.FromSeconds(5)),"Garbage creation timed out");
133+
shortWeak=@short;
134+
longWeak=@long;
135+
returnhandle;
133136
}
134137

135138
privatestaticlongCompareWithFinalizerOn(PyObjectpyCollect,boolenbale)
@@ -210,6 +213,7 @@ internal static void CreateMyPyObject(IntPtr op)
210213
}
211214

212215
[Test]
216+
[Obsolete("GC tests are not guaranteed")]
213217
publicvoidErrorHandling()
214218
{
215219
boolcalled=false;
@@ -227,7 +231,7 @@ public void ErrorHandling()
227231
WeakReferencelongWeak;
228232
{
229233
MakeAGarbage(outshortWeak,outlongWeak);
230-
varobj=(PyLong)longWeak.Target;
234+
varobj=(PyObject)longWeak.Target;
231235
IntPtrhandle=obj.Handle;
232236
shortWeak=null;
233237
longWeak=null;
@@ -278,36 +282,13 @@ public void ValidateRefCount()
278282
}
279283
}
280284

285+
[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.NoOptimization)]// ensure lack of references to s1 and s2
281286
privatestaticIntPtrCreateStringGarbage()
282287
{
283288
PyStrings1=newPyString("test_string");
284289
// s2 steal a reference from s1
285290
PyStrings2=newPyString(s1.Handle);
286291
returns1.Handle;
287292
}
288-
289-
privatestaticboolWaitForCollected(IntPtrop,inthash,intmilliseconds)
290-
{
291-
varstopwatch=Stopwatch.StartNew();
292-
do
293-
{
294-
vargarbage=Finalizer.Instance.GetCollectedObjects();
295-
foreach(varitemingarbage)
296-
{
297-
// The validation is not 100% precise,
298-
// but it's rare that two conditions satisfied but they're still not the same object.
299-
if(item.Target.GetHashCode()!=hash)
300-
{
301-
continue;
302-
}
303-
varobj=(IPyDisposable)item.Target;
304-
if(obj.GetTrackedHandles().Contains(op))
305-
{
306-
returntrue;
307-
}
308-
}
309-
}while(stopwatch.ElapsedMilliseconds<milliseconds);
310-
returnfalse;
311-
}
312293
}
313294
}

‎src/runtime/pylong.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public PyLong(uint value) : base(FromInt((int)value))
7474
}
7575

7676

77-
privatestaticIntPtrFromLong(longvalue)
77+
internalstaticIntPtrFromLong(longvalue)
7878
{
7979
IntPtrval=Runtime.PyLong_FromLongLong(value);
8080
PythonException.ThrowIfIsNull(val);

‎src/runtime/pyobject.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ public PyObject(IntPtr ptr)
5454
#endif
5555
}
5656

57+
[Obsolete("this is for testing purposes only")]
58+
internalPyObject(IntPtrptr,boolskipCollect)
59+
{
60+
if(ptr==IntPtr.Zero)thrownewArgumentNullException(nameof(ptr));
61+
62+
obj=ptr;
63+
if(!skipCollect)
64+
Finalizer.Instance.ThrottledCollect();
65+
#ifTRACE_ALLOC
66+
Traceback=newStackTrace(1);
67+
#endif
68+
}
69+
5770
/// <summary>
5871
/// Creates new <see cref="PyObject"/> pointing to the same object as
5972
/// the <paramref name="reference"/>. Increments refcount, allowing <see cref="PyObject"/>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp