Skip to content

[Android] ImageReaderSurfaceProducer.waitOnFence leaks one sync_file FD per frame (image.getFence() never closed) → freeze at FD limit (API 33+) #188161

Closed

Description

@maCStacker

Steps to reproduce

Display a continuously rendering external texture (e.g. a WebRTC video stream via flutter_webrtc ≥ 0.12.12, which uses the new TextureRegistry.SurfaceProducer API) in fullscreen on an Android 13+ device, and leave it running.

Expected results

File-descriptor count stays stable; the stream keeps rendering indefinitely.

Actual results

The process leaks one anon:sync_file file descriptor per rendered frame and hits the 32768 RLIMIT_NOFILE ceiling in ~12 min (~2700 fd/min on the test device). After that all new fd operations fail ("Too many open files"), rendering freezes and the app is effectively dead.

Root cause: in the engine, ImageReaderSurfaceProducer.waitOnFence(Image) (and the twin in ImageTextureRegistryEntry) calls image.getFence() and awaitForever() but never fence.close(). SyncFence is AutoCloseable and owns a sync_file fd that the receiver must close. Across the Android embedding there are 2x
getFence() and 0x fence.close(). The fence-wait path is gated on SDK_INT >= 33, so it affects Android 13+. Present in stable 3.41.7 and current master.

Suggested fix — close the fence after awaiting (try-with-resources):
try (SyncFence fence = image.getFence()) {
fence.awaitForever();
} catch (IOException e) { /* drop */ }

Code sample

Code sample // Flutter engine source: FlutterRenderer.java -> ImageReaderSurfaceProducer.waitOnFence // (stable 3.41.7 and master) @RequiresApi(API_LEVELS.API_33) private void waitOnFence(Image image) { try { SyncFence fence = image.getFence(); fence.awaitForever(); } catch (IOException e) { // Drop. } } // fence (a SyncFence wrapping a sync_file FD) is never close()d.

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
Samsung SM-P619, Android 14. Per-minute fd breakdown from /proc/self/fd:
  anon:sync_file 2842 -> 32129 over ~12 min, while socket/file/etc. stay flat.
  Then: fcntl(F_DUPFD_CLOEXEC) failed ... Too many open files.
  Forcing the legacy SurfaceTexture path (debugForceSurfaceProducerGlTextures=true) did NOT reliably avoid it.
  Related: #143777 (closed not planned), #84702, flutter-webrtc/flutter-webrtc#1974.

Flutter Doctor output

Doctor output
  [✓] Flutter (Channel stable, 3.41.7, on Linux Mint 22.3 6.17.0-23-generic, locale de_DE.UTF-8)
      • Flutter version 3.41.7 on channel stable at /home/macyo/development/flutter
      • Framework revision cc0734ac71 (2026-04-15), Engine revision 59aa584fdf
      • Dart version 3.11.5, DevTools version 2.54.2
  [✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
      • Platform android-36, build-tools 35.0.0
      • Java version OpenJDK Runtime Environment (build 17.0.18)
      • All Android licenses accepted.
  [✓] Chrome - develop for the web
  [✓] Linux toolchain - develop for Linux desktop

Activity

  1. added
    in triagePresently being triaged by the triage team
    platform-androidAndroid applications specifically
    engineflutter/engine related. See also e: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-androidOwned by Android platform team
    and removed
    in triagePresently being triaged by the triage team
    on Jun 18, 2026
  2. mbcorona commented on Jun 18, 2026

    @mbcorona
    Member

    Hi @maCStacker , thanks for the detailed report. Routing to the Android team for investigation.

  3. mboetger commented on Jun 18, 2026

    @mboetger
    Contributor

    I believe this is a duplicate of 187222. I took at stab at fixing this with #187267. If you can test with that patch and let me know if the issue still exists that would be great.

  4. mboetger commented on Jun 18, 2026

    @mboetger
    Contributor

    It's possible #187216 also fixes it

  5. jason-simmons commented on Jun 20, 2026

    @jason-simmons
    Contributor

    The flutter-webrtc package is using external textures. So this is a different situation from #187222

    Reproduced this by running the flutter-webrtc example app on a Pixel 6a.

    The android.hardware.SyncFence Java object will eventually close the underlying file descriptor when it is garbage collected. But Java GC may not happen promptly, especially in a Flutter app that is doing very few Java operations.

    The FlutterRenderer texture classes should instead close SyncFence objects immediately, as suggested in #188161 (comment)

  6. added
    triaged-androidTriaged by Android platform team
    P1High-priority issues at the top of the work list
    on Jun 23, 2026
  7. JonasJW commented on Jul 9, 2026

    @JonasJW

    Production confirmation, with two data points that may widen this issue's
    scope and priority:

    1. This is not limited to flutter_webrtc — plain video_player hits it.
    Our fitness app plays looping exercise videos via video_player 2.10.0 /
    video_player_android 2.9.5 (the SurfaceProducer path). Workouts play video
    for 30-60 minutes; affected Android 13+ devices (e.g. Samsung SM-A525F,
    Android 14) reach the 32768 fd ceiling mid-session.

    2. Fleet-level version bisect confirming the regression window.
    Zero "Too many open files" events across 90 days of Flutter 3.41.x-built
    releases (~15 versions). We upgraded to 3.44.1 on 2026-06-11 and shipped a
    build the same day; the first EMFILE event arrived 2026-06-13, and every
    3.44-built release since shows them. Interesting nuance for the "present in
    3.41.7" note above: in practice our 3.41 fleet never exhausted fds — which is
    consistent with @jason-simmons' observation that GC eventually closes the
    SyncFence fds; something in the 3.44 era makes the leak outrun GC
    deterministically.

    What exhaustion looks like at fleet scale (in case others land here from
    the symptoms): once fence dup/merge starts failing with EMFILE, the raster
    thread blocks forever in Surface::dequeueBuffer → BufferQueueProducer::waitForFreeSlotThenRelock (on Adreno also
    Adreno-GSL ioctl_kgsl_syncobj_create fail 24), producing either a permanent
    silent freeze (UI dead, Dart isolate keeps running — audio continues, no ANR
    recorded anywhere) or, if the user touches the screen, an "Input dispatching
    timed out" ANR with the main thread in flutter::SurfaceChanged → fml::AutoResetWaitableEvent::Wait. Happy to share full thread dumps and
    logcat captures.

  8. added a commit that references this issue on Jul 20, 2026
  9. github-actions commented on Jul 23, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Jul 23, 2026
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

    P1High-priority issues at the top of the work listengineflutter/engine related. See also e: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-androidAndroid applications specificallyteam-androidOwned by Android platform teamtriaged-androidTriaged by Android platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions