network: fix ip caching bug

when the promise request errors, it is not cleared. this means that
future requests always fail.
This commit is contained in:
Girish Ramakrishnan
2025-10-17 12:37:46 +02:00
parent 8d2ea7e736
commit 084050bb2f

View File

@@ -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;
}