From ba8bf13cb6ae8cf3f4ac78153953d7afef14759d Mon Sep 17 00:00:00 2001 From: NguyenTranHoangSym Date: Thu, 20 Jul 2023 11:21:31 +0700 Subject: [PATCH] SDA-4182_racing: Add racing condition --- src/app/registry-handler.ts | 39 +++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/app/registry-handler.ts b/src/app/registry-handler.ts index c4f29074..02d55387 100644 --- a/src/app/registry-handler.ts +++ b/src/app/registry-handler.ts @@ -12,24 +12,22 @@ export const retrieveWindowsRegistry = async (): Promise => { const Registry = require('winreg'); const registryLocalStore = RegistryStore; const fetchLogic = (err, channel) => { - if (err) { - logger.info('registry-handler: error occurred. Details: ', err); + if (err || !channel) { + logger.error('registry-handler: error occurred. Details: ', err); - return 'An error has occurred'; + return ''; } else { - if (channel.type === RegistryValueType.REG_SZ) { + if (channel?.type === RegistryValueType.REG_SZ) { registryLocalStore.setRegistry({ currentChannel: channel.value }); logger.info( 'registry-handler: value retrieved successfully, send to Registry Store', ); - return channel.value; } else { logger.info( 'registry-handler: the value was looked for did not exist or its VALUE_TYPE is incorrect', ); - - return 'Key Value doesnt exist'; + return ''; } } }; @@ -44,18 +42,21 @@ export const retrieveWindowsRegistry = async (): Promise => { key: CHANNEL_NEST_LOCATION, }); - return regKeyUser.get(CHANNEL_KEY, (error, channel) => { - if (error && !channel) { - regKeyLocal.get(CHANNEL_KEY, (err, localChannel) => { - return fetchLogic(err, localChannel); - }); - } else if (channel.type === RegistryValueType.REG_SZ) { - registryLocalStore.setRegistry({ currentChannel: channel.value }); - logger.info( - 'registry-handler: value retrieved successfully, send to Registry Store', - ); + return new Promise((resolve) => { + regKeyUser.get(CHANNEL_KEY, (error, channel) => { + if (error || !channel) { + logger.error('registry-handler: error occurred. Details: ', error); - return channel; - } + regKeyLocal.get(CHANNEL_KEY, (err, localChannel) => { + resolve(fetchLogic(err, localChannel)); + }); + } else if (channel.type === RegistryValueType.REG_SZ) { + logger.info( + 'registry-handler: value retrieved successfully, send to Registry Store', + ); + registryLocalStore.setRegistry({ currentChannel: channel.value }); + resolve(channel.value); + } + }); }); };