Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.socket.on('error', () => {
logger.verbose('Socket Error');
this.shutdown().ignoreErrors();
});
// Keep track of processid for killing it.
// Possible PTVSD died before sending message back.
try {
logger.verbose('Sending Terminated Event');
this.sendMessage(new TerminatedEvent(), this.outputStream);
} catch (err) {
const message = `Error in sending Terminated Event: ${err && err.message ? err.message : err.toString()}`;
const details = [message, err && err.name ? err.name : '', err && err.stack ? err.stack : ''].join(EOL);
logger.error(`${message}${EOL}${details}`);
}
this.terminatedEventSent = true;
}
if (!this.disconnectResponseSent && this.restart && this.disconnectRequest) {
// This is a work around for PTVSD bug, else this entire block is unnecessary.
try {
logger.verbose('Sending Disconnect Response');
this.sendMessage(new Response(this.disconnectRequest, ''), this.outputStream);
} catch (err) {
const message = `Error in sending Disconnect Response: ${err && err.message ? err.message : err.toString()}`;
const details = [message, err && err.name ? err.name : '', err && err.stack ? err.stack : ''].join(EOL);
logger.error(`${message}${EOL}${details}`);
}
this.disconnectResponseSent = true;
}
if (this.launchOrAttach === 'launch' && this.ptvsdProcessId) {
logger.verbose('killing process');
try {
// 1. Wait for some time, its possible the program has run to completion.
// We need to wait till the process exits (else the message `Terminated: 15` gets printed onto the screen).
// 2. Also, its possible we manually sent the `Terminated` event above.
// Hence we need to wait till VSC receives the above event.
private shutdown = async () => {
logger.verbose('check and shutdown');
if (this.hasShutdown) {
return;
}
this.hasShutdown = true;
logger.verbose('shutdown');
if (!this.terminatedEventSent && !this.restart) {
// Possible PTVSD died before sending message back.
try {
logger.verbose('Sending Terminated Event');
this.sendMessage(new TerminatedEvent(), this.outputStream);
} catch (err) {
const message = `Error in sending Terminated Event: ${err && err.message ? err.message : err.toString()}`;
const details = [message, err && err.name ? err.name : '', err && err.stack ? err.stack : ''].join(EOL);
logger.error(`${message}${EOL}${details}`);
}
private onResponseDisconnect = async () => {
this.disconnectResponseSent = true;
logger.verbose('onResponseDisconnect');
// When VS Code sends a disconnect request, PTVSD replies back with a response, but its upto us to kill the process.
// Wait for sometime, until the messages are sent out (remember, we're just intercepting streams here).
// Also its possible PTVSD might run to completion.
setTimeout(this.shutdown, 100);
}
}
if (!this.disconnectResponseSent && this.restart && this.disconnectRequest) {
// This is a work around for PTVSD bug, else this entire block is unnecessary.
try {
logger.verbose('Sending Disconnect Response');
this.sendMessage(new Response(this.disconnectRequest, ''), this.outputStream);
} catch (err) {
const message = `Error in sending Disconnect Response: ${err && err.message ? err.message : err.toString()}`;
const details = [message, err && err.name ? err.name : '', err && err.stack ? err.stack : ''].join(EOL);
logger.error(`${message}${EOL}${details}`);
}
this.disconnectResponseSent = true;
}
if (this.launchOrAttach === 'launch' && this.ptvsdProcessId) {
logger.verbose('killing process');
try {
// 1. Wait for some time, its possible the program has run to completion.
// We need to wait till the process exits (else the message `Terminated: 15` gets printed onto the screen).
// 2. Also, its possible we manually sent the `Terminated` event above.
// Hence we need to wait till VSC receives the above event.
await sleep(100);
logger.verbose('Kill process now');
killProcessTree(this.ptvsdProcessId!);
} catch { }
this.ptvsdProcessId = undefined;
}
if (!this.restart) {
if (this.debugSession) {
logger.verbose('Shutting down debug session');
this.debugSession.shutdown();
if (!this.disconnectResponseSent && this.restart && this.disconnectRequest) {
// This is a work around for PTVSD bug, else this entire block is unnecessary.
try {
logger.verbose('Sending Disconnect Response');
this.sendMessage(new Response(this.disconnectRequest, ''), this.outputStream);
} catch (err) {
const message = `Error in sending Disconnect Response: ${err && err.message ? err.message : err.toString()}`;
const details = [message, err && err.name ? err.name : '', err && err.stack ? err.stack : ''].join(EOL);
logger.error(`${message}${EOL}${details}`);
}
this.disconnectResponseSent = true;
}
if (this.launchOrAttach === 'launch' && this.ptvsdProcessId) {
logger.verbose('killing process');
try {
// 1. Wait for some time, its possible the program has run to completion.
// We need to wait till the process exits (else the message `Terminated: 15` gets printed onto the screen).
// 2. Also, its possible we manually sent the `Terminated` event above.
// Hence we need to wait till VSC receives the above event.
await sleep(100);
logger.verbose('Kill process now');
killProcessTree(this.ptvsdProcessId!);
} catch { }
this.ptvsdProcessId = undefined;
}
if (!this.restart) {
if (this.debugSession) {
logger.verbose('Shutting down debug session');
this.debugSession.shutdown();
private logCall(propertyKey: PropertyKey, synchronicity: Synchronicity, methodCallArguments: any[], outcome: Outcome, resultOrException: unknown, callId: number): void {
const endPrefix = callId ? `END ${callId}: ` : '';
const message = `${endPrefix}${this.printMethodCall(propertyKey, methodCallArguments)} ${this.printMethodSynchronicity(synchronicity)} ${this.printMethodResponse(outcome, resultOrException)}`;
logger.verbose(message);
}
makeRequest(options, cb: (resolve, reject, body) => void): Promise {
if (this.verbose) {
logger.verbose('req -> ' + JSON.stringify(options));
}
return new Promise((resolve, reject) => {
if (!this.estabilished) {
reject(Error('Connection is not estabilished'));
return;
}
if (typeof options.json === 'undefined') {
options.json = true;
}
request(Object.assign(this.getOptions(), options), (err, res, body) => {
if (err) {
return reject(err);
}
if (this.verbose) {
logger.verbose('req: ' + JSON.stringify(options));
request(Object.assign(this.getOptions(), options), (err, res, body) => {
if (err) {
return reject(err);
}
if (this.verbose) {
logger.verbose('req: ' + JSON.stringify(options));
logger.verbose('res: ' + JSON.stringify(body));
}
if (res.statusCode >= 400) {
return reject(new Error(res.statusMessage));
}
cb(resolve, reject, body);
});
});
private logSyncPartFinished(propertyKey: PropertyKey, _methodCallArguments: any[], callId: number): void {
const message = `SNRET ${callId}: ${this.printMethodName(propertyKey)}`;
logger.verbose(message);
}