[Service] Terminate service apps one by one 13/267413/4
authorDongHyun Song <dh81.song@samsung.com>
Fri, 3 Dec 2021 02:26:23 +0000 (11:26 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Thu, 9 Dec 2021 03:58:29 +0000 (03:58 +0000)
When 2 apps are terminated at the same time, we faced lots of thread
safety issues.
This patch will terminate service apps one by one.

Change-Id: I96d91213b1b06968f598b01660d86cdaa11e8dd6
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/service/service_manager.ts

index fe3fd77473fa32968a1dc2def6e0cf891e0a42c7..c639eba3238ed692d0c59cc051fc96a8e572713f 100644 (file)
@@ -6,12 +6,24 @@ interface WorkerMap {
   [id: string]: any;
 }
 let workers: WorkerMap = {};
+let dyingWorkerQueue: WorkerMap = {};
 
 Object.defineProperty(global, 'serviceType', {
   value: wrt.getServiceModel(),
   writable: false
 });
 
+function checkDyingWorker() {
+  let dyingWorkers = Object.keys(dyingWorkerQueue);
+  if (dyingWorkers.length) {
+    let workerId = dyingWorkers[0];
+    if (dyingWorkerQueue[workerId] === 'will-terminate') {
+      dyingWorkerQueue[workerId] = 'terminated';
+      workers[workerId].terminate();
+    }
+  }
+}
+
 function createWorker(id: string, startService: string, filename: string) {
   if (workers[id]) {
     workers[id].postMessage({ type: 'wake' });
@@ -27,14 +39,17 @@ function createWorker(id: string, startService: string, filename: string) {
   });
   workers[id].on('message', (message: string) => {
     if (message === 'will-terminate') {
-      workers[id].terminate();
+      dyingWorkerQueue[id] = message;
+      checkDyingWorker();
     }
   });
   workers[id].on('exit', (code: number) => {
     wrt.tv?.serviceUmount(id);
+    delete dyingWorkerQueue[id];
     delete workers[id];
     let runningServices = Object.keys(workers);
-    console.debug(`exit code(${code}), remain services(${runningServices})`);
+    console.debug(`${id} terminated, remain services(${runningServices})`);
+    checkDyingWorker();
   });
 }