Skip to content

Commit 8fb1cef

Browse files
authored
Merge pull request #45 from aws-asolidu/1.3-terminal-crash-mitigation
Add sagemaker-terminal-crash-mitigation extension
2 parents d9a3546 + dd2b983 commit 8fb1cef

File tree

12 files changed

+490
-0
lines changed

12 files changed

+490
-0
lines changed

patched-vscode/build/gulpfile.extensions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const compilations = [
6262
'extensions/simple-browser/tsconfig.json',
6363
'extensions/sagemaker-extension/tsconfig.json',
6464
'extensions/sagemaker-idle-extension/tsconfig.json',
65+
'extensions/sagemaker-terminal-crash-mitigation/tsconfig.json',
6566
'extensions/tunnel-forwarding/tsconfig.json',
6667
'extensions/typescript-language-features/test-workspace/tsconfig.json',
6768
'extensions/typescript-language-features/web/tsconfig.json',

patched-vscode/build/npm/dirs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const dirs = [
4141
'extensions/references-view',
4242
'extensions/sagemaker-extension',
4343
'extensions/sagemaker-idle-extension',
44+
'extensions/sagemaker-terminal-crash-mitigation',
4445
'extensions/search-result',
4546
'extensions/simple-browser',
4647
'extensions/tunnel-forwarding',
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.vscode/**
2+
.vscode-test/**
3+
out/test/**
4+
out/**
5+
test/**
6+
src/**
7+
tsconfig.json
8+
out/test/**
9+
out/**
10+
cgmanifest.json
11+
yarn.lock
12+
preview-src/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Terminal Crash Mitigation
2+
This extension addresses a critical issue where terminals fail to open. As of August 9, 2024, the root cause remains unidentified. The extension works by monitoring the creation of new terminals and detects if a terminal closes within 1 second of being opened. When this condition is met, it assumes the issue has occurred and attempts to mitigate it by terminating any background terminal processes. However, it will not terminate any terminal processes if there is an active terminal in the UI.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright Amazon.com Inc. or its affiliates. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
//@ts-check
7+
8+
'use strict';
9+
10+
const withBrowserDefaults = require('../shared.webpack.config').browser;
11+
12+
module.exports = withBrowserDefaults({
13+
context: __dirname,
14+
entry: {
15+
extension: './src/extension.ts'
16+
},
17+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright Amazon.com Inc. or its affiliates. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
//@ts-check
7+
8+
'use strict';
9+
10+
const withDefaults = require('../shared.webpack.config');
11+
12+
module.exports = withDefaults({
13+
context: __dirname,
14+
resolve: {
15+
mainFields: ['module', 'main']
16+
},
17+
entry: {
18+
extension: './src/extension.ts',
19+
}
20+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "sagemaker-terminal-crash-mitigation",
3+
"displayName": "Sagemaker terminal crash mitigation",
4+
"description": "Mitgate issue where the terminal crashes when trying to open on app startup",
5+
"extensionKind": [
6+
"workspace"
7+
],
8+
"version": "1.0.0",
9+
"publisher": "sagemaker",
10+
"license": "MIT",
11+
"engines": {
12+
"vscode": "^1.70.0"
13+
},
14+
"main": "./out/extension",
15+
"categories": [
16+
"Other"
17+
],
18+
"activationEvents": [
19+
"*"
20+
],
21+
"capabilities": {
22+
"virtualWorkspaces": true,
23+
"untrustedWorkspaces": {
24+
"supported": true
25+
}
26+
},
27+
"contributes": {
28+
"configuration": {
29+
"type": "object",
30+
"title": "Sagemaker Idle Extension",
31+
"properties": {}
32+
},
33+
"commands": []
34+
},
35+
"scripts": {
36+
"compile": "gulp compile-extension:sagemaker-terminal-crash-mitigation",
37+
"watch": "npm run build-preview && gulp watch-extension:sagemaker-terminal-crash-mitigation",
38+
"vscode:prepublish": "npm run build-ext",
39+
"build-ext": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:sagemaker-terminal-crash-mitigation ./tsconfig.json"
40+
},
41+
"dependencies": {},
42+
"repository": {}
43+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import * as vscode from 'vscode';
2+
import { exec } from 'child_process';
3+
4+
const logPrefix = '[sagemaker-terminal-crash-mitigation]';
5+
6+
export function activate(_context: vscode.ExtensionContext) {
7+
let lastTerminal: vscode.Terminal | undefined;
8+
let lastProcessId: number | undefined;
9+
let lastOpenedTime: number | undefined;
10+
11+
/**
12+
* Event listener for when a new terminal is opened.
13+
* Tracks the terminal's process ID and the time it was opened.
14+
*/
15+
vscode.window.onDidOpenTerminal(async terminal => {
16+
lastTerminal = terminal;
17+
lastOpenedTime = Date.now();
18+
try {
19+
lastProcessId = await terminal.processId;
20+
console.log(`${logPrefix} Terminal opened: PID ${lastProcessId}, Time: ${lastOpenedTime}`);
21+
} catch (error) {
22+
console.error(`${logPrefix} Error getting process ID: ${error}`);
23+
}
24+
});
25+
26+
/**
27+
* Event listener for when a terminal is closed.
28+
* Checks if the closed terminal is the one that was last opened,
29+
* and if it closed within 1 second. If no other terminals are active,
30+
* executes a command to kill all bash processes.
31+
*/
32+
vscode.window.onDidCloseTerminal(async terminal => {
33+
if (lastTerminal && lastProcessId && lastOpenedTime) {
34+
try {
35+
const currentProcessId = await terminal.processId;
36+
console.log(`${logPrefix} Terminal closed: PID ${currentProcessId}`);
37+
38+
if (currentProcessId === lastProcessId) {
39+
const timeElapsed = Date.now() - lastOpenedTime;
40+
console.log(`${logPrefix} Time elapsed since opening: ${timeElapsed}ms`);
41+
42+
if (timeElapsed < 1000) {
43+
const remainingTerminals = vscode.window.terminals.length;
44+
console.log(`${logPrefix} Number of remaining terminals: ${remainingTerminals}`);
45+
46+
if (remainingTerminals === 0) {
47+
console.log(`${logPrefix} No other active terminals. Executing kill command.`);
48+
execKillCommand();
49+
} else {
50+
console.log(`${logPrefix} There are other active terminals. Kill command not executed.`);
51+
}
52+
} else {
53+
console.log(`${logPrefix} Terminal closed after 1 second. No action taken.`);
54+
}
55+
} else {
56+
console.log(`${logPrefix} Closed terminal PID does not match last opened terminal PID. No action taken.`);
57+
}
58+
} catch (error) {
59+
console.error(`${logPrefix} Error getting process ID on close: ${error}`);
60+
}
61+
}
62+
});
63+
}
64+
65+
66+
/**
67+
* Executes the command to kill all bash processes.
68+
* Fetches all bash process IDs and sends a `kill -9` signal to each one.
69+
*/
70+
function execKillCommand() {
71+
exec("ps -eo pid,comm | grep bash | awk '{print $1}'", (error, stdout, stderr) => {
72+
if (error) {
73+
console.error(`${logPrefix} Error fetching bash PIDs: ${error.message}`);
74+
return;
75+
}
76+
if (stderr) {
77+
console.error(`${logPrefix} Error in command output: ${stderr}`);
78+
return;
79+
}
80+
81+
const pids = stdout.trim().split('\n').filter(pid => pid);
82+
if (pids.length === 0) {
83+
console.log(`${logPrefix} No bash processes found to kill.`);
84+
return;
85+
}
86+
87+
pids.forEach(pid => {
88+
exec(`kill -9 ${pid}`, (killError, _killStdout, killStderr) => {
89+
if (killError) {
90+
console.error(`${logPrefix} Error killing PID ${pid}: ${killError.message}`);
91+
return;
92+
}
93+
if (killStderr) {
94+
console.error(`${logPrefix} Error output while killing PID ${pid}: ${killStderr}`);
95+
return;
96+
}
97+
console.log(`${logPrefix} Killed bash process with PID ${pid}.`);
98+
});
99+
});
100+
});
101+
}
102+
103+
export function deactivate() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "./out"
5+
},
6+
"include": [
7+
"../sagemaker-terminal-crash-mitigation/src/**/*",
8+
"../../src/vscode-dts/vscode.d.ts"
9+
]
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

0 commit comments

Comments
 (0)