Skip to content

Commit f1e1948

Browse files
authored
fix: reject run promise when close session (#1390)
1 parent c307280 commit f1e1948

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

client/src/components/notebook/Controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ export class NotebookController {
9898
]),
9999
]);
100100
execution.end(false, Date.now());
101+
if (!this._interrupted) {
102+
this._interrupted = deferred();
103+
}
101104
}
102105
if (this._interrupted) {
103106
this._interrupted.resolve();
104-
this._interrupted = undefined;
105107
}
106108
}
107109

client/src/connection/itc/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class ITCSession extends Session {
208208
* @param onLog A callback handler responsible for marshalling log lines back to the higher level extension API.
209209
* @returns A promise that eventually resolves to contain the given {@link RunResult} for the input code execution.
210210
*/
211-
public run = async (
211+
protected _run = async (
212212
code: string,
213213
skipPageHeaders?: boolean,
214214
): Promise<RunResult> => {
@@ -249,7 +249,7 @@ export class ITCSession extends Session {
249249
* Cleans up resources for the given SAS session.
250250
* @returns void promise.
251251
*/
252-
public close = async (): Promise<void> => {
252+
protected _close = async (): Promise<void> => {
253253
return new Promise((resolve) => {
254254
if (this._shellProcess) {
255255
this._shellProcess.stdin.write(

client/src/connection/rest/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class RestSession extends Session {
138138
updateStatusBarItem(true);
139139
};
140140

141-
public run = async (code: string) => {
141+
protected _run = async (code: string) => {
142142
if (!this._computeSession?.sessionId) {
143143
throw new Error();
144144
}
@@ -189,7 +189,7 @@ class RestSession extends Session {
189189
return res;
190190
};
191191

192-
public close = async () => {
192+
protected _close = async () => {
193193
if (this.sessionId()) {
194194
this._computeSession.delete();
195195
this._computeSession = undefined;

client/src/connection/session.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import { ProgressLocation, l10n, window } from "vscode";
44

5-
import { OnLogFn, RunResult } from ".";
5+
import type { OnLogFn, RunResult } from ".";
66

77
export abstract class Session {
8+
protected _rejectRun: (reason?: unknown) => void | undefined;
9+
810
protected _onSessionLogFn: OnLogFn | undefined;
911
public set onSessionLogFn(value: OnLogFn) {
1012
this._onSessionLogFn = value;
@@ -30,8 +32,27 @@ export abstract class Session {
3032
}
3133

3234
protected abstract establishConnection(): Promise<void>;
33-
abstract run(code: string): Promise<RunResult>;
35+
36+
run(code: string, ...args): Promise<RunResult> {
37+
return new Promise((resolve, reject) => {
38+
this._rejectRun = reject;
39+
this._run(code, ...args)
40+
.then(resolve, reject)
41+
.finally(() => (this._rejectRun = undefined));
42+
});
43+
}
44+
protected abstract _run(code: string, ...args): Promise<RunResult>;
45+
3446
cancel?(): Promise<void>;
35-
abstract close(): Promise<void> | void;
47+
48+
close(): Promise<void> | void {
49+
if (this._rejectRun) {
50+
this._rejectRun({ message: l10n.t("The SAS session has closed.") });
51+
this._rejectRun = undefined;
52+
}
53+
return this._close();
54+
}
55+
protected abstract _close(): Promise<void> | void;
56+
3657
abstract sessionId?(): string | undefined;
3758
}

client/src/connection/ssh/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class SSHSession extends Session {
112112
});
113113
};
114114

115-
public run = (code: string): Promise<RunResult> => {
115+
protected _run = (code: string): Promise<RunResult> => {
116116
this._html5FileName = "";
117117

118118
return new Promise((_resolve, _reject) => {
@@ -124,7 +124,7 @@ export class SSHSession extends Session {
124124
});
125125
};
126126

127-
public close = (): void | Promise<void> => {
127+
protected _close = (): void | Promise<void> => {
128128
if (!this._stream) {
129129
this.disposeResources();
130130
return;

client/test/connection/itc/Coderunner.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class MockSession extends Session {
2626
return;
2727
}
2828

29-
public async run(codeString: string): Promise<connection.RunResult> {
29+
protected async _run(codeString: string): Promise<connection.RunResult> {
3030
if (this._runMap) {
3131
const [, result] = Object.entries(this._runMap).find(([code]) =>
3232
codeString.includes(code),
@@ -45,7 +45,7 @@ export class MockSession extends Session {
4545
return {};
4646
}
4747

48-
public close(): void | Promise<void> {}
48+
protected _close(): void | Promise<void> {}
4949

5050
public sessionId?(): string {
5151
return "";

0 commit comments

Comments
 (0)