Catch socket close during protocol handshake

zlib errors can cause the protocol handshake to not reject (until the
timeout).
This commit is contained in:
Asher 2021-04-20 15:48:24 -05:00
parent f0bafa387f
commit 16fc315afa
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 20 additions and 6 deletions

View File

@ -6,7 +6,9 @@ import { logger } from 'vs/server/node/logger';
import { enableCustomMarketplace } from 'vs/server/node/marketplace'; import { enableCustomMarketplace } from 'vs/server/node/marketplace';
import { Vscode } from 'vs/server/node/server'; import { Vscode } from 'vs/server/node/server';
setUnexpectedErrorHandler((error) => logger.warn(error instanceof Error ? error.message : error)); setUnexpectedErrorHandler((error) => {
logger.warn('Uncaught error', field('error', error instanceof Error ? error.message : error));
});
enableCustomMarketplace(); enableCustomMarketplace();
proxyAgent.monkeyPatch(true); proxyAgent.monkeyPatch(true);

View File

@ -54,10 +54,24 @@ export class Protocol extends PersistentProtocol {
*/ */
public handshake(): Promise<ConnectionTypeRequest> { public handshake(): Promise<ConnectionTypeRequest> {
this.logger.debug('Initiating handshake...'); this.logger.debug('Initiating handshake...');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const cleanup = () => {
handler.dispose();
onClose.dispose();
clearTimeout(timeout);
};
const onClose = this.onSocketClose(() => {
cleanup();
this.logger.debug('Handshake failed');
reject(new Error('Protocol socket closed unexpectedly'));
});
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
cleanup();
this.logger.debug('Handshake timed out'); this.logger.debug('Handshake timed out');
reject(new Error('protocol handshake timed out')); reject(new Error('Protocol handshake timed out'));
}, 10000); // Matches the client timeout. }, 10000); // Matches the client timeout.
const handler = this.onControlMessage((rawMessage) => { const handler = this.onControlMessage((rawMessage) => {
@ -69,16 +83,14 @@ export class Protocol extends PersistentProtocol {
case 'auth': case 'auth':
return this.authenticate(message); return this.authenticate(message);
case 'connectionType': case 'connectionType':
handler.dispose(); cleanup();
clearTimeout(timeout);
this.logger.debug('Handshake completed'); this.logger.debug('Handshake completed');
return resolve(message); return resolve(message);
default: default:
throw new Error('Unrecognized message type'); throw new Error('Unrecognized message type');
} }
} catch (error) { } catch (error) {
handler.dispose(); cleanup();
clearTimeout(timeout);
reject(error); reject(error);
} }
}); });