Skip to content

WGL: pbuffer pool grows without limit when wglReleasePbufferDC fails #238

Open

Description

@RieBi

Summary

On Windows hosts with NVIDIA OpenGL, -gpu host leaks WGL pbuffers. Each pbuffer has a hidden
NVOpenGLPbuffer window. After some hours, there are thousands of these windows. Window
operations on the full desktop become slow.

Environment

  • Android Emulator 36.4.9 and 37.2.13 (build 16740729).
  • Windows 11 (10.0.26300).
  • NVIDIA GeForce RTX 4070 Laptop GPU. Drivers 32.0.15.9159 and 32.0.16.2124.
  • Guest: API 36.1, google_apis, x86_64.

Steps to reproduce

  1. Start the emulator in a window with -gpu host.
  2. Run UI tests that start and stop apps. Continue for some hours.
  3. In Task Manager, monitor the GDI objects of qemu-system-x86_64.exe.
  4. Count the top-level windows of class NVOpenGLPbuffer that the process owns.

Result

The number of windows and GDI objects increases continuously. A framework restart
(adb shell stop; adb shell start) does not decrease it.

Boot 8 flows 16 flows After restart
NVOpenGLPbuffer windows 68 170 297 293
GDI objects 137 241 367 362

After 13.5 hours: 4,129 windows and 4,251 GDI objects. Moving a window of a different app took
13.8 ms instead of 0.29 ms. HWND_BROADCAST took 4.9 s instead of 0.33 s.

Expected result

The number of pbuffers stays approximately constant.

Cause

host/gl/glestranslator/egl/egl_os_api_wgl.cpp, WglDisplay::releasePbuffer():

if (!winpb->releaseDC()) return false;
  1. WinSurface::releaseDC() sets m_hdc to 0 and m_dcReleased to true. Then it returns the
    result of wglReleasePbufferDC().
  2. If that call fails, releasePbuffer() returns before mFreePbufs[...].push_back(pb) and
    mLivePbufs[...].erase(pb).
  3. The pbuffer stays in mLivePbufs. It is not reused and not destroyed.
  4. createPbufferSurface() refills an empty pool with max(live, 8) new pbuffers. Thus, the
    pool doubles again and again.

Evidence: we read the WglDisplay containers of a running qemu with ReadProcessMemory. After 8
UI flows, mLivePbufs had 96 pbuffers. Only 20 had a live EglPbufferSurface. The other 76 had
m_dcReleased = 1 and m_hdc = 0. The 37.2.13 DLL has the same code.

We do not know why wglReleasePbufferDC() fails in the emulator. In a test process on the same
host, it did not fail in more than 10,000 calls. #200 reports ERROR_BUSY from WGL calls on a
similar host, because drainGlRenderThreadResources() does not hold m_lock. This can be the
trigger. We did not confirm it.

Proposed fix

Always move the pbuffer to the free list. When the pool gives the pbuffer again, refreshDC()
gets a new DC. If that fails, the pbuffer goes to mDeletePbufs, as before. PR: #239.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions