Skip to content

Commit e18a541

Browse files
authored
[video_player] Ignore late position updates after disposal (flutter#12971)
A platform seek can finish after `VideoPlayerController.dispose()`, causing `_updatePosition` to write to the disposed notifier and throw. This also happens in the controller's own `VideoEventType.completed` pause/seek chain, without the caller invoking a method after disposal. Checks `_isDisposed` at the shared position-update boundary. Adds regressions for an explicit pending seek and the pending seek from a playback-completed event. Both tests fail on the unmodified controller and pass with the guard. The patch updates the package to 2.14.1 and incorporates the existing `NEXT` changelog entry as required by the release instructions. Fixes flutter#193147 Validation with Flutter 3.44.9 / Dart 3.12.2: - Full Dart package suite: 118 passed, one existing skipped test. - `dart analyze --fatal-infos` for the package and example: no issues. - Repository Dart formatter run; unrelated pre-existing caption-test reformatting excluded from the diff. - Repository `publish-check`: passed after committing the four-file patch. - `git diff --check`: passed. Repository-wide `validate` could not complete in the sparse checkout because the root README references packages outside the checkout (`animations`). Native/integration tests were not run; this change is limited to the Dart controller. No public API or dartdoc changes are needed. ## Pre-Review Checklist
1 parent 7368361 commit e18a541

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎packages/video_player/video_player/CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.14.1
22

3+
* Fixes late position updates after a controller is disposed.
34
* Updates the example app to Robolectric 4.17.
45
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
56

‎packages/video_player/video_player/lib/video_player.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
978978
}
979979

980980
void _updatePosition(Duration position) {
981+
// A pending platform seek or position request can complete after disposal.
982+
if (_isDisposed) {
983+
return;
984+
}
981985
// The underlying native implementation on some platforms sometimes reports
982986
// a position slightly past the reported max duration. Clamp to the duration
983987
// to insulate clients from this behavior.

‎packages/video_player/video_player/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for displaying inline video with other Flutter
33
widgets on Android, iOS, macOS and web.
44
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
6-
version: 2.14.0
6+
version: 2.14.1
77

88
environment:
99
sdk: ^3.12.0

‎packages/video_player/video_player/test/video_player_test.dart‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,24 @@ void main() {
672672
});
673673

674674
group('seekTo', () {
675+
test('ignores a pending seek result after disposal', () async {
676+
final controller = VideoPlayerController.networkUrl(_localhostUri);
677+
addTearDown(controller.dispose);
678+
await controller.initialize();
679+
680+
final seekCompleter = Completer<void>();
681+
fakeVideoPlayerPlatform.seekCompleter = seekCompleter;
682+
final VideoPlayerValue valueBeforeSeek = controller.value;
683+
final Future<void> pendingSeek = controller.seekTo(const Duration(milliseconds: 500));
684+
expect(fakeVideoPlayerPlatform.calls.last, 'seekTo');
685+
686+
await controller.dispose();
687+
seekCompleter.complete();
688+
await pendingSeek;
689+
690+
expect(controller.value, valueBeforeSeek);
691+
});
692+
675693
test('works', () async {
676694
final controller = VideoPlayerController.networkUrl(_localhostUri);
677695
addTearDown(controller.dispose);
@@ -1456,6 +1474,28 @@ void main() {
14561474
});
14571475

14581476
group('Platform callbacks', () {
1477+
testWidgets('ignores a pending completion seek result after disposal', (
1478+
WidgetTester tester,
1479+
) async {
1480+
final controller = VideoPlayerController.networkUrl(_localhostUri);
1481+
addTearDown(controller.dispose);
1482+
await controller.initialize();
1483+
1484+
final seekCompleter = Completer<void>();
1485+
fakeVideoPlayerPlatform.seekCompleter = seekCompleter;
1486+
fakeVideoPlayerPlatform.streams[controller.playerId]!.add(
1487+
VideoEvent(eventType: VideoEventType.completed),
1488+
);
1489+
await tester.pump();
1490+
expect(fakeVideoPlayerPlatform.calls.last, 'seekTo');
1491+
final VideoPlayerValue valueBeforeDisposal = controller.value;
1492+
1493+
await tester.runAsync(controller.dispose);
1494+
seekCompleter.complete();
1495+
await tester.pump();
1496+
expect(controller.value, valueBeforeDisposal);
1497+
});
1498+
14591499
testWidgets('playing completed', (WidgetTester tester) async {
14601500
final controller = VideoPlayerController.networkUrl(_localhostUri);
14611501

@@ -2021,6 +2061,7 @@ void main() {
20212061

20222062
class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
20232063
Completer<bool> initialized = Completer<bool>();
2064+
Completer<void>? seekCompleter;
20242065
List<String> calls = <String>[];
20252066
List<DataSource> dataSources = <DataSource>[];
20262067
List<VideoViewType> viewTypes = <VideoViewType>[];
@@ -2108,6 +2149,7 @@ class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
21082149
@override
21092150
Future<void> seekTo(int playerId, Duration position) async {
21102151
calls.add('seekTo');
2152+
await seekCompleter?.future;
21112153
_positions[playerId] = position;
21122154
}
21132155

0 commit comments

Comments
 (0)