Fix device cleanup

Summary:
Fixes https://github.com/facebook/flipper/issues/1989

We had some self healing side effect that would destroy devices when registering a new device with the same serial, if they weren't yet. Redux isn't too happy about that, causing the attached crash.

Instead introduced a utility to destroy devices, and log an error if the device life cycle isn't respected by the device implementations, rather than crashing we will now just waste some memory.

Changelog: Fix a crash when disconnecting metro devices

Reviewed By: passy

Differential Revision: D26749214

fbshipit-source-id: 4c185ac521d44c1337fac8a9145440123b8b784c
This commit is contained in:
Michel Weststrate
2021-03-02 03:57:02 -08:00
committed by Facebook GitHub Bot
parent 4a71a5abd1
commit 224ec4d5d6
9 changed files with 49 additions and 42 deletions

View File

@@ -16,7 +16,7 @@ import type Client from '../Client';
import type {UninitializedClient} from '../UninitializedClient';
import {isEqual} from 'lodash';
import {performance} from 'perf_hooks';
import type {Actions} from '.';
import type {Actions, Store} from '.';
import {WelcomeScreenStaticView} from '../sandy-chrome/WelcomeScreen';
import {getPluginKey, isDevicePluginDefinition} from '../utils/pluginUtils';
import {deconstructClientId} from '../utils/clientUtils';
@@ -231,10 +231,9 @@ export default (state: State = INITAL_STATE, action: Actions): State => {
(device) => device.serial === payload.serial,
);
if (existing !== -1) {
console.debug(
console.warn(
`Got a new device instance for already existing serial ${payload.serial}`,
);
state.devices[existing].destroy();
newDevices[existing] = payload;
} else {
newDevices.push(payload);
@@ -255,7 +254,11 @@ export default (state: State = INITAL_STATE, action: Actions): State => {
if (!deviceSerials.has(device.serial)) {
return true;
} else {
device.destroy();
if (device.connected.get()) {
console.warn(
'Tried to unregister a device before it was destroyed',
);
}
return false;
}
});
@@ -654,3 +657,20 @@ export function isPluginEnabled(
const enabledAppPlugins = enabledPlugins[appInfo.app];
return enabledAppPlugins && enabledAppPlugins.indexOf(pluginId) > -1;
}
export function destroyDevice(store: Store, logger: Logger, serial: string) {
const device = store
.getState()
.connections.devices.find((device) => device.serial === serial);
if (device) {
device.destroy();
logger.track('usage', 'unregister-device', {
os: device.os,
serial,
});
store.dispatch({
type: 'UNREGISTER_DEVICES',
payload: new Set([serial]),
});
}
}