Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@gaaclarke
Copy link
Member

@gaaclarke gaaclarke commented Nov 16, 2022

This makes it so platform channel messages can be responded to from any thread. Most of the work has already been implemented under the hood. This had to add support for the linux embedder. The important thing is that this switched the BinaryMessenger's weak reference to the engine to a GWeakRef which is thread-safe. This guarantees that the engine is not deleted while we are using it to dispatch the response to the ui thread.

issue: flutter/flutter#93945

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@gaaclarke gaaclarke force-pushed the threadsafe-linux-replies branch 2 times, most recently from 723b3ff to 96a4de7 Compare November 16, 2022 22:04
@flutter flutter deleted a comment from google-cla bot Nov 16, 2022
@gaaclarke
Copy link
Member Author

@robert-ancell @jpnurmi can you give this PR a look please? I think there is a bug in it where the dispose method for the engine may now be called from a background thread. I've added a comment at that part above.

For some context, I don't have tests written yet but the idea is to make it so platform channels can be responded to on any thread. This PR should be close but has one potential bug I can see. Also, I have zero gtk experience so I might be doing something wrong.

My thought is the easiest fix would be to add a ref in the background thread and post a task on the platform thread to unref it. That would guarantee the dispose happened on the platform thread. Let me know what you think. Thanks!

@robert-ancell
Copy link
Contributor

I tried to think if there were other methods of doing this (for example, making an asynchronous function that would do the send in the main loop and return the result to the calling thread), but I think your solution is probably the most practical.

It would be nice if Flutter plugin developers would synchronize all their responses to the glib main loop but it is C, so people will try and use threads. 😢

@gaaclarke
Copy link
Member Author

gaaclarke commented Nov 17, 2022

@robert-ancell Thanks for taking a look! I'll clean it up.

It would be nice if Flutter plugin developers would synchronize all their responses to the glib main loop but it is C, so people will try and use threads. 😢

Yea, there is a usability aspect to this change, the other driving force was performance. The platform thread can become saturated (especially at startup) and the largest cost for platform methods is thread hops. If we make users jump back to the platform thread to respond to messages running on different threads they are jumping to the platform thread to almost immediately jump to the ui thread, which is wasteful and slow.

@gaaclarke gaaclarke force-pushed the threadsafe-linux-replies branch 5 times, most recently from 8def204 to fe44841 Compare November 18, 2022 22:44
@gaaclarke gaaclarke marked this pull request as ready for review November 18, 2022 22:45
@gaaclarke gaaclarke force-pushed the threadsafe-linux-replies branch from d99e3db to e9356c6 Compare November 22, 2022 22:19
Copy link
Contributor

@robert-ancell robert-ancell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good other than the above comments.

@gaaclarke gaaclarke force-pushed the threadsafe-linux-replies branch from a4458db to f887d1c Compare November 24, 2022 00:22
Copy link
Contributor

@robert-ancell robert-ancell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@gaaclarke
Copy link
Member Author

giphy

@gaaclarke gaaclarke added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 24, 2022
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 24, 2022
@auto-submit
Copy link
Contributor

auto-submit bot commented Nov 24, 2022

auto label is removed for flutter/engine, pr: 37689, due to - The status or check suite Linux Framework Smoke Tests has failed. Please fix the issues identified (or deflake) before re-applying this label.

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

Labels

autosubmit Merge PR when tree becomes green via auto submit App platform-linux

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants