Skip to content

Commit 4ebbb2d

Browse files
committed
Fix stopping trace server during shutdown
Fixes #13 Signed-off-by: Bernd Hufmann <[email protected]>
1 parent 0fceb92 commit 4ebbb2d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export function activate(context: vscode.ExtensionContext) {
1414
activation = context;
1515
}
1616

17-
export function deactivate() {
18-
server.stopOrReset(activation);
17+
export async function deactivate() {
18+
await server.shutdown(activation);
1919
}
2020

2121
function registerStopOrReset(context: vscode.ExtensionContext | undefined): vscode.Disposable {

src/trace-server.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const millis = 10000;
1313
const none = -1;
1414
const prefix = 'Trace Server';
1515
const suffix = ' failure or so.';
16+
const SHUTDOWN_DELAY = 2000;
1617

1718
export class TraceServer {
1819
private server: ChildProcess | undefined;
@@ -71,6 +72,24 @@ export class TraceServer {
7172
this.server = undefined;
7273
}
7374

75+
async shutdown(context: vscode.ExtensionContext) {
76+
// Take the pid from the server instead from workspace state because
77+
// during shutdown the workspace can't be queried anymore
78+
const pid = this.server ? this.server.pid : undefined;
79+
if (!pid) {
80+
return;
81+
}
82+
if (pid) {
83+
treeKill(pid);
84+
// Allow the treeKill to finish collecting and killing all
85+
// spawned processes.
86+
await new Promise(resolve => setTimeout(resolve, SHUTDOWN_DELAY));
87+
88+
// Try to reset pid in workspace state
89+
await context?.workspaceState.update(key, none);
90+
}
91+
}
92+
7493
async startIfStopped(context: vscode.ExtensionContext | undefined) {
7594
const pid = context?.workspaceState.get(key);
7695
const stopped = !pid || pid === none;

0 commit comments

Comments
 (0)