From 215cfc054cdf3738d0c3e8ed6c547de19e75e647 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Wed, 26 Jul 2017 20:59:01 -0300 Subject: [PATCH] Avoid an indefinite recursion that grows the call stack when reporting the current state fails We used to have a recursion based on Promises and Promise.delay, which caused the promise never to resolve so eventually the stack would be exhausted. This fixes it by using a simpler way to check if reporting the state is in progress and using a setImmediate to call applyState outside of the Promise chain. Change-Type: patch Signed-off-by: Pablo Carranza Velez --- src/device.coffee | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/device.coffee b/src/device.coffee index 3abf868cbf..36b1e869ed 100644 --- a/src/device.coffee +++ b/src/device.coffee @@ -164,7 +164,7 @@ exports.getDeviceType = memoizePromise -> do -> APPLY_STATE_SUCCESS_DELAY = 1000 APPLY_STATE_RETRY_DELAY = 5000 - applyPromise = Promise.resolve() + applyPending = false targetState = {} actualState = {} updateState = { update_pending: false, update_failed: false, update_downloaded: false } @@ -176,8 +176,10 @@ do -> applyState = -> stateDiff = getStateDiff() if _.size(stateDiff) is 0 + applyPending = false return - applyPromise = Promise.join( + applyPending = true + Promise.join( utils.getConfig('apiKey') device.getID() (apiKey, deviceID) -> @@ -202,7 +204,7 @@ do -> Promise.delay(APPLY_STATE_RETRY_DELAY) .finally -> # Check if any more state diffs have appeared whilst we've been processing this update. - applyState() + setImmediate(applyState) exports.setUpdateState = (value) -> _.merge(updateState, value) @@ -221,7 +223,7 @@ do -> _.merge(targetState, updatedState) # Only trigger applying state if an apply isn't already in progress. - if !applyPromise.isPending() + if !applyPending applyState() return