You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SkiaSharp crash under CoreCLR & NativeAOT investigation notes #21656
difficulty/tbdCategorizes an issue for which the difficulty level needs to be defined.triage/untriagedIndicates an issue requires triaging or verification
The caller of UnoSkCanvasView.OnDrawFrame() is Android.Opengl.GLSurfaceView.IRendererInvoker.n_OnDrawFrame_Ljavax_microedition_khronos_opengles_GL10_(), which is the generated code provided below:
Not the SkRecord itself! Rather, SkRecord::fRecords contains a null value!
Which doesn't actually make sense: I don't see anything in SkRecord.h that would allow nullptr to be inserted into SkRecord::fRecords…
Unless SkRecordDraw() is doing an out-of-bounds invocation? I can't see how that would happen.
Alternatively, if the SkRecord itself were freed "from elsewhere"…. Following that thought fragment, the SkRecord comes from SkBigPicture::playback(), as *fRecord, i.e. the SkRecord lifetime is tied to the lifetime of SkBigPicture.
The lastRenderedFrame.frame in CompositionTarget.Draw() / the SkiaRenderHelper.RenderPicture() invocation is the SkBigPicture. What's the lifetime of lastRenderedFrame.frame?
lastRenderedFrame is from CompositionHelper._lastRenderedFrame:
if(lastRenderedFrameNullableis not {}lastRenderedFrame)
When CompositionHelper._lastRenderedFrame is changed, then lastRenderedFrame.frame will only be preserved by the GC for as long as that instance is referenced by managed code, i.e. it's eligible for collection here:
The GC’s reachability will ensure that objects won’t be collected until we are finished with them. But what if class C has a Finalize() method which closes _handle? When we call C.OperateOnHandle(), we now have a race between the application and the GC / Finalizer. Eventually, that’s a race we’re going to lose.
…
So far we’ve seen how the Finalizer thread and the application can race when the resource can be separated from its enclosing object.
What could be happening is:
Main thread: calls CompositionHelper.Render(), which sets CompositionHelper._lastRenderedFrame.
Render thread: reads CompositionHelper._lastRenderedFrame into lastRenderedFrame, eventually calls canvas.DrawPicture(lastRenderedFrame.frame);
GC thread comes along, sees that (picture parameter in SkiaRenderHelper.RenderPicture() / lastRenderedFrame.frame value within CompositionTarget.Draw()) is no longer referenced from managed code.
In retrospect, this actually feels like it should be fixed in SkiaSharp: mono/SkiaSharp#3393
In the meantime, here's a plausible workaround for Uno: #21660
No further auditing has been done within unoplatform/uno to look for places where lifetime should be extended. Fixing it in SkiaSharp makes more sense.
Closing this issue, as the investigation has been concluded.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or feature requests.
difficulty/tbdCategorizes an issue for which the difficulty level needs to be defined.triage/untriagedIndicates an issue requires triaging or verification
Activity
jonpryor commentedon Oct 21, 2025
#21643 has a crash in the NativeAOT stage!
Now that we have native debug symbols for
libSkiaSharp.so, we can follow the symbolification instructions:which gives a native stack trace of:
Or, in GitHub link form:
SkRecord::Record::type(): https://github.com/mono/skia/blob/927041a58f130e0dd0562ba86cb4170989ad39e9/src/core/SkRecord.h#L147SkRecord::visit<SkRecords::Draw&>(int, SkRecords::Draw&) const: https://github.com/mono/skia/blob/927041a58f130e0dd0562ba86cb4170989ad39e9/src/core/SkRecord.h#L46SkBigPicture::playback(SkCanvas*, SkPicture::AbortCallback*) const: https://github.com/mono/skia/blob/927041a58f130e0dd0562ba86cb4170989ad39e9/src/core/SkBigPicture.cpp#L37SkCanvas::onDrawPicture(SkPicture const*, SkMatrix const*, SkPaint const*): https://github.com/mono/skia/blob/927041a58f130e0dd0562ba86cb4170989ad39e9/src/core/SkCanvas.cpp#L2627SkCanvas::drawPicture(SkPicture const*, SkMatrix const*, SkPaint const*): https://github.com/mono/skia/blob/927041a58f130e0dd0562ba86cb4170989ad39e9/src/core/SkCanvas.cpp#L2616jonpryor commentedon Oct 21, 2025
Because NativeAOT causes "managed" methods to be native code, we can also infer the managed call stack!
SkiaApi.sk_canvas_draw_picture(): https://github.com/mono/SkiaSharp/blob/release/3.119.2-preview.1/binding/SkiaSharp/SkiaApi.generated.cs#L2645-L2662SkiaRenderHelper.RenderPicture():uno/src/Uno.UI/Helpers/SkiaRenderHelper.skia.cs
Line 47 in d7fb5e6
CompositionTarget.Draw():uno/src/Uno.UI/UI/Xaml/Media/CompositionTarget.Rendering.skia.cs
Lines 135 to 139 in d7fb5e6
CompositionTarget.OnNativePlatformFrameRequested():uno/src/Uno.UI/UI/Xaml/Media/CompositionTarget.RenderScheduling.skia.cs
Line 168 in d7fb5e6
UnoSKCanvasView.OnDrawFrame():uno/src/Uno.UI.Runtime.Skia.Android/Rendering/UnoSKCanvasView.cs
Lines 168 to 208 in d7fb5e6
The caller of
UnoSkCanvasView.OnDrawFrame()isAndroid.Opengl.GLSurfaceView.IRendererInvoker.n_OnDrawFrame_Ljavax_microedition_khronos_opengles_GL10_(), which is the generated code provided below:jonpryor commentedon Oct 21, 2025
Useful additional reading: Chris Brumme's Lifetime, GC.KeepAlive, handle recycling
jonpryor commentedon Oct 21, 2025
Now begins the digging and conjecture. (Fun!)
The SIGSEGV in
SkRecord::Record::type()implies that theRecordis null. This in turn implies that a member ofSkRecord::fRecordsis null: https://github.com/mono/skia/blob/927041a58f130e0dd0562ba86cb4170989ad39e9/src/core/SkRecord.h#L177Not the
SkRecorditself! Rather,SkRecord::fRecordscontains a null value!Which doesn't actually make sense: I don't see anything in
SkRecord.hthat would allownullptrto be inserted intoSkRecord::fRecords…Unless
SkRecordDraw()is doing an out-of-bounds invocation? I can't see how that would happen.Alternatively, if the
SkRecorditself were freed "from elsewhere"…. Following that thought fragment, theSkRecordcomes fromSkBigPicture::playback(), as*fRecord, i.e. theSkRecordlifetime is tied to the lifetime ofSkBigPicture.The
lastRenderedFrame.frameinCompositionTarget.Draw()/ theSkiaRenderHelper.RenderPicture()invocation is theSkBigPicture. What's the lifetime oflastRenderedFrame.frame?lastRenderedFrameis fromCompositionHelper._lastRenderedFrame:uno/src/Uno.UI/UI/Xaml/Media/CompositionTarget.Rendering.skia.cs
Lines 106 to 112 in ebfad0a
When
CompositionHelper._lastRenderedFrameis changed, thenlastRenderedFrame.framewill only be preserved by the GC for as long as that instance is referenced by managed code, i.e. it's eligible for collection here:uno/src/Uno.UI/Helpers/SkiaRenderHelper.skia.cs
Line 48 in d7fb5e6
Meanwhile, when is
CompositionHelper._lastRenderedFramechanged? This comment block describes the lifetime:uno/src/Uno.UI/UI/Xaml/Media/CompositionTarget.RenderScheduling.skia.cs
Lines 17 to 68 in ebfad0a
Of note, every time
Render()is invoked,_lastRenderedFrameis updated:uno/src/Uno.UI/UI/Xaml/Media/CompositionTarget.Rendering.skia.cs
Line 85 in ebfad0a
Now we have a plausible explanation.
From Lifetime, GC.KeepAlive, handle recycling:
What could be happening is:
CompositionHelper.Render(), which setsCompositionHelper._lastRenderedFrame.CompositionHelper._lastRenderedFrameintolastRenderedFrame, eventually callscanvas.DrawPicture(lastRenderedFrame.frame);canvas.DrawPicture(lastRenderedFrame.frame)pictureparameter inSkiaRenderHelper.RenderPicture()/lastRenderedFrame.framevalue withinCompositionTarget.Draw()) is no longer referenced from managed code.lastRenderedFrame.frame.Finalize()SKPicture(SkiaSharp) /SkBigPicture(Skia) is finalized, presumably memory is zero'd out.SkBigPicturedestroyed in (6).Furthermore, this is all "random", tied to the interaction between Render thread and the finalizer.
chore: extend the lifetime of SKPicture
jonpryor commentedon Oct 22, 2025
In retrospect, this actually feels like it should be fixed in SkiaSharp: mono/SkiaSharp#3393
In the meantime, here's a plausible workaround for Uno: #21660
No further auditing has been done within unoplatform/uno to look for places where lifetime should be extended. Fixing it in SkiaSharp makes more sense.
Closing this issue, as the investigation has been concluded.
github-actions commentedon May 1, 2026
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or feature requests.