Skip to content

[Linux] Fix GLX/EGL context desynchronization in multi-window rendering - #193530

Open
jtmcdole wants to merge 2 commits into
masterfrom
windowing_test_egl_desync
Open

jtmcdole wants to merge 2 commits into
masterfrom
windowing_test_egl_desync

Conversation

@jtmcdole

@jtmcdole jtmcdole commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Fixes a Gdk-ERROR: BadAccess (GLX minor_code 26) crash when realizing secondary FlViews on X11.

Under libglvnd, calling eglMakeCurrent on the GTK main thread while a GLX context (GdkGLContext) is current fails with EGL_BAD_ACCESS ("Another window API already has a current context"). Because EGL does not unbind foreign APIs, the GLX context must be explicitly unbound first via gdk_gl_context_clear_current() so that an X_GLXMakeContextCurrent(None) request is dispatched to the X server, preventing client-server state desynchronization.

  • Call gdk_gl_context_clear_current() in fl_opengl_manager_make_platform_current() before eglMakeCurrent().
  • Use fl_opengl_manager_make_platform_current() in fl_compositor_opengl_new() and call fl_opengl_manager_clear_current() after compositor/shader setup and disposal.
  • Add unit test FlCompositorOpenGLTest.ClearsGdkGLContextBeforePlatformContext.

Part of stack #193534.

@jtmcdole
jtmcdole requested a review from a team as a code owner September 29, 2026 22:50
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Sep 29, 2026
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-linux Building on or for Linux specifically a: desktop Running on desktop team-linux Owned by the Linux platform team labels Sep 29, 2026
@jtmcdole
jtmcdole added this pull request to stack #193531 September 29, 2026 22:50
@jtmcdole jtmcdole changed the title Fix GLX/EGL context desynchronization in multi-window rendering and enable Xvfb for windowing_test [Linux] Fix GLX/EGL context desynchronization in multi-window rendering and enable Xvfb for windowing_test Sep 29, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Linux platform OpenGL manager to explicitly clear the GDK GL context before making the platform context current, preventing EGL bad access errors under libglvnd. It also ensures OpenGL contexts are cleared after setup and disposal, and adds a corresponding unit test. Review feedback suggests checking the return value of fl_opengl_manager_make_platform_current to prevent undefined behavior, using a local g_autoptr in the test to avoid potential memory leaks, and maintaining context symmetry by clearing the OpenGL context in the caller of setup_shader rather than inside the function.

@jtmcdole
jtmcdole force-pushed the windowing_test_egl_desync branch from 92d4ca7 to 2f8ce20 Compare September 29, 2026 23:02
@jtmcdole
jtmcdole removed this pull request from stack #193531 September 29, 2026 23:02
@jtmcdole
jtmcdole added this pull request to stack #193534 September 29, 2026 23:03
@jtmcdole jtmcdole changed the title [Linux] Fix GLX/EGL context desynchronization in multi-window rendering and enable Xvfb for windowing_test [Linux] Fix GLX/EGL context desynchronization in multi-window rendering Sep 29, 2026
@jtmcdole

Copy link
Copy Markdown
Member Author

Turns out, stacked prs are not going to work since Cocoon has no clue what's going on.

@jtmcdole
jtmcdole force-pushed the windowing_test_egl_desync branch from 2f8ce20 to 6220144 Compare September 29, 2026 23:14
Base automatically changed from windowing_test_openbox to master October 1, 2026 22:52
@jtmcdole
jtmcdole force-pushed the windowing_test_egl_desync branch from 6220144 to a0941d5 Compare October 1, 2026 22:52
Under `libglvnd`, calling `eglMakeCurrent` on the GTK main thread while a GLX context (`GdkGLContext`) is active fails with `EGL_BAD_ACCESS` ("Another window API already has a current context"). Because EGL does not unbind foreign APIs, the GLX context must be explicitly unbound first via `gdk_gl_context_clear_current()` so that an `X_GLXMakeContextCurrent(None)` request is dispatched to the X server, preventing client-server state desynchronization.

Use `fl_opengl_manager_make_platform_current()` instead of `fl_opengl_manager_make_current()` in `fl_compositor_opengl_new()` and clear the EGL context via `fl_opengl_manager_clear_current()` after compositor/shader setup and disposal.
@jtmcdole
jtmcdole force-pushed the windowing_test_egl_desync branch from a0941d5 to 8a756b2 Compare October 2, 2026 03:07
@jtmcdole
jtmcdole requested a review from gaaclarke October 2, 2026 03:07
@jtmcdole

jtmcdole commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

I'm going to have to land this one PR at a time since ci.yaml doesn't support stacked branches. Good feature request I guess.....

gaaclarke
gaaclarke previously approved these changes Oct 6, 2026

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there integration tests for this too? This sort of thing makes me a bit nervous since I'm not as familiar with the testing. LGTM otherwise.

fl_opengl_manager_make_current(opengl_manager);
self->can_blit = fl_opengl_manager_can_blit(opengl_manager);
self->can_fence = fl_opengl_manager_can_fence(opengl_manager);
if (fl_opengl_manager_make_platform_current(opengl_manager)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this suggests this code shouldn't be here and instead it should lazy-load these when required. But this is existing behaviour so doesn't have to be part of this PR.

// explicitly unbound first via gdk_gl_context_clear_current() so that an
// X_GLXMakeContextCurrent(None) request is dispatched to the X server,
// preventing client-server state desynchronization.
gdk_gl_context_clear_current();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a bad code smell - the opengl manager shouldn't need to know anything about GDK. This should have been called somewhere else.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a change that I think fixes this.

@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If both you and the PR author believe this PR qualifies for a test exemption, escalate to the tech lead for the changed area to review and apply the test-exempt label. All reviewers should feel empowered to ask for tests before escalating.

@robert-ancell robert-ancell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got to love context headaches.

@flutter-dashboard

Copy link
Copy Markdown

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop CICD Run CI/CD engine flutter/engine related. See also e: labels. missing-tests platform-linux Building on or for Linux specifically team-linux Owned by the Linux platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants