Skip to content

Commit 68ed60d

Browse files
committed
[flutter_tools] Guard getLogReader against synchronous connection disposal
Replaces `late final readerFuture` in `ExtensionBackedDevice.getLogReader` with a nullable future and disposal guard so synchronous startup failures on an already-disposed `ExtensionConnection` complete cleanly without throwing a `LateInitializationError` or caching a dead reader, and widens active app-scoped log streams to device-wide when a second application requests logs. Part of flutter#190755 Part of flutter#190750 Part of flutter#190753
1 parent 86e7282 commit 68ed60d

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

‎packages/flutter_tools/lib/src/experimental/extension_device_manager.dart‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ class ExtensionBackedDevice extends Device {
736736
if (existingReader.appId != resolvedAppId) {
737737
if (!existingReader.hasActiveSubscriber) {
738738
await existingReader.reconfigureAppId(resolvedAppId, clearPendingLines: true);
739-
} else if (existingReader.appId != null && resolvedAppId == null) {
739+
} else if (existingReader.appId != null) {
740740
await existingReader.reconfigureAppId(null);
741741
}
742742
}
@@ -748,7 +748,8 @@ class ExtensionBackedDevice extends Device {
748748
}
749749
}
750750

751-
late final Future<ExtensionDeviceLogReader> readerFuture;
751+
var readerDisposed = false;
752+
Future<ExtensionDeviceLogReader>? readerFuture;
752753
readerFuture = ExtensionDeviceLogReader.create(
753754
appId: resolvedAppId,
754755
connection: connection,
@@ -757,12 +758,15 @@ class ExtensionBackedDevice extends Device {
757758
logger: logger,
758759
name: name,
759760
onDispose: () {
760-
if (identical(_logReader, readerFuture)) {
761+
readerDisposed = true;
762+
if (readerFuture != null && identical(_logReader, readerFuture)) {
761763
_logReader = null;
762764
}
763765
},
764766
);
765-
_logReader = readerFuture;
767+
if (!readerDisposed) {
768+
_logReader = readerFuture;
769+
}
766770
final ExtensionDeviceLogReader reader = await readerFuture;
767771
if (_disposed) {
768772
await reader.dispose();

‎packages/flutter_tools/test/general.shard/extension_protocol/extension_device_test.dart‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,12 +1845,24 @@ void main() {
18451845
await pumpEventQueue();
18461846
expect(r1Closed, isFalse);
18471847

1848-
// Scenario C: Active app-scoped subscriber widens to device-wide (app: null) in place without closing.
1849-
final DeviceLogReader widened = await device.getLogReader();
1848+
// Scenario C: Active app-scoped subscriber widens to device-wide (app: null) in place when a second app is requested.
1849+
final secondAppPkg = ExtensionBackedApplicationPackage(id: 'com.example.other_app');
1850+
final DeviceLogReader widened = await device.getLogReader(app: secondAppPkg);
18501851
expect(identical(widened, concurrent[0]), isTrue);
18511852
expect(r1Closed, isFalse);
18521853
await pumpEventQueue();
18531854
expect(startedLogAppIds, <String?>[null, 'com.example.app', null]);
1855+
1856+
// Scenario D: Calling getLogReader after connection is already disposed handles synchronous failure without LateInitializationError.
1857+
await widened.dispose();
1858+
await conn.dispose();
1859+
logger.clear();
1860+
final DeviceLogReader postCloseReader = await device.getLogReader(app: appPkg);
1861+
expect((postCloseReader as ExtensionDeviceLogReader).isDisposed, isTrue);
1862+
expect(
1863+
logger.warningText,
1864+
contains('Failed to start log stream for extension device "ext-log-device"'),
1865+
);
18541866
});
18551867

18561868
testWithoutContext('handles !supportsInstall no-op semantics, catches RPC and peer-exit exceptions with printError, and propagates host errors', () async {

0 commit comments

Comments
 (0)