Skip to content

FlutterEngineRemoveView may execute the callback too early #164564

@knopp

Description

@knopp

It seems that it possible that the callback provided to FlutterEngineRemoveView can be invoked before raster thread is done with the view.

The rasterization may require OpenGL context or other resources from the window that will be destroyed when the callback is invoked.

We should guarantee that we only call the callback while being sure raster thread will not attempt to render the view.

This is current code:

  task_runners_.GetUITaskRunner()->RunNowOrPostTask(
      task_runners_.GetUITaskRunner(),
      [&task_runners = task_runners_,           //
       engine = engine_->GetWeakPtr(),          //
       rasterizer = rasterizer_->GetWeakPtr(),  //
       view_id,                                 //
       callback = std::move(callback)           //
  ] {
        if (engine) {
          bool removed = engine->RemoveView(view_id);
          callback(removed);
        }
        // Don't wait for the raster task here, which only cleans up memory and
        // does not affect functionality. Make sure it is done after Dart
        // removes the view to avoid receiving another rasterization request
        // that adds back the view record.
        task_runners.GetRasterTaskRunner()->PostTask([rasterizer, view_id]() {
          if (rasterizer) {
            rasterizer->CollectView(view_id);
          }
        });
      });

I think this is racy. The rasterization request might have already been posted to raster task, so calling the callback immediately from UI thread seems too soon.

@dkwingsmt, any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectengineflutter/engine related. See also e: labels.team-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions