Avoid forwarding the data after socket is disconnected. (#146665)
In a ProxiedDevicePortForwarder, there might be a race condition where the local socket has been disconnected, but the remote end was still sending new data. In this case, avoid forwarding new data to the socket.
This commit is contained in:
@@ -633,11 +633,18 @@ class ProxiedPortForwarder extends DevicePortForwarder {
|
||||
'port': devicePort,
|
||||
}));
|
||||
final Stream<List<int>> dataStream = connection.listenToEvent('proxy.data.$id').asyncExpand((DaemonEventData event) => event.binary);
|
||||
dataStream.listen(socket.add);
|
||||
final StreamSubscription<List<int>> subscription = dataStream.listen(socket.add);
|
||||
final Future<DaemonEventData> disconnectFuture = connection.listenToEvent('proxy.disconnected.$id').first;
|
||||
|
||||
bool socketDoneCalled = false;
|
||||
|
||||
unawaited(disconnectFuture.then<void>((_) async {
|
||||
try {
|
||||
await socket.close();
|
||||
if (socketDoneCalled) {
|
||||
await subscription.cancel();
|
||||
} else {
|
||||
await (subscription.cancel(), socket.close()).wait;
|
||||
}
|
||||
} on Exception {
|
||||
// ignore
|
||||
}
|
||||
@@ -670,6 +677,8 @@ class ProxiedPortForwarder extends DevicePortForwarder {
|
||||
// Do nothing here. Everything will be handled in the `then` block below.
|
||||
return false;
|
||||
}).whenComplete(() {
|
||||
socketDoneCalled = true;
|
||||
unawaited(subscription.cancel());
|
||||
// Send a proxy disconnect event just in case.
|
||||
unawaited(connection.sendRequest('proxy.disconnect', <String, Object>{
|
||||
'id': id,
|
||||
|
||||
Reference in New Issue
Block a user