Skip to content

Commit

Permalink
Catch sync errors while starting HTTP server
Browse files Browse the repository at this point in the history
Reviewed By: lblasa

Differential Revision: D50636260

fbshipit-source-id: f23fb2005c779b87122ae34bfdd9371d8bdec8b4
  • Loading branch information
aigoncharov authored and facebook-github-bot committed Oct 25, 2023
1 parent 97f01d0 commit 8c5078d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 69 deletions.
64 changes: 34 additions & 30 deletions desktop/flipper-server-core/src/app-connectivity/ServerRSocket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,41 @@ class ServerRSocket extends ServerWebSocketBase {
start(port: number, sslConfig?: SecureServerConfig): Promise<number> {
const self = this;
return new Promise((resolve, reject) => {
// eslint-disable-next-line prefer-const
let rawServer: RSocketServer<any, any> | undefined;
const serverFactory = (onConnect: (socket: Socket) => void) => {
const transportServer = sslConfig
? tls.createServer(sslConfig, (socket) => {
onConnect(socket);
})
: net.createServer(onConnect);
transportServer.on('error', reject).on('listening', () => {
console.debug(
`${
sslConfig ? 'Secure' : 'Certificate'
} server started on port ${port}`,
'server',
);
self.listener.onListening(port);
self.rawServer_ = rawServer;
resolve((transportServer.address() as AddressInfo).port);
try {
// eslint-disable-next-line prefer-const
let rawServer: RSocketServer<any, any> | undefined;
const serverFactory = (onConnect: (socket: Socket) => void) => {
const transportServer = sslConfig
? tls.createServer(sslConfig, (socket) => {
onConnect(socket);
})
: net.createServer(onConnect);
transportServer.on('error', reject).on('listening', () => {
console.debug(
`${
sslConfig ? 'Secure' : 'Certificate'
} server started on port ${port}`,
'server',
);
self.listener.onListening(port);
self.rawServer_ = rawServer;
resolve((transportServer.address() as AddressInfo).port);
});
return transportServer;
};
rawServer = new RSocketServer({
getRequestHandler: sslConfig
? this._trustedRequestHandler
: this._untrustedRequestHandler,
transport: new RSocketTCPServer({
port: port,
serverFactory: serverFactory,
}),
});
return transportServer;
};
rawServer = new RSocketServer({
getRequestHandler: sslConfig
? this._trustedRequestHandler
: this._untrustedRequestHandler,
transport: new RSocketTCPServer({
port: port,
serverFactory: serverFactory,
}),
});
rawServer.start();
rawServer.start();
} catch (e) {
reject(e);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,50 @@ class ServerWebSocket extends ServerWebSocketBase {

async start(port: number, sslConfig?: SecureServerConfig): Promise<number> {
const assignedPort = await new Promise<number>((resolve, reject) => {
const server = sslConfig
? createHttpsServer(sslConfig)
: createHttpServer();

const wsServer = new WSServer({
server,
verifyClient: this.verifyClient(),
maxPayload: WEBSOCKET_MAX_MESSAGE_SIZE,
});

// We do not need to listen to http server's `error` because it is propagated to WS
// https://github.com/websockets/ws/blob/a3a22e4ed39c1a3be8e727e9c630dd440edc61dd/lib/websocket-server.js#L109
const onConnectionError = (error: Error) => {
reject(
new Error(
`Unable to start server at port ${port} due to ${JSON.stringify(
serializeError(error),
)}`,
),
);
};
wsServer.once('error', onConnectionError);
server.listen(port, () => {
console.debug(
`[ws] ${
sslConfig ? 'Secure' : 'Insecure'
} server started on port ${port}`,
'server',
);

// Unsubscribe connection error listener.
// We'll attach a permanent error listener later.
wsServer.off('error', onConnectionError);

this.listener.onListening(port);
this.wsServer = wsServer;
this.httpServer = server;
try {
const server = sslConfig
? createHttpsServer(sslConfig)
: createHttpServer();

const wsServer = new WSServer({
server,
verifyClient: this.verifyClient(),
maxPayload: WEBSOCKET_MAX_MESSAGE_SIZE,
});

resolve((server.address() as AddressInfo).port);
});
// We do not need to listen to http server's `error` because it is propagated to WS
// https://github.com/websockets/ws/blob/a3a22e4ed39c1a3be8e727e9c630dd440edc61dd/lib/websocket-server.js#L109
const onConnectionError = (error: Error) => {
reject(
new Error(
`Unable to start server at port ${port} due to ${JSON.stringify(
serializeError(error),
)}`,
),
);
};
wsServer.once('error', onConnectionError);
server.listen(port, () => {
console.debug(
`[ws] ${
sslConfig ? 'Secure' : 'Insecure'
} server started on port ${port}`,
'server',
);

// Unsubscribe connection error listener.
// We'll attach a permanent error listener later.
wsServer.off('error', onConnectionError);

this.listener.onListening(port);
this.wsServer = wsServer;
this.httpServer = server;

resolve((server.address() as AddressInfo).port);
});
} catch (e) {
reject(e);
}
});

assertNotNull(this.wsServer);
Expand Down

0 comments on commit 8c5078d

Please sign in to comment.