This is an automated email from the ASF dual-hosted git repository.

rstrickland pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil-vscode.git


The following commit(s) were added to refs/heads/main by this push:
     new 1669f7b  Properly Dispose of Data Editor Session / Server
1669f7b is described below

commit 1669f7b01dfa7cc5c028eb1165cb7ad213097c3e
Author: stricklandrbls <[email protected]>
AuthorDate: Thu Jan 25 15:03:14 2024 -0600

    Properly Dispose of Data Editor Session / Server
    
    If the data editor panel was closed, or a DFDL debug session terminated,
    the omega edit server would not be properly shut down from the data
    editor side.
    
    - Implemented proper `dataEditorClient` & `WebviewPanel` disposal.
    
    Closes #898
---
 src/dataEditor/dataEditorClient.ts          | 36 ++++++++++++++++++-----------
 src/dataEditor/include/server/ServerInfo.ts | 10 +++++++-
 src/dataEditor/include/utils.ts             | 24 +++++++++++++++++++
 3 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/src/dataEditor/dataEditorClient.ts 
b/src/dataEditor/dataEditorClient.ts
index 1591f3d..3b357ef 100644
--- a/src/dataEditor/dataEditorClient.ts
+++ b/src/dataEditor/dataEditorClient.ts
@@ -41,7 +41,6 @@ import {
   getLogger,
   getServerHeartbeat,
   getServerInfo,
-  getSessionCount,
   getViewportData,
   IOFlags,
   IServerInfo,
@@ -82,9 +81,15 @@ import {
   HeartbeatInfo,
   IHeartbeatInfo,
 } from './include/server/heartbeat/HeartBeatInfo'
-import { configureOmegaEditPort, ServerInfo } from 
'./include/server/ServerInfo'
 import { extractDaffodilEvent } from '../daffodilDebugger/daffodil'
 import * as editor_config from './config'
+import {
+  configureOmegaEditPort,
+  NoSessionsExist,
+  ServerInfo,
+  ServerStopPredicate,
+} from './include/server/ServerInfo'
+import { isDFDLDebugSessionActive } from './include/utils'
 
 // 
*****************************************************************************
 // global constants
@@ -186,6 +191,7 @@ export class DataEditorClient implements vscode.Disposable {
 
     // destroy the session and remove it from the list of active sessions
     removeActiveSession(await destroySession(this.omegaSessionId))
+    await serverStop()
     this.panel.dispose()
   }
 
@@ -218,6 +224,10 @@ export class DataEditorClient implements vscode.Disposable 
{
     }, HEARTBEAT_INTERVAL_MS)
   }
 
+  sessionId(): string {
+    return this.omegaSessionId
+  }
+
   private async setupDataEditor() {
     assert(
       checkpointPath && checkpointPath.length > 0,
@@ -853,19 +863,17 @@ async function createDataEditorWebviewPanel(
 
   dataEditorView.panel.onDidDispose(
     async () => {
-      await dataEditorView.dispose()
-      // stop the server if the session count is zero
-      const sessionCount = await getSessionCount()
-      if (sessionCount === 0) {
-        assert(activeSessions.length === 0)
-
-        // stop the server
-        await serverStop()
-      }
+      removeActiveSession(await destroySession(dataEditorView.sessionId()))
+      await serverStopIf(NoSessionsExist)
     },
-    undefined,
+    dataEditorView,
     ctx.subscriptions
   )
+  if (isDFDLDebugSessionActive())
+    vscode.debug.onDidTerminateDebugSession(async () => {
+      removeActiveSession(await destroySession(dataEditorView.sessionId()))
+      await serverStopIf(NoSessionsExist)
+    })
 
   dataEditorView.show()
   return dataEditorView
@@ -1125,7 +1133,9 @@ function removeDirectory(dirPath: string): void {
     fs.rmdirSync(dirPath)
   }
 }
-
+async function serverStopIf(predicate: ServerStopPredicate) {
+  if (await predicate()) await serverStop()
+}
 async function serverStop() {
   const serverPidFile = getPidFile(omegaEditPort)
   if (fs.existsSync(serverPidFile)) {
diff --git a/src/dataEditor/include/server/ServerInfo.ts 
b/src/dataEditor/include/server/ServerInfo.ts
index 62440b9..85e24a5 100644
--- a/src/dataEditor/include/server/ServerInfo.ts
+++ b/src/dataEditor/include/server/ServerInfo.ts
@@ -15,10 +15,11 @@
  * limitations under the License.
  */
 
-import { IServerInfo } from '@omega-edit/client'
 import * as editor_config from '../../config'
 import * as fs from 'fs'
 import assert from 'assert'
+import { IServerInfo, getSessionCount } from '@omega-edit/client'
+
 export class ServerInfo implements IServerInfo {
   serverHostname: string = 'unknown'
   serverProcessId: number = 0
@@ -52,3 +53,10 @@ export function configureOmegaEditPort(configVars: 
editor_config.Config): void {
     assert(omegaEditPort !== 0, 'omegaEditPort is not set')
   }
 }
+export type ServerStopPredicate = (context?: any) => Promise<boolean>
+export const NoSessionsExist: ServerStopPredicate = (): Promise<boolean> => {
+  return new Promise(async (resolve) => {
+    const count = await getSessionCount()
+    resolve(count === 0)
+  })
+}
diff --git a/src/dataEditor/include/utils.ts b/src/dataEditor/include/utils.ts
new file mode 100644
index 0000000..c85aa71
--- /dev/null
+++ b/src/dataEditor/include/utils.ts
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import { debug } from 'vscode'
+
+export function isDFDLDebugSessionActive(): boolean {
+  return (
+    debug.activeDebugSession !== undefined &&
+    debug.activeDebugSession.type === 'dfdl'
+  )
+}

Reply via email to