Skip to content

Commit a5909a9

Browse files
tregula501claude
andcommitted
fix(android): dispose native player before activity finish to stop exit crash
Exiting the app with a live video texture crashed 5/5 times in testing: the engine's ImageReaderSurfaceProducer delivers leftover frames after FlutterJNI detaches and calls scheduleFrame on the dead engine (flutter/flutter#188300); a secondary variant aborts in the mpv core thread. Stopping playback is not enough - the queued texture frames fire during activity teardown regardless. Back-at-root on Android is now intercepted by a PopScope: the player is fully disposed (releasing the texture/SurfaceProducer) with a 2s timeout guard, then SystemNavigator.pop finishes the activity. Verified 0/5 crashes on the launch-play-exit loop that previously failed 5/5. Refs flutter/flutter#188300 Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 2e726cc commit a5909a9

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎lib/app.dart‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22

33
import 'package:flutter/material.dart';
4+
import 'package:flutter/services.dart';
45
import 'package:flutter_riverpod/flutter_riverpod.dart';
56

67
import 'core/themes/app_theme.dart';
@@ -195,7 +196,7 @@ class _FlowTVAppState extends ConsumerState<FlowTVApp>
195196
}
196197
// On mobile, use shortestSide so phones always stay on the mobile UI
197198
// (even in landscape) while tablets/foldables get the desktop layout.
198-
return Builder(
199+
final home = Builder(
199200
builder: (context) {
200201
final shortestSide = MediaQuery.of(context).size.shortestSide;
201202
if (shortestSide >= 600) {
@@ -204,6 +205,38 @@ class _FlowTVAppState extends ConsumerState<FlowTVApp>
204205
return const MobileHomeScreen();
205206
},
206207
);
208+
209+
if (!Platform.isAndroid) return home;
210+
211+
// Android: exiting while a video texture is alive crashes in the engine —
212+
// ImageReaderSurfaceProducer delivers leftover frames after FlutterJNI
213+
// detaches (flutter/flutter#188300). Intercept back-at-root, release the
214+
// native player (and its texture) first, then finish the activity.
215+
return PopScope(
216+
canPop: false,
217+
onPopInvokedWithResult: (didPop, _) {
218+
if (didPop) return;
219+
_exitAfterPlayerShutdown();
220+
},
221+
child: home,
222+
);
223+
}
224+
225+
bool _exiting = false;
226+
227+
Future<void> _exitAfterPlayerShutdown() async {
228+
if (_exiting) return;
229+
_exiting = true;
230+
try {
231+
await ref
232+
.read(playerControllerProvider.notifier)
233+
.shutdownForExit()
234+
.timeout(const Duration(seconds: 2));
235+
} catch (e) {
236+
// Never block exit on cleanup.
237+
AppLogger.warning('Player shutdown on exit failed: $e');
238+
}
239+
await SystemNavigator.pop();
207240
}
208241
}
209242

‎lib/presentation/providers/player_provider.dart‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,36 @@ class PlayerControllerNotifier extends Notifier<PlayerState> {
548548
}
549549
}
550550

551+
/// Fully release the native player before the Android activity finishes.
552+
///
553+
/// Exiting with a live video texture crashes the app: the engine's
554+
/// ImageReaderSurfaceProducer delivers leftover frames after FlutterJNI
555+
/// detaches (flutter/flutter#188300). Awaiting the player's disposal tears
556+
/// the texture down first so no callback can fire into the dead engine.
557+
Future<void> shutdownForExit() async {
558+
_isDisposed = true;
559+
_prebuffer.dispose();
560+
_retry.dispose();
561+
for (final sub in _subscriptions) {
562+
sub.cancel();
563+
}
564+
_subscriptions.clear();
565+
_currentChannel = null;
566+
567+
final player = _player;
568+
_player = null;
569+
_videoController = null;
570+
if (player != null) {
571+
await player.dispose();
572+
}
573+
574+
try {
575+
WakelockPlus.disable();
576+
} catch (_) {
577+
// App is exiting — nothing to do.
578+
}
579+
}
580+
551581
/// Refresh/reload the current channel (useful for frozen streams)
552582
Future<void> refreshChannel(Channel channel) async {
553583
try {

0 commit comments

Comments
 (0)