Repository navigation
Commit 2a9821a
committed
fix: Resolve ImageReader ANRs by running OnImageAvailableListener on a background HandlerThread (flutter#168716)
Moves the `ImageReader.OnImageAvailableListener` to run on a dedicated background `HandlerThread` owned by `ImageReaderSurfaceProducer` to avoid blocking the Android UI thread with graphics buffer allocation (`acquireLatestImage`) which led to severe Input Dispatching Timed Out ANRs and graphics driver crashes on low-end devices.
Key changes:
- Created and managed a dedicated `HandlerThread` inside `ImageReaderSurfaceProducer` to handle `OnImageAvailable` callbacks.
- Refactored `onImage` to receive `PerImageReader` directly, eliminating map lookups and preventing memory leaks of native images when callbacks race with producer release/cleanup.
- Marshalled `scheduleEngineFrame()` (which performs JNI frame scheduling) back to the main thread via the outer class's main-thread `handler` to respect the `@UiThread` check of `FlutterJNI`.
- Marshalled the entire `release()` sequence in `finalize()` back to the main thread to prevent thread data races on non-thread-safe lists and set collections inside `FlutterRenderer` on the GC finalizer thread.
- Made state and thread fields `volatile` to guarantee proper visibility across the main thread, background thread, and GC threads.
- Added a dedicated Robolectric test `ImageReaderSurfaceProducerOnImageAvailableListenerRunsOnBackgroundThread` to verify that the listener runs on a non-main-thread Handler looper, and updated existing Robolectric tests to idle the background looper during frame rendering.1 parent 8bc1f30 commit 2a9821a
2 files changed
Lines changed: 78 additions & 11 deletions
File tree
- engine/src/flutter/shell/platform/android
- io/flutter/embedding/engine/renderer
- test/io/flutter/embedding/engine/renderer
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
470 | 471 | | |
471 | 472 | | |
472 | 473 | | |
473 | | - | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
474 | 477 | | |
475 | 478 | | |
476 | 479 | | |
| |||
522 | 525 | | |
523 | 526 | | |
524 | 527 | | |
525 | | - | |
| 528 | + | |
526 | 529 | | |
527 | 530 | | |
528 | 531 | | |
| |||
541 | 544 | | |
542 | 545 | | |
543 | 546 | | |
544 | | - | |
| 547 | + | |
545 | 548 | | |
546 | 549 | | |
547 | | - | |
| 550 | + | |
548 | 551 | | |
549 | 552 | | |
550 | 553 | | |
| |||
631 | 634 | | |
632 | 635 | | |
633 | 636 | | |
634 | | - | |
| 637 | + | |
635 | 638 | | |
636 | 639 | | |
637 | | - | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
638 | 644 | | |
639 | 645 | | |
640 | 646 | | |
| |||
647 | 653 | | |
648 | 654 | | |
649 | 655 | | |
650 | | - | |
| 656 | + | |
651 | 657 | | |
652 | 658 | | |
653 | 659 | | |
| |||
657 | 663 | | |
658 | 664 | | |
659 | 665 | | |
660 | | - | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
661 | 672 | | |
662 | 673 | | |
663 | 674 | | |
| |||
755 | 766 | | |
756 | 767 | | |
757 | 768 | | |
758 | | - | |
759 | 769 | | |
| 770 | + | |
760 | 771 | | |
761 | 772 | | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
762 | 778 | | |
763 | 779 | | |
764 | 780 | | |
| |||
813 | 829 | | |
814 | 830 | | |
815 | 831 | | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
816 | 835 | | |
817 | 836 | | |
818 | 837 | | |
| |||
931 | 950 | | |
932 | 951 | | |
933 | 952 | | |
934 | | - | |
935 | | - | |
| 953 | + | |
936 | 954 | | |
937 | 955 | | |
938 | 956 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
500 | 500 | | |
501 | 501 | | |
502 | 502 | | |
| 503 | + | |
503 | 504 | | |
504 | 505 | | |
505 | 506 | | |
| |||
520 | 521 | | |
521 | 522 | | |
522 | 523 | | |
| 524 | + | |
523 | 525 | | |
524 | 526 | | |
525 | 527 | | |
| |||
559 | 561 | | |
560 | 562 | | |
561 | 563 | | |
| 564 | + | |
562 | 565 | | |
563 | 566 | | |
564 | 567 | | |
| |||
587 | 590 | | |
588 | 591 | | |
589 | 592 | | |
| 593 | + | |
590 | 594 | | |
591 | 595 | | |
592 | 596 | | |
| |||
602 | 606 | | |
603 | 607 | | |
604 | 608 | | |
| 609 | + | |
605 | 610 | | |
606 | 611 | | |
607 | 612 | | |
| |||
616 | 621 | | |
617 | 622 | | |
618 | 623 | | |
| 624 | + | |
619 | 625 | | |
620 | 626 | | |
621 | 627 | | |
| |||
673 | 679 | | |
674 | 680 | | |
675 | 681 | | |
| 682 | + | |
676 | 683 | | |
677 | 684 | | |
678 | 685 | | |
| |||
683 | 690 | | |
684 | 691 | | |
685 | 692 | | |
| 693 | + | |
686 | 694 | | |
687 | 695 | | |
688 | 696 | | |
| |||
692 | 700 | | |
693 | 701 | | |
694 | 702 | | |
| 703 | + | |
695 | 704 | | |
696 | 705 | | |
697 | 706 | | |
| |||
701 | 710 | | |
702 | 711 | | |
703 | 712 | | |
| 713 | + | |
704 | 714 | | |
705 | 715 | | |
706 | 716 | | |
| |||
731 | 741 | | |
732 | 742 | | |
733 | 743 | | |
| 744 | + | |
734 | 745 | | |
735 | 746 | | |
736 | 747 | | |
| |||
739 | 750 | | |
740 | 751 | | |
741 | 752 | | |
| 753 | + | |
742 | 754 | | |
743 | 755 | | |
744 | 756 | | |
| |||
748 | 760 | | |
749 | 761 | | |
750 | 762 | | |
| 763 | + | |
751 | 764 | | |
752 | 765 | | |
753 | 766 | | |
| |||
766 | 779 | | |
767 | 780 | | |
768 | 781 | | |
| 782 | + | |
769 | 783 | | |
770 | 784 | | |
771 | 785 | | |
| |||
965 | 979 | | |
966 | 980 | | |
967 | 981 | | |
| 982 | + | |
968 | 983 | | |
969 | 984 | | |
970 | 985 | | |
| |||
973 | 988 | | |
974 | 989 | | |
975 | 990 | | |
| 991 | + | |
976 | 992 | | |
977 | 993 | | |
978 | 994 | | |
| |||
981 | 997 | | |
982 | 998 | | |
983 | 999 | | |
| 1000 | + | |
984 | 1001 | | |
985 | 1002 | | |
986 | 1003 | | |
| |||
1074 | 1091 | | |
1075 | 1092 | | |
1076 | 1093 | | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
1077 | 1125 | | |
| 1126 | + | |
0 commit comments