Skip to content

[Engine, PlatformView, Hybird Composition] Potential deadlock on platform thread caused by Thread Merge #80491

Closed

Description

@rogeryi

Suppose we have TaskRunner A, which belong to Thread A currently, and TaskRunner B, which belong to Thread B currently. The following steps happen sequentially will cause deadlock.

  1. Thread B is executing Task M, which belong to TaskRunner B;
  2. Thread A post Task N to TaskRunner B, and block itself to wait Task N signal the latch;
  3. Task M merge TaskRunner B to TaskRunner A, then the new added Task N will be executed on Thread A;
  4. But Thread A is waiting, then deadlock happen;
void PlatformViewAndroid::NotifyChanged(const SkISize& size) {
  if (!android_surface_) {
    return;
  }
  fml::AutoResetWaitableEvent latch;
  fml::TaskRunner::RunNowOrPostTask(
      task_runners_.GetRasterTaskRunner(),  //
      [&latch, surface = android_surface_.get(), size]() {
        surface->OnScreenSurfaceResize(size);
        latch.Signal();
      });
  latch.Wait();
}

The real scenario in Flutter:

  1. Raster thread is drawing a frame, and SurfaceChanged just happen on Platform thread;
  2. Platform thread post a task to RasterTaskRunner to resize Surface, which still belong to raster thread at this moment;
  3. Platform thread is waiting the above resize task executing;
  4. At the end of frame drawing, because we have a new PlatformLayer, then Rasterizer will merge RasterTaskRunner to PlatformTaskRunner, then the new added resize task will be executed on Platform thread now, and we got deadlock on Platform thread;

Activity

  1. changed the title [-]Potential deadlock on platform thread caused by Thread Merge[/-] [+][Engine] Potential deadlock on platform thread caused by Thread Merge[/+] on Apr 15, 2021
  2. changed the title [-][Engine] Potential deadlock on platform thread caused by Thread Merge[/-] [+][Engine, PlatformView, Hybird Composition] Potential deadlock on platform thread caused by Thread Merge[/+] on Apr 15, 2021
  3. TahaTesser commented on Apr 15, 2021

    @TahaTesser
    Contributor

    Hi @rogeryi
    Unfortunately, I am not familiar with the Engine platform view.
    Can you please explain if this is a bug and Is there any way to verify these reproducible steps?
    Thank you

  4. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Apr 15, 2021
  5. rogeryi commented on Apr 15, 2021

    @rogeryi
    ContributorAuthor

    Hi @rogeryi
    Unfortunately, I am not familiar with the Engine platform view.
    Can you please explain if this is a bug and Is there any way to verify these reproducible steps?
    Thank you

    Yes, its a bug and when it happen, it will cause Flutter Platform thread deadlock and the App will not response anymore.
    But this is probabilistic threading issue, and its chance is rare, although in theory it is exists, but hard to reproduce it stably.

  6. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Apr 15, 2021
  7. added
    engineflutter/engine related. See also e: labels.
    c: performanceRelates to speed or footprint issues (see "perf:" labels)
    a: platform-viewsEmbedding Android/iOS views in Flutter apps
    and removed
    in triagePresently being triaged by the triage team
    on Apr 15, 2021
  8. added
    P1High-priority issues at the top of the work list
    on Apr 19, 2021
  9. tylorvan commented on Jul 30, 2021

    @tylorvan

    Is it resolved? We has faced the same bug in our app.

  10. tongyangsheng commented on Nov 30, 2021

    @tongyangsheng

    Is it resolved? We has faced the same bug in our app.

  11. tongyangsheng commented on Nov 30, 2021

    @tongyangsheng

    We currently use PlatfromVie's technical solution in our own application. We have discovered a large number of thread merging problems causing deadlocks. Our number of users is in the tens of millions. One of the interfaces has caused a very high problem due to this thread merging problem. Crash rate, I think this is a problem worthy of attention.

  12. dnfield commented on Dec 9, 2021

    @dnfield
    Contributor

    I'm going to close this in favor of #94524, which at this point has more context.

  13. github-actions commented on Dec 23, 2021

    @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.

  14. locked as resolved and limited conversation to collaborators on Dec 23, 2021
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 lista: platform-viewsEmbedding Android/iOS views in Flutter appsc: performanceRelates to speed or footprint issues (see "perf:" labels)engineflutter/engine related. See also e: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions