Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

OpenGlException: eglMakeCurrent failed with error EGL_CONTEXT_LOST

Open adirh3 opened this issue 4 years ago • 14 comments

This happens to me when using the version on the master, it's very rare but happened 3 times when I logged into my computer and opened the window using the Show functions. AllowEglInitialization=true and UseDeferredRendering = false

Application: Blast.exe CoreCLR Version: 5.0.20.27801 .NET Version: 5.0.0-preview.5.20278.1 Description: The process was terminated due to an unhandled exception. Exception Info: Avalonia.OpenGL.OpenGlException: eglMakeCurrent failed with error EGL_CONTEXT_LOST (0x300E) at Avalonia.OpenGL.EglContext.MakeCurrent(EglSurface surface) at Avalonia.OpenGL.EglGlPlatformSurface.RenderTarget.BeginDraw() at Avalonia.Skia.GlRenderTarget.BeginRenderingSession() at Avalonia.Skia.SkiaGpuRenderTarget.CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) at Avalonia.Rendering.ImmediateRenderer.Paint(Rect rect) at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) This happens to me when I use Acrylic + UseGPU randomly, couldn't find any issue like this, are you familiar with it or I should open an issue?

adirh3 avatar Jun 15 '20 19:06 adirh3

Just confirming I saw this same error too today - while resizing a window which had opened and displayed fine previously.

capdj avatar Jun 24 '20 16:06 capdj

image Update - This happened twice before crashing on the same exception

adirh3 avatar Jul 01 '20 09:07 adirh3

Please, try with this PR https://github.com/AvaloniaUI/Avalonia/pull/4242

kekekeks avatar Jul 10 '20 14:07 kekekeks

Please, try with this PR #4242

I will try and update if it works, thanks.

adirh3 avatar Jul 10 '20 14:07 adirh3

@kekekeks Didn't happen for me since I started using the latest nightly build but I also switched to the deferred renderer (not sure if it matters). Also, it's probably worth noting that this exception occurred to me only when logging to a computer through RDP.

adirh3 avatar Jul 13 '20 09:07 adirh3

Just to confirm, I've seen it happen 2-3 times running on a normal session, not via RDP so (assuming mine is the same issue) it's not confined just to RDP sessions.

capdj avatar Jul 13 '20 10:07 capdj

The thing is, there are two scenarios when EGL_CONTEXT_LOST can happen:

  1. a genuine device loss (a switch to RDP session with RemoteFX, hardware device reset, etc)
  2. an invalid command issued by Avalonia

The first one we need to handle properly, but for second one we need to determine what was causing it, since invalid calls might cause worse errors as well.

kekekeks avatar Jul 13 '20 10:07 kekekeks

The thing is, there are two scenarios when EGL_CONTEXT_LOST can happen:

  1. a genuine device loss (a switch to RDP session with RemoteFX, hardware device reset, etc)
  2. an invalid command issued by Avalonia

The first one we need to handle properly, but for second one we need to determine what was causing it, since invalid calls might cause worse errors as well.

It used to happen because of RDP but just now it happened when I was just opening the window and it has different stack trace - Exception Info: Avalonia.OpenGL.OpenGlException: eglCreateWindowSurface failed with error EGL_CONTEXT_LOST (0x300E) at Avalonia.OpenGL.EglDisplay.CreateWindowSurface(IntPtr window) at Avalonia.OpenGL.EglGlPlatformSurface.CreateGlRenderTarget() at Avalonia.Skia.GlSkiaGpu.TryCreateRenderTarget(IEnumerable1 surfaces) at Avalonia.Skia.PlatformRenderInterface.CreateRenderTarget(IEnumerable1 surfaces) at Avalonia.Controls.TopLevel.CreateRenderTarget() at Avalonia.Controls.TopLevel.Avalonia.Rendering.IRenderRoot.CreateRenderTarget() at Avalonia.Rendering.DeferredRenderer.EnsureDrawingContext(IDrawingContextImpl& context) at Avalonia.Rendering.DeferredRenderer.UpdateRenderLayersAndConsumeSceneIfNeeded(IDrawingContextImpl& context, Boolean recursiveCall) at Avalonia.Rendering.DeferredRenderer.Render(Boolean forceComposite) at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)

Also, if it helps - I noticed one of the exceptions had different error code - Exception Info: Avalonia.OpenGL.OpenGlException: eglCreateWindowSurface failed with error EGL_BAD_ALLOC (0x3003) at Avalonia.OpenGL.EglDisplay.CreateWindowSurface(IntPtr window) at Avalonia.OpenGL.EglGlPlatformSurface.CreateGlRenderTarget() at Avalonia.Skia.GlSkiaGpu.TryCreateRenderTarget(IEnumerable1 surfaces) at Avalonia.Skia.PlatformRenderInterface.CreateRenderTarget(IEnumerable1 surfaces) at Avalonia.Controls.TopLevel.CreateRenderTarget() at Avalonia.Controls.TopLevel.Avalonia.Rendering.IRenderRoot.CreateRenderTarget() at Avalonia.Rendering.ImmediateRenderer.Paint(Rect rect) at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam

adirh3 avatar Jul 13 '20 13:07 adirh3

I encountered this today while upgrading my graphics drivers for https://github.com/AvaloniaUI/XamlControlsGallery/issues/4 while XamlControlGallery was running.

Probably not all that useful, but here's the exception and stack trace:

Click to expand

Avalonia.OpenGL.OpenGlException: 'eglCreateWindowSurface failed with error EGL_CONTEXT_LOST (0x300E)'

 	Avalonia.OpenGL.dll!Avalonia.OpenGL.EglDisplay.CreateWindowSurface(System.IntPtr window)	Unknown
 	Avalonia.OpenGL.dll!Avalonia.OpenGL.EglGlPlatformSurface.CreateGlRenderTarget()	Unknown
 	Avalonia.Skia.dll!Avalonia.Skia.GlRenderTarget.GlRenderTarget(SkiaSharp.GRContext grContext, Avalonia.OpenGL.IGlPlatformSurface glSurface)	Unknown
 	Avalonia.Skia.dll!Avalonia.Skia.GlSkiaGpu.TryCreateRenderTarget(System.Collections.Generic.IEnumerable<object> surfaces)	Unknown
 	Avalonia.Skia.dll!Avalonia.Skia.PlatformRenderInterface.CreateRenderTarget(System.Collections.Generic.IEnumerable<object> surfaces)	Unknown
 	Avalonia.Controls.dll!Avalonia.Controls.TopLevel.CreateRenderTarget()	Unknown
 	Avalonia.Controls.dll!Avalonia.Controls.TopLevel.Avalonia.Rendering.IRenderRoot.CreateRenderTarget()	Unknown
 	Avalonia.Visuals.dll!Avalonia.Rendering.DeferredRenderer.EnsureDrawingContext(ref Avalonia.Platform.IDrawingContextImpl context)	Unknown
 	Avalonia.Visuals.dll!Avalonia.Rendering.DeferredRenderer.UpdateRenderLayersAndConsumeSceneIfNeeded(ref Avalonia.Platform.IDrawingContextImpl context, bool recursiveCall)	Unknown
 	Avalonia.Visuals.dll!Avalonia.Rendering.DeferredRenderer.Render(bool forceComposite)	Unknown
 	Avalonia.Visuals.dll!Avalonia.Rendering.DeferredRenderer.Paint(Avalonia.Rect rect)	Unknown
 	Avalonia.Controls.dll!Avalonia.Controls.TopLevel.HandlePaint(Avalonia.Rect rect)	Unknown
 	Avalonia.Win32.dll!Avalonia.Win32.WindowImpl.AppWndProc(System.IntPtr hWnd, uint msg, System.IntPtr wParam, System.IntPtr lParam)	Unknown
 	Avalonia.Win32.dll!Avalonia.Win32.WindowImpl.WndProc(System.IntPtr hWnd, uint msg, System.IntPtr wParam, System.IntPtr lParam)	Unknown
 	[Native to Managed Transition]	
 	user32.dll!00007fff347be858()	Unknown
 	user32.dll!00007fff347be3dc()	Unknown
 	user32.dll!00007fff347d0bc3()	Unknown
 	ntdll.dll!00007fff3612fbf4()	Unknown
 	win32u.dll!00007fff33901704()	Unknown
 	user32.dll!00007fff347be2ea()	Unknown
 	[Managed to Native Transition]	
 	Avalonia.Win32.dll!Avalonia.Win32.Win32Platform.RunLoop(System.Threading.CancellationToken cancellationToken)	Unknown
 	Avalonia.Base.dll!Avalonia.Threading.Dispatcher.MainLoop(System.Threading.CancellationToken cancellationToken)	Unknown
 	Avalonia.Controls.dll!Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(string[] args)	Unknown
 	Avalonia.Controls.dll!Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime<Avalonia.AppBuilder>(Avalonia.AppBuilder builder, string[] args, Avalonia.Controls.ShutdownMode shutdownMode)	Unknown
>	XamlControlsGallery.NetCore.dll!XamlControlsGallery.Program.Main(string[] args) Line 13	C#
 	[Native to Managed Transition]	
 	hostpolicy.dll!00007ffed5ea4e2d()	Unknown
 	hostpolicy.dll!00007ffed5eb2e27()	Unknown
 	hostpolicy.dll!00007ffed5eb2a36()	Unknown
 	hostpolicy.dll!00007ffed5eb4262()	Unknown
 	hostfxr.dll!00007ffed62c3aaa()	Unknown
 	hostfxr.dll!00007ffed62c7404()	Unknown
 	hostfxr.dll!00007ffed62c5c0b()	Unknown
 	hostfxr.dll!00007ffed62c20e9()	Unknown
 	XamlControlsGallery.NetCore.exe!00007ff6aa142361()	Unknown
 	XamlControlsGallery.NetCore.exe!00007ff6aa142758()	Unknown
 	XamlControlsGallery.NetCore.exe!00007ff6aa144608()	Unknown
 	kernel32.dll!00007fff35c66fd4()	Unknown
 	ntdll.dll!00007fff360dcec1()	Unknown

PathogenDavid avatar Sep 28 '20 01:09 PathogenDavid

CONTEXT_LOSS handling is a must have feature. Especially on a notebooks. After a sleep users are getting almost 100% repro. This is making them angry a lot :) App freeze/crash + potential progress loss is a pain. I hope this feature will have a higher priority and will be fixed in near future. Thanks!

Mikolaytis avatar Aug 11 '21 14:08 Mikolaytis

No progress on this? I encountered this issue today while simply resizing the main window of my app.

radiolondra avatar Oct 20 '21 08:10 radiolondra

@kekekeks as a quick solution - maybe let's handle CONTEXT_LOSS with exception? So we can handle it as unobserved exception, save all the changes in app and restart it.

Now device lost generates a freeze of app on MakeCurrent EGL method on windows - it's a disaster, because we can't handle this. Only solution now is to listen to Render Thread responsiveness from background thread and if there is a minute lag - restart the app.

Mikolaytis avatar Jan 23 '22 21:01 Mikolaytis

let's handle CONTEXT_LOSS with exception

  1. that exception would happen on the render thread
  2. UI thread might be in a blocking wait for the render thread to finish rendering (that currently only happens during resize or window close)

kekekeks avatar Jan 24 '22 08:01 kekekeks

I'm seeing this issue if I leave an application open and then put Windows to sleep. Upon wakeup, the exception will fire 100% of the time.

jbosh avatar Jul 20 '22 17:07 jbosh