From dd79b18738cdb66f003a5195e78b5ff03248e27e Mon Sep 17 00:00:00 2001 From: DongHyun Song Date: Tue, 13 Oct 2020 10:20:10 +0900 Subject: [PATCH] [Service] Refactor stop service sequence 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 --- wrt_app/common/service_manager.ts | 15 ++++++++++++--- wrt_app/common/service_runner.ts | 11 ++++++++--- wrt_app/service/builtins/wasm_builder.ts | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/wrt_app/common/service_manager.ts b/wrt_app/common/service_manager.ts index c4b2940..beb3d6e 100644 --- a/wrt_app/common/service_manager.ts +++ b/wrt_app/common/service_manager.ts @@ -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); + } }); } } diff --git a/wrt_app/common/service_runner.ts b/wrt_app/common/service_runner.ts index 43681b5..c86829c 100644 --- a/wrt_app/common/service_runner.ts +++ b/wrt_app/common/service_runner.ts @@ -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); } } diff --git a/wrt_app/service/builtins/wasm_builder.ts b/wrt_app/service/builtins/wasm_builder.ts index 7ec30a3..6f6c5b1 100644 --- a/wrt_app/service/builtins/wasm_builder.ts +++ b/wrt_app/service/builtins/wasm_builder.ts @@ -26,7 +26,7 @@ export function run(app_id: string) { compileWasmForCaching(file_path); }); if (parentPort) { - parentPort.postMessage('stop'); + parentPort.postMessage({type: 'stop', delay: 0}); } } -- 2.7.4