Skip to content

Commit bb929c4

Browse files
Always check pid before clearing workspace (#1321)
* Check pid before clear Ensure that we aren't clearing the workspace too eagerly. * Update rTerminal.ts * No need to specify pid again --------- Co-authored-by: Kun Ren <[email protected]>
1 parent 4f0da0d commit bb929c4

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/rTerminal.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ export function deleteTerminal(term: vscode.Terminal): void {
162162
if (isDeepStrictEqual(term, rTerm)) {
163163
rTerm = undefined;
164164
if (config().get<boolean>('sessionWatcher')) {
165-
cleanupSession();
165+
void term.processId.then((v) => {
166+
if (v) {
167+
cleanupSession(v.toString());
168+
}
169+
});
166170
}
167171
}
168172
}

src/session.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -777,15 +777,15 @@ async function updateRequest(sessionStatusBarItem: StatusBarItem) {
777777
if (request.plot_url) {
778778
await globalHttpgdManager?.showViewer(request.plot_url);
779779
}
780-
void watchProcess(Number(pid)).then((v) => {
781-
if (v === Number(pid)) {
782-
cleanupSession();
783-
}
780+
void watchProcess(pid).then((v: string) => {
781+
cleanupSession(v);
784782
});
785783
break;
786784
}
787785
case 'detach': {
788-
cleanupSession();
786+
if (request.pid) {
787+
cleanupSession(request.pid);
788+
}
789789
break;
790790
}
791791
case 'browser': {
@@ -826,19 +826,21 @@ async function updateRequest(sessionStatusBarItem: StatusBarItem) {
826826
}
827827
}
828828

829-
export function cleanupSession(): void {
830-
if (sessionStatusBarItem) {
831-
sessionStatusBarItem.text = 'R: (not attached)';
832-
sessionStatusBarItem.tooltip = 'Click to attach active terminal.';
829+
export function cleanupSession(pidArg: string): void {
830+
if (pid === pidArg) {
831+
if (sessionStatusBarItem) {
832+
sessionStatusBarItem.text = 'R: (not attached)';
833+
sessionStatusBarItem.tooltip = 'Click to attach active terminal.';
834+
}
835+
workspaceData.globalenv = {};
836+
workspaceData.loaded_namespaces = [];
837+
workspaceData.search = [];
838+
rWorkspace?.refresh();
839+
removeSessionFiles();
833840
}
834-
workspaceData.globalenv = {};
835-
workspaceData.loaded_namespaces = [];
836-
workspaceData.search = [];
837-
rWorkspace?.refresh();
838-
removeSessionFiles();
839841
}
840842

841-
async function watchProcess(pid: number): Promise<number> {
843+
async function watchProcess(pid: string): Promise<string> {
842844
function pidIsRunning(pid: number) {
843845
try {
844846
process.kill(pid, 0);
@@ -848,9 +850,11 @@ async function watchProcess(pid: number): Promise<number> {
848850
}
849851
}
850852

853+
const pidArg = Number(pid);
854+
851855
let res = true;
852856
do {
853-
res = pidIsRunning(pid);
857+
res = pidIsRunning(pidArg);
854858
await new Promise(resolve => {
855859
setTimeout(resolve, 1000);
856860
});

0 commit comments

Comments
 (0)