Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Properly disconnect socket io on exit #221

Merged
merged 24 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c480348
Initial implementation of the tracker
xnkevinnguyen Feb 20, 2020
f537d35
Create messaging service class
xnkevinnguyen Feb 21, 2020
10f483a
Update with latest debugger changes
xnkevinnguyen Feb 21, 2020
b616a7d
Send stacktrace message from adapter
xnkevinnguyen Feb 21, 2020
0dec72d
Establish connection for debug adapter and webview with the messaging…
xnkevinnguyen Feb 21, 2020
6dbfc9d
Debug adapter sends start and stop message to webview
xnkevinnguyen Feb 21, 2020
e58caf6
Remove changes on button component
xnkevinnguyen Feb 21, 2020
b6abfd5
Reformatting
xnkevinnguyen Feb 21, 2020
1f977c4
Pause input if on breakpoint
xnkevinnguyen Feb 22, 2020
da18aa7
Merge branch 'users/t-xunguy/dap' of https://github.com/microsoft/vsc…
xnkevinnguyen Feb 24, 2020
44daa13
Freeze buttons on svg
xnkevinnguyen Feb 24, 2020
9d3265e
added socket message for python process exit
andreamah Feb 25, 2020
c6e535e
Send message to python side on disconnect from vscode server side
xnkevinnguyen Feb 25, 2020
8cc304e
backend confirmation message
andreamah Feb 25, 2020
9fe8287
Confirm disconnect on python side
xnkevinnguyen Feb 25, 2020
0ad646b
Update with dev
xnkevinnguyen Feb 26, 2020
d82263b
Lint files
xnkevinnguyen Feb 26, 2020
eb417fe
Disconnect frontend after timeout
xnkevinnguyen Feb 26, 2020
c8f3b9d
Only disconnect socket io server on exit from port and not on restart
xnkevinnguyen Feb 27, 2020
b8d46b8
Merge branch 'dev' into users/t-anmah/debugger-socket-fix
xnkevinnguyen Feb 27, 2020
9e10e08
Format files
xnkevinnguyen Feb 27, 2020
04fb8ff
Merge branch 'users/t-anmah/debugger-socket-fix' of https://github.co…
xnkevinnguyen Feb 27, 2020
34be933
Remove unecessary logs
xnkevinnguyen Feb 27, 2020
5be7f32
Save previous debugger to disconnect
xnkevinnguyen Feb 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/common/debugger_communication_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ def input_changed(data):
@sio.on("received_state")
def received_state(data):
processing_state_event.set()


@sio.on("process_disconnect")
def process_disconnect(data):
sio.disconnect()
13 changes: 12 additions & 1 deletion src/debugger/debugAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { DebugAdapterTracker, DebugConsole, DebugSession } from "vscode";
import { DebuggerCommunicationService } from "../service/debuggerCommunicationService";
import { MessagingService } from "../service/messagingService";
import { DEBUG_COMMANDS } from "../view/constants";

export class DebugAdapter implements DebugAdapterTracker {
private readonly console: DebugConsole | undefined;
private readonly messagingService: MessagingService;
private debugCommunicationService: DebuggerCommunicationService;
constructor(
debugSession: DebugSession,
messagingService: MessagingService
messagingService: MessagingService,
debugCommunicationService: DebuggerCommunicationService
) {
this.console = debugSession.configuration.console;
this.messagingService = messagingService;
this.debugCommunicationService = debugCommunicationService;
}
onWillStartSession() {
// To Implement
Expand All @@ -24,6 +28,13 @@ export class DebugAdapter implements DebugAdapterTracker {
break;
case DEBUG_COMMANDS.STACK_TRACE:
this.messagingService.sendPauseMessage();
break;
case DEBUG_COMMANDS.DISCONNECT:
// Triggered on stop event for debugger
if (!message.arguments.restart) {
this.debugCommunicationService.handleStopEvent();
}
break;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also add a default case which would throw an UnsupportedOperationException. Up to you.

}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/debugger/debugAdapterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ import {
DebugSession,
ProviderResult,
} from "vscode";
import { DebuggerCommunicationService } from "../service/debuggerCommunicationService";
import { MessagingService } from "../service/messagingService";
import { DebugAdapter } from "./debugAdapter";

export class DebugAdapterFactory implements DebugAdapterTrackerFactory {
private debugSession: DebugSession;
private messagingService: MessagingService;
private debugCommunicationService: DebuggerCommunicationService;
constructor(
debugSession: DebugSession,
messagingService: MessagingService
messagingService: MessagingService,
debugCommunicationService: DebuggerCommunicationService
) {
this.debugSession = debugSession;
this.messagingService = messagingService;
this.debugCommunicationService = debugCommunicationService;
}
public createDebugAdapterTracker(
session: DebugSession
): ProviderResult<DebugAdapterTracker> {
return new DebugAdapter(session, this.messagingService);
return new DebugAdapter(
session,
this.messagingService,
this.debugCommunicationService
);
}
}
16 changes: 11 additions & 5 deletions src/debuggerCommunicationServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import * as socketio from "socket.io";
import { WebviewPanel } from "vscode";
import { SERVER_INFO } from "./constants";

const DEBUGGER_MESSAGES = {
export const DEBUGGER_MESSAGES = {
EMITTER: {
INPUT_CHANGED: "input_changed",
RECEIVED_STATE: "received_state",
DISCONNECT: "frontend_disconnected",
DISCONNECT: "process_disconnect",
},
LISTENER: {
UPDATE_STATE: "updateState",
Expand Down Expand Up @@ -45,10 +45,9 @@ export class DebuggerCommunicationServer {
this.currentActiveDevice = currentActiveDevice;
}

// send the message to start closing the connection
public closeConnection(): void {
this.serverIo.close();
this.serverHttp.close();
console.info("Closing the server");
this.sendDisconnectEvent();
}

public setWebview(webviewPanel: WebviewPanel | undefined) {
Expand All @@ -71,6 +70,13 @@ export class DebuggerCommunicationServer {
this.isPendingResponse = true;
}
}
public disconnectFromPort() {
this.serverIo.close();
this.serverHttp.close();
}
private sendDisconnectEvent() {
this.serverIo.emit(DEBUGGER_MESSAGES.EMITTER.DISCONNECT, {});
}

private initHttpServer(): void {
this.serverHttp.listen(this.port);
Expand Down
56 changes: 31 additions & 25 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { DebugAdapterFactory } from "./debugger/debugAdapterFactory";
import { DebuggerCommunicationServer } from "./debuggerCommunicationServer";
import * as utils from "./extension_utils/utils";
import { SerialMonitor } from "./serialMonitor";
import { DebuggerCommunicationService } from "./service/debuggerCommunicationService";
import { MessagingService } from "./service/messagingService";
import { SimulatorDebugConfigurationProvider } from "./simulatorDebugConfigurationProvider";
import TelemetryAI from "./telemetry/telemetryAI";
Expand All @@ -36,12 +37,12 @@ let telemetryAI: TelemetryAI;
let pythonExecutableName: string = "python";
let configFileCreated: boolean = false;
let inDebugMode: boolean = false;
let debuggerCommunicationHandler: DebuggerCommunicationServer;
// Notification booleans
let firstTimeClosed: boolean = true;
let shouldShowInvalidFileNamePopup: boolean = true;
let shouldShowRunCodePopup: boolean = true;
const messagingService = new MessagingService();
const debuggerCommunicationService = new DebuggerCommunicationService();

let currentActiveDevice: string = DEFAULT_DEVICE;

Expand Down Expand Up @@ -193,11 +194,11 @@ export async function activate(context: vscode.ExtensionContext) {
console.log(`About to write ${messageJson} \n`);
if (
inDebugMode &&
debuggerCommunicationHandler
debuggerCommunicationService.getCurrentDebuggerServer()
) {
debuggerCommunicationHandler.emitInputChanged(
messageJson
);
debuggerCommunicationService
.getCurrentDebuggerServer()
.emitInputChanged(messageJson);
} else if (childProcess) {
childProcess.stdin.write(
messageJson + "\n"
Expand Down Expand Up @@ -240,11 +241,11 @@ export async function activate(context: vscode.ExtensionContext) {
console.log(`Sensor changed ${messageJson} \n`);
if (
inDebugMode &&
debuggerCommunicationHandler
debuggerCommunicationService.getCurrentDebuggerServer()
) {
debuggerCommunicationHandler.emitInputChanged(
messageJson
);
debuggerCommunicationService
.getCurrentDebuggerServer()
.emitInputChanged(messageJson);
} else if (childProcess) {
childProcess.stdin.write(
messageJson + "\n"
Expand Down Expand Up @@ -283,8 +284,12 @@ export async function activate(context: vscode.ExtensionContext) {
currentPanel.onDidDispose(
() => {
currentPanel = undefined;
if (debuggerCommunicationHandler) {
debuggerCommunicationHandler.setWebview(undefined);
if (
debuggerCommunicationService.getCurrentDebuggerServer()
) {
debuggerCommunicationService
.getCurrentDebuggerServer()
.setWebview(undefined);
}
killProcessIfRunning();
if (firstTimeClosed) {
Expand Down Expand Up @@ -947,7 +952,8 @@ export async function activate(context: vscode.ExtensionContext) {

const debugAdapterFactory = new DebugAdapterFactory(
vscode.debug.activeDebugSession,
messagingService
messagingService,
debuggerCommunicationService
);
vscode.debug.registerDebugAdapterTrackerFactory(
"python",
Expand All @@ -958,27 +964,29 @@ export async function activate(context: vscode.ExtensionContext) {
if (simulatorDebugConfiguration.deviceSimulatorExpressDebug) {
// Reinitialize process
killProcessIfRunning();
console.log("Debug Started");
inDebugMode = true;

try {
// Shut down existing server on debug restart
if (debuggerCommunicationHandler) {
debuggerCommunicationHandler.closeConnection();
debuggerCommunicationHandler = undefined;
if (debuggerCommunicationService.getCurrentDebuggerServer()) {
debuggerCommunicationService.resetCurrentDebuggerServer();
}

debuggerCommunicationHandler = new DebuggerCommunicationServer(
currentPanel,
utils.getServerPortConfig(),
currentActiveDevice
debuggerCommunicationService.setCurrentDebuggerServer(
new DebuggerCommunicationServer(
currentPanel,
utils.getServerPortConfig(),
currentActiveDevice
)
);

handleDebuggerTelemetry();

openWebview();
if (currentPanel) {
debuggerCommunicationHandler.setWebview(currentPanel);
debuggerCommunicationService
.getCurrentDebuggerServer()
.setWebview(currentPanel);
currentPanel.webview.postMessage({
currentActiveDevice,
command: "activate-play",
Expand All @@ -1005,12 +1013,10 @@ export async function activate(context: vscode.ExtensionContext) {
// On Debug Session Stop: Stop communiation
const debugSessionStopped = vscode.debug.onDidTerminateDebugSession(() => {
if (simulatorDebugConfiguration.deviceSimulatorExpressDebug) {
console.log("Debug Stopped");
inDebugMode = false;
simulatorDebugConfiguration.deviceSimulatorExpressDebug = false;
if (debuggerCommunicationHandler) {
debuggerCommunicationHandler.closeConnection();
debuggerCommunicationHandler = undefined;
if (debuggerCommunicationService.getCurrentDebuggerServer()) {
debuggerCommunicationService.resetCurrentDebuggerServer();
}
if (currentPanel) {
currentPanel.webview.postMessage({
Expand Down
27 changes: 27 additions & 0 deletions src/service/debuggerCommunicationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DebuggerCommunicationServer } from "../debuggerCommunicationServer";

export class DebuggerCommunicationService {
private currentDebuggerServer?: DebuggerCommunicationServer;
private previousDebuggerServerToDisconnect?: DebuggerCommunicationServer;

public setCurrentDebuggerServer(debugServer: DebuggerCommunicationServer) {
this.currentDebuggerServer = debugServer;
}
// Used for restart and stop event
public resetCurrentDebuggerServer() {
if (this.currentDebuggerServer) {
this.currentDebuggerServer.closeConnection();
}
this.previousDebuggerServerToDisconnect = this.currentDebuggerServer;
this.currentDebuggerServer = undefined;
}
public getCurrentDebuggerServer() {
return this.currentDebuggerServer;
}
// Only used for stop event
public handleStopEvent() {
if (this.previousDebuggerServerToDisconnect) {
this.previousDebuggerServerToDisconnect.disconnectFromPort();
}
}
}
1 change: 1 addition & 0 deletions src/view/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export enum VSCODE_MESSAGES_TO_WEBVIEW {
export enum DEBUG_COMMANDS {
STACK_TRACE = "stackTrace",
CONTINUE = "continue",
DISCONNECT = "disconnect",
}
export enum SENSOR_LIST {
TEMPERATURE = "temperature",
Expand Down