[Service] Refactor stop service sequence 91/245591/4
authorDongHyun Song <dh81.song@samsung.com>
Tue, 13 Oct 2020 01:20:10 +0000 (10:20 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Tue, 13 Oct 2020 01:43:58 +0000 (10:43 +0900)
When service application needs to terminate by tizen://exit, it
should call wrt.stopService(id).

However, it is called in worker thread with standalone model,
WRTServiceManager::Remove() will be handled in worker thread, not
main thread. then, uv loop sometimes makes abort() or get SIGSEGV.
Then, wrt.stopService(id) should be called in main thread side.

Change-Id: I103bf8f38111db984bb22d0af56c05456851b681
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/common/service_manager.ts
wrt_app/common/service_runner.ts
wrt_app/service/builtins/wasm_builder.ts

index c4b2940..beb3d6e 100644 (file)
@@ -43,7 +43,13 @@ export function startService(id: string, filename: string) {
   console.log(`startService - ${id}`);
   if (isMainThread) {
     let startService = `${__dirname}/service_runner.js`;
-    createWorker(id, startService, filename);
+    let worker = createWorker(id, startService, filename);
+    worker.on('message', (message: any) => {
+      if (message.type === 'stop') {
+        console.log(`${id} will shutdown after ${message.delay}ms`);
+        setTimeout(() => wrt.stopService(id), message.delay);
+      }
+    });
   }
 }
 
@@ -64,8 +70,11 @@ export function handleBuiltinService(serviceId: string, serviceName: string) {
       console.log(`Builtin service is ${serviceName}`);
       let startService = `${__dirname}/../service/builtins/${serviceName}.js`;
       let worker = createWorker(serviceId, startService, '');
-      worker.on('stop', () => {
-        terminateWorker(serviceId, 0);
+      worker.on('message', (message: any) => {
+        if (message.type === 'stop') {
+          console.log(`${serviceName} built-in service will be stopped`);
+          terminateWorker(serviceId, message.delay);
+        }
       });
     }
   }
index 43681b5..c86829c 100644 (file)
@@ -31,13 +31,18 @@ function registerExtensionResolver(id: string) {
   }
 }
 
+function requestStopService(delay: number) {
+  if (parentPort) {
+    parentPort.postMessage({type: 'stop', delay: delay});
+  }
+}
+
 let app: any = null;
 export function start(id: string, filename: string) {
   XWalkExtension.initialize();
   XWalkExtension.setRuntimeMessageHandler((type, data) => {
     if (type === 'tizen://exit') {
-      console.log(`${id} will be closed by ${type}`);
-      setTimeout(() => wrt.stopService(id), 500);
+      requestStopService(500);
     }
   });
 
@@ -66,7 +71,7 @@ export function start(id: string, filename: string) {
     }
   } catch (e) {
     console.log(`exception on start: ${e}`);
-    setTimeout(() => wrt.stopService(id), 500);
+    requestStopService(500);
   }
 }
 
index 7ec30a3..6f6c5b1 100644 (file)
@@ -26,7 +26,7 @@ export function run(app_id: string) {
     compileWasmForCaching(file_path);
   });
   if (parentPort) {
-    parentPort.postMessage('stop');
+    parentPort.postMessage({type: 'stop', delay: 0});
   }
 }