Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linux] fix: make textures thread-safe on linux #40478

Merged
merged 5 commits into from
Apr 2, 2023

Conversation

Kingtous
Copy link
Contributor

@Kingtous Kingtous commented Mar 21, 2023

We've encountered some memory corruption when developing RustDesk, which is an open-source remote desktop built with Flutter, and it uses the texture API provided by Flutter to provide the remote screen to clients.

Both Windows and macOS work well, except for Linux. Sometimes random crashes will show and print different stack traces in these conditions:

  • after registering a new texture
  • preparing a RGBA data and markAvaliable

The runtime often complains double free or corruption.
One of the stack traces using lldb-10:
image

We checked the implementation of calling textures API on Linux several times, but have no luck.

We've noticed that on Linux, textures are organized by g_hash_table, which is not the thread-safe hashtable and we should ensure synchronization on different threads. Also, we noticed the textures map on Windows already has the synchronization (for example

{
std::lock_guard<std::mutex> lock(map_mutex_);
auto it = textures_.find(texture_id);
if (it != textures_.end()) {
). So the implementation on Linux lacks this synchronization on textures management.

We add the synchronization by declaring a GMutex, which is a mutex provided by GTK, in this PR. It can make g_hash_table thread-safe in different threads(ui, raster, etc).

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.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@Kingtous Kingtous force-pushed the fix/texture_linux_thread_safe branch 5 times, most recently from 7244d58 to 6cc750d Compare March 21, 2023 07:00
@Kingtous Kingtous changed the title fix: make textures thread-safe on linux [Linux] fix: make textures thread-safe on linux Mar 21, 2023
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.

Change looks good, thanks! Just some details.

shell/platform/linux/fl_texture_registrar.cc Outdated Show resolved Hide resolved
shell/platform/linux/fl_texture_registrar.cc Outdated Show resolved Hide resolved
shell/platform/linux/fl_texture_registrar_test.cc Outdated Show resolved Hide resolved
@Kingtous Kingtous force-pushed the fix/texture_linux_thread_safe branch from 3d5a544 to d27e24e Compare March 31, 2023 01:18
@flutter-dashboard
Copy link

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

Changes reported for pull request #40478 at sha d27e24e

@Kingtous Kingtous force-pushed the fix/texture_linux_thread_safe branch 2 times, most recently from 69884cc to 8d32cc7 Compare March 31, 2023 03:55
@Kingtous Kingtous force-pushed the fix/texture_linux_thread_safe branch from 8d32cc7 to ba7423e Compare March 31, 2023 04:01
@Kingtous Kingtous force-pushed the fix/texture_linux_thread_safe branch from ba7423e to 665e35a Compare March 31, 2023 04:06
@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

Changes reported for pull request #40478 at sha 665e35a

@Kingtous Kingtous force-pushed the fix/texture_linux_thread_safe branch from 90104b2 to 2f14fa7 Compare March 31, 2023 06:31
@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

Changes reported for pull request #40478 at sha 2f14fa7

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.

Changes look great now, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants