Skip to content

Don't send app lifecycle events unless there's a listener #131452

Open

Description

@loic-sharma

Repro steps

On macOS:

  1. Using a locally built engine, run the spinning square example or any app that doesn't use the widgets layer
  2. Hide/show the window a few times

You'll see the following errors in the console:

> flutter run --local-engine host_debug_unopt -d macos  ./raw/spinning_square.dart
flutter: A message on the flutter/lifecycle channel was discarded before it could be handled.
This happens when a plugin sends messages to the framework side before the framework has had an opportunity to register a listener. See the ChannelBuffers API documentation for details on how to configure the channel to expect more messages, or to expect messages to get discarded:
  https://api.flutter.dev/flutter/dart-ui/ChannelBuffers-class.html
The capacity of the flutter/lifecycle channel is 1 message.

Solution

We shouldn't send flutter/lifecycle messages unless the Dart app has a listener for that channel.

/cc @gspencergoog

Activity

  1. self-assigned this
    on Aug 3, 2023
  2. loic-sharma commented on Aug 11, 2023

    @loic-sharma
    MemberAuthor

    @bleroux provided the excellent suggestion that we could set the lifecycle channel's size to 0 so that messages are dropped if the framework isn't subscribed. If we go with this approach, we'd likely want to add a "synchronize state" message to the lifecycle channel so that the framework can get its initial state at start up.

  3. bleroux commented on Aug 11, 2023

    @bleroux
    Contributor

    Great!
    For clarification, if the only goal is to remove the error messages, the solution is not to set the buffer size to 0 but to allow the channel to overflow (see https://github.com/flutter/engine/blob/93e8901490e78c7ba7e319cce4470d9c6478c6dc/lib/ui/channel_buffers.dart#L304C1-L309C35).

    Setting the buffer size to 0 makes sense if the buffered message could be obsolete when delivered (this is the case in order to fix #125975).

    FYI, I’m working on several PRs to add helper functions for the ‘resize’ and ‘overflow’ commands (see #132386).
    I filed flutter-team-archive/engine#44636 for Linux and flutter-team-archive/engine#44434 for Android.
    I plan to file the macOS implementation at the end of next week. And the Windows one the week after.

  4. gspencergoog commented on Aug 14, 2023

    @gspencergoog
    Contributor

    I've tried manipulating the buffer size before, and when I was initially implementing the lifecycle events I had a little trouble manipulating the buffer size at startup time, simply because the Dart isolate wasn't up and running yet when I needed to set the buffer size. Since the buffer sizing code is in Dart, it doesn't actually change the buffer size in that case.

    This might not be an issue here, but I thought I'd mention it.

  5. flutter-triage-bot commented on Jan 1, 2024

    @flutter-triage-bot

    This issue is assigned to @gspencergoog but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  6. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Jan 1, 2024
  7. gspencergoog commented on Jan 2, 2024

    @gspencergoog
    Contributor

    This is still planned work. It seems like setting the buffer size to zero isn't going to work here, for the reasons I outlined above, so we'll need to add a mechanism for telling the engine when listeners have subscribed and unsubscribed. The alternative would be to move the buffering code into each platform implementation so it can take effect even if the Dart isolate isn't running yet, which seems like a lot of duplicated code.

  8. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Jan 2, 2024
  9. loic-sharma commented on Feb 27, 2024

    @loic-sharma
    MemberAuthor

    FYI, this was fixed on Windows by introducing a "channel updates" embedder API (here, here). The Windows embedder uses this callback to drop lifecycle events (here) until it is notified that the Dart app is listening to the flutter/lifecycle channel (here, here)

  10. flutter-triage-bot commented on Jun 7, 2024

    @flutter-triage-bot

    The triaged-desktop label is irrelevant if there is no team-desktop label or fyi-desktop label.

  11. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Oct 11, 2024
  12. flutter-triage-bot commented on Oct 11, 2024

    @flutter-triage-bot

    This issue is assigned to @gspencergoog but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  13. removed their assignment
    on Oct 11, 2024
  14. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Oct 11, 2024
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

    P2Important issues not at the top of the work lista: desktopRunning on desktopteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions