Skip to content

Commit 20c9174

Browse files
authored
Merge pull request #182 from NewtonDer/fix_idle_shutdown_1-6
Fix: Move checkTerminalActivity to server side
2 parents d25ffee + e1250b3 commit 20c9174

File tree

8 files changed

+113
-135
lines changed

8 files changed

+113
-135
lines changed

patched-vscode/build/npm/dirs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const dirs = [
4545
'extensions/sagemaker-terminal-crash-mitigation',
4646
'extensions/sagemaker-open-notebook-extension',
4747
'extensions/sagemaker-ui-dark-theme',
48-
'extensions/post-startup-notifications',
48+
'extensions/post-startup-notifications',
4949
'extensions/search-result',
5050
'extensions/simple-browser',
5151
'extensions/tunnel-forwarding',

patched-vscode/extensions/sagemaker-idle-extension/src/extension.ts

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@ import * as vscode from "vscode";
22
import * as fs from "fs";
33
import * as path from "path";
44

5-
let idleFilePath: string
6-
let terminalActivityInterval: NodeJS.Timeout | undefined
7-
const LOG_PREFIX = "[sagemaker-idle-extension]"
8-
const CHECK_INTERVAL = 60000; // 60 seconds interval
5+
let idleFilePath: string;
96

107
export function activate(context: vscode.ExtensionContext) {
118
initializeIdleFilePath();
129
registerEventListeners(context);
13-
startMonitoringTerminalActivity();
1410
}
1511

16-
export function deactivate() {
17-
if(terminalActivityInterval) {
18-
clearInterval(terminalActivityInterval)
19-
}
20-
}
12+
export function deactivate() {}
2113

2214
/**
2315
* Initializes the file path where the idle timestamp will be stored.
@@ -28,7 +20,7 @@ function initializeIdleFilePath() {
2820
idleFilePath = path.join(tmpDirectory, ".sagemaker-last-active-timestamp");
2921

3022
// Set initial lastActivetimestamp
31-
updateLastActivityTimestamp()
23+
updateLastActivityTimestamp();
3224
}
3325

3426
/**
@@ -56,52 +48,6 @@ function registerEventListeners(context: vscode.ExtensionContext) {
5648
);
5749
}
5850

59-
/**
60-
* Starts monitoring terminal activity by setting an interval to check for activity in the /dev/pts directory.
61-
*/
62-
const startMonitoringTerminalActivity = () => {
63-
terminalActivityInterval = setInterval(checkTerminalActivity, CHECK_INTERVAL);
64-
};
65-
66-
67-
/**
68-
* Checks for terminal activity by reading the /dev/pts directory and comparing modification times of the files.
69-
*
70-
* The /dev/pts directory is used in Unix-like operating systems to represent pseudo-terminal (PTY) devices.
71-
* Each active terminal session is assigned a PTY device. These devices are represented as files within the /dev/pts directory.
72-
* When a terminal session has activity, such as when a user inputs commands or output is written to the terminal,
73-
* the modification time (mtime) of the corresponding PTY device file is updated. By monitoring the modification
74-
* times of the files in the /dev/pts directory, we can detect terminal activity.
75-
*
76-
* If activity is detected (i.e., if any PTY device file was modified within the CHECK_INTERVAL), this function
77-
* updates the last activity timestamp.
78-
*/
79-
const checkTerminalActivity = () => {
80-
fs.readdir("/dev/pts", (err, files) => {
81-
if (err) {
82-
console.error(`${LOG_PREFIX} Error reading /dev/pts directory:`, err);
83-
return;
84-
}
85-
86-
const now = Date.now();
87-
const activityDetected = files.some((file) => {
88-
const filePath = path.join("/dev/pts", file);
89-
try {
90-
const stats = fs.statSync(filePath);
91-
const mtime = new Date(stats.mtime).getTime();
92-
return now - mtime < CHECK_INTERVAL;
93-
} catch (error) {
94-
console.error(`${LOG_PREFIX} Error reading file stats:`, error);
95-
return false;
96-
}
97-
});
98-
99-
if (activityDetected) {
100-
updateLastActivityTimestamp();
101-
}
102-
});
103-
};
104-
10551
/**
10652
* Updates the last activity timestamp by recording the current timestamp in the idle file and
10753
* refreshing the status bar. The timestamp should be in ISO 8601 format and set to the UTC timezone.

patched-vscode/src/vs/server/node/webClientServer.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,41 @@ export async function serveFile(filePath: string, cacheControl: CacheControl, lo
9999
}
100100
}
101101

102+
const CHECK_INTERVAL = 60000; // 60 seconds interval
102103
const APP_ROOT = dirname(FileAccess.asFileUri('').fsPath);
103104

105+
/**
106+
* Checks for terminal activity by reading the /dev/pts directory and comparing modification times of the files.
107+
*
108+
* The /dev/pts directory is used in Unix-like operating systems to represent pseudo-terminal (PTY) devices.
109+
* Each active terminal session is assigned a PTY device. These devices are represented as files within the /dev/pts directory.
110+
* When a terminal session has activity, such as when a user inputs commands or output is written to the terminal,
111+
* the modification time (mtime) of the corresponding PTY device file is updated. By monitoring the modification
112+
* times of the files in the /dev/pts directory, we can detect terminal activity.
113+
*
114+
* If activity is detected (i.e., if any PTY device file was modified within the CHECK_INTERVAL), this function
115+
* updates the last activity timestamp.
116+
*/
117+
function checkTerminalActivity(idleFilePath: string) {
118+
try {
119+
const files: string[] = fs.readdirSync('/dev/pts');
120+
const now = new Date();
121+
122+
const activityDetected = files.some((file: string) => {
123+
const filePath = path.join('/dev/pts', file);
124+
const stats = fs.statSync(filePath);
125+
const mtime = new Date(stats.mtime).getTime();
126+
return now.getTime() - mtime < CHECK_INTERVAL;
127+
});
128+
129+
if (activityDetected) {
130+
fs.writeFileSync(idleFilePath, now.toISOString());
131+
}
132+
} catch (err) {
133+
console.error('Error checking terminal activity:', err);
134+
}
135+
}
136+
104137
export class WebClientServer {
105138

106139
private readonly _webExtensionResourceUrlTemplate: URI | undefined;
@@ -481,6 +514,8 @@ export class WebClientServer {
481514
writeFileSync(idleFilePath, timestamp);
482515
}
483516

517+
checkTerminalActivity(idleFilePath);
518+
484519
const data = await readFile(idleFilePath, 'utf8');
485520

486521
res.statusCode = 200;

patches/custom-extensions-marketplace.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
9898
===================================================================
9999
--- sagemaker-code-editor.orig/vscode/src/vs/server/node/webClientServer.ts
100100
+++ sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
101-
@@ -331,14 +331,7 @@ export class WebClientServer {
101+
@@ -364,14 +364,7 @@ export class WebClientServer {
102102
const productConfiguration = {
103103
rootEndpoint: base,
104104
embedderIdentifier: 'server-distro',

patches/display-language.patch

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
304304
import { CharCode } from 'vs/base/common/charCode';
305305
import { IExtensionManifest } from 'vs/platform/extensions/common/extensions';
306306

307-
@@ -362,6 +363,8 @@ export class WebClientServer {
307+
@@ -395,6 +396,8 @@ export class WebClientServer {
308308
callbackRoute: this._callbackRoute
309309
};
310310

@@ -313,15 +313,15 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
313313
const nlsBaseUrl = this._productService.extensionsGallery?.nlsBaseUrl;
314314
const values: { [key: string]: string } = {
315315
WORKBENCH_WEB_CONFIGURATION: asJSON(workbenchWebConfiguration),
316-
@@ -370,6 +373,7 @@ export class WebClientServer {
316+
@@ -403,6 +406,7 @@ export class WebClientServer {
317317
WORKBENCH_NLS_BASE_URL: vscodeBase + (nlsBaseUrl ? `${nlsBaseUrl}${!nlsBaseUrl.endsWith('/') ? '/' : ''}${this._productService.commit}/${this._productService.version}/` : ''),
318318
BASE: base,
319319
VS_BASE: vscodeBase,
320320
+ NLS_CONFIGURATION: asJSON(nlsConfiguration),
321321
};
322322

323323
if (useTestResolver) {
324-
@@ -401,7 +405,7 @@ export class WebClientServer {
324+
@@ -434,7 +438,7 @@ export class WebClientServer {
325325
`frame-src 'self' https://*.vscode-cdn.net data:;`,
326326
'worker-src \'self\' data: blob:;',
327327
'style-src \'self\' \'unsafe-inline\';',

patches/sagemaker-extensions-sync.patch

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ Index: sagemaker-code-editor/vscode/build/gulpfile.extensions.js
99
+ 'extensions/sagemaker-extensions-sync/tsconfig.json',
1010
'extensions/sagemaker-terminal-crash-mitigation/tsconfig.json',
1111
'extensions/sagemaker-open-notebook-extension/tsconfig.json',
12-
'extensions/tunnel-forwarding/tsconfig.json',
12+
'extensions/sagemaker-ui-dark-theme/tsconfig.json',
1313
Index: sagemaker-code-editor/vscode/build/npm/dirs.js
1414
===================================================================
1515
--- sagemaker-code-editor.orig/vscode/build/npm/dirs.js
1616
+++ sagemaker-code-editor/vscode/build/npm/dirs.js
17-
@@ -40,6 +40,7 @@ const dirs = [
17+
@@ -40,11 +40,12 @@ const dirs = [
1818
'extensions/php-language-features',
1919
'extensions/references-view',
2020
'extensions/sagemaker-extension',
2121
+ 'extensions/sagemaker-extensions-sync',
2222
'extensions/sagemaker-idle-extension',
2323
'extensions/sagemaker-terminal-crash-mitigation',
2424
'extensions/sagemaker-open-notebook-extension',
25+
'extensions/sagemaker-ui-dark-theme',
26+
- 'extensions/post-startup-notifications',
27+
+ 'extensions/post-startup-notifications',
28+
'extensions/search-result',
29+
'extensions/simple-browser',
30+
'extensions/tunnel-forwarding',
2531
Index: sagemaker-code-editor/vscode/extensions/sagemaker-extensions-sync/.vscodeignore
2632
===================================================================
2733
--- /dev/null

0 commit comments

Comments
 (0)