diff --git a/src/network/generic.js b/src/network/generic.js index 101ea1771..fc403c688 100644 --- a/src/network/generic.js +++ b/src/network/generic.js @@ -29,7 +29,7 @@ async function getIP(type) { throw new BoxError(BoxError.EXTERNAL_ERROR, `Unable to detect ${type}. API server (${type}.api.cloudron.io) unreachable`); } - if (!response.body && !response.body.ip) { + if (!response.body?.ip) { debug('get: Unexpected answer. No "ip" found in response body.', response.body); throw new BoxError(BoxError.EXTERNAL_ERROR, `Unable to detect ${type}. No IP found in response`); } @@ -46,8 +46,14 @@ async function getIPv4(config) { if (gCache.ipv4.value && (Date.now() - gCache.ipv4.timestamp <= 5 * 60 * 1000)) return gCache.ipv4.value; if (!gCache.ipv4.request) gCache.ipv4.request = getIP('ipv4'); - await gCache.ipv4.request; - gCache.ipv4.request = null; + + const thisRequest = gCache.ipv4.request; + try { + await thisRequest; + } finally { // when we have multiple waiters, protect against a new caller creating a new request + if (thisRequest === gCache.ipv4.request) gCache.ipv4.request = null; + } + return gCache.ipv4.value; } @@ -59,8 +65,14 @@ async function getIPv6(config) { if (gCache.ipv6.value && (Date.now() - gCache.ipv6.timestamp <= 5 * 60 * 1000)) return gCache.ipv6.value; if (!gCache.ipv6.request) gCache.ipv6.request = getIP('ipv6'); - await gCache.ipv6.request; - gCache.ipv6.request = null; + + const thisRequest = gCache.ipv6.request; + try { + await thisRequest; + } finally { // when we have multiple waiters, protect against a new caller creating a new request + if (thisRequest === gCache.ipv6.request) gCache.ipv6.request = null; + } + return gCache.ipv6.value; }