Skip to content

egl: make the EglSurface handle counter atomic - #243

Open
RieBi wants to merge 1 commit into
google:mainfrom
RieBi:egl-surface-handle-atomic
Open

RieBi wants to merge 1 commit into
google:mainfrom
RieBi:egl-surface-handle-atomic

Conversation

@RieBi

@RieBi RieBi commented Nov 17, 2026

Copy link
Copy Markdown

Problem

Fixes #241.

The EglSurface constructor sets m_hndl = ++s_nextSurfaceHndl. The counter is a plain
unsigned int. eglCreatePbufferSurface() constructs the surface before it takes
graphicsDriverLock(). Thus, two threads can get the same handle. EglDisplay::addSurface() then
returns the handle of the first surface to both callers.

Fix

Make s_nextSurfaceHndl a std::atomic<unsigned int>.

Window surfaces and contexts get their handles while the lock is held. This change does not
modify them.

Test

Windows CMake build with clang-cl, target EGL_translator_static: completes. We found the race by
code review. We did not see it at run time.

This change was developed with AI assistance (Claude Code; see the Assisted-by trailer).

The EglSurface constructor sets m_hndl = ++s_nextSurfaceHndl. The
counter is a plain unsigned int. eglCreatePbufferSurface() constructs
the surface before it takes graphicsDriverLock(), and ~EglContext()
can construct one outside the lock. Thus, two threads can increment
the counter at the same time and get the same handle.

EglDisplay::addSurface() does not add a surface with a handle that is
already in the map. It returns the existing handle. The second caller
then gets the handle of the first surface, and two emulated window
surfaces share one host surface.

Make the counter std::atomic<unsigned int>.

Test: Windows CMake build with clang-cl, target EGL_translator_static.
      Found by code review; not seen at run time.

Assisted-by: Claude Code:Claude-Opus-5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EGL translator: concurrent eglCreatePbufferSurface() calls can get the same surface handle

1 participant