Fix disconnect during init logging

Summary:
When a client disconnects while it's still initialising, we get a critical error.
But this is expected to happen now and again, so adding a specific error for it, and handling it accordingly.
It doesn't change the control flow, just the logging.

Reviewed By: nikoant

Differential Revision: D32794062

fbshipit-source-id: cf27fb9c345aa6369d338838a46e75a6b7afde41
This commit is contained in:
John Knox
2021-12-03 06:27:13 -08:00
committed by Facebook GitHub Bot
parent 9fc1d3cfb9
commit b7a29ac0b3
4 changed files with 37 additions and 10 deletions

View File

@@ -34,6 +34,7 @@ export {
CancelledPromiseError, CancelledPromiseError,
UserUnauthorizedError, UserUnauthorizedError,
UserNotSignedInError, UserNotSignedInError,
NoLongerConnectedToClientError,
isConnectivityOrAuthError, isConnectivityOrAuthError,
isError, isError,
isAuthError, isAuthError,

View File

@@ -60,6 +60,14 @@ export class UserNotSignedInError extends Error {
name: 'UserNotSignedInError'; name: 'UserNotSignedInError';
} }
export class NoLongerConnectedToClientError extends Error {
constructor(msg: string = 'No longer connected to client.') {
super(msg);
this.name = 'NoLongerConnectedToClientError';
}
name: 'NoLongerConnectedToClientError';
}
declare global { declare global {
interface Error { interface Error {
interaction?: unknown; interaction?: unknown;

View File

@@ -15,7 +15,10 @@ import BaseDevice from './devices/BaseDevice';
import {Logger} from 'flipper-common'; import {Logger} from 'flipper-common';
import {Store} from './reducers/index'; import {Store} from './reducers/index';
import {performance} from 'perf_hooks'; import {performance} from 'perf_hooks';
import {reportPluginFailures} from 'flipper-common'; import {
reportPluginFailures,
NoLongerConnectedToClientError,
} from 'flipper-common';
import {default as isProduction} from './utils/isProduction'; import {default as isProduction} from './utils/isProduction';
import {EventEmitter} from 'events'; import {EventEmitter} from 'events';
import invariant from 'invariant'; import invariant from 'invariant';
@@ -509,7 +512,7 @@ export default class Client extends EventEmitter {
key: 'appnotconnectedwarning', key: 'appnotconnectedwarning',
duration: 0.5, duration: 0.5,
}); });
reject(new Error('Not connected to client')); reject(new NoLongerConnectedToClientError());
return; return;
} }
if (!fromPlugin || this.isAcceptingMessagesFromPlugin(plugin)) { if (!fromPlugin || this.isAcceptingMessagesFromPlugin(plugin)) {

View File

@@ -9,7 +9,11 @@
import React from 'react'; import React from 'react';
import {State, Store} from '../reducers/index'; import {State, Store} from '../reducers/index';
import {FlipperServer, Logger} from 'flipper-common'; import {
FlipperServer,
Logger,
NoLongerConnectedToClientError,
} from 'flipper-common';
import {FlipperServerImpl} from 'flipper-server-core'; import {FlipperServerImpl} from 'flipper-server-core';
import {selectClient} from '../reducers/connections'; import {selectClient} from '../reducers/connections';
import Client from '../Client'; import Client from '../Client';
@@ -245,13 +249,24 @@ export async function handleClientConnected(
type: 'NEW_CLIENT', type: 'NEW_CLIENT',
payload: client, payload: client,
}); });
try {
await timeout( await timeout(
30 * 1000, 30 * 1000,
client.init(), client.init(),
`[conn] Failed to initialize client ${query.app} on ${query.device_id} in a timely manner`, `[conn] Failed to initialize client ${query.app} on ${query.device_id} in a timely manner`,
); );
console.log(`[conn] ${query.app} on ${query.device_id} connected and ready.`); console.log(
`[conn] ${query.app} on ${query.device_id} connected and ready.`,
);
} catch (e) {
if (e instanceof NoLongerConnectedToClientError) {
console.warn(
`[conn] Client ${query.app} on ${query.device_id} disconnected while initialising`,
);
return;
}
throw e;
}
} }
function getDeviceBySerial( function getDeviceBySerial(