Skip to content

Commit

Permalink
Fix a lock-up of the stats-getter
Browse files Browse the repository at this point in the history
For some reason, those promises never settle, so we end up stuck
trying to load users. Provide our own timeout on top of it, so that we
bail if this happens.
  • Loading branch information
tiennou committed Sep 30, 2024
1 parent c5c661e commit 6dd1b64
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/pushStats/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,45 @@ const SERVER_PORT = parseInt(/** @type {string} */ (process.env.SERVER_PORT), 10

/**
* Check whether there's a server nearby
* @returns {Promise<[string, number]>}
* @returns {Promise<[string, number] | undefined>}
*/
async function checkLocalhostServer() {
const hosts = [
'localhost',
'host.docker.internal',
'172.17.0.1',
];
/** @type {Promise<[string, number]>[]} */
/** @type {Promise<[string, number] | undefined>[]} */
const promises = [];
for (const host of hosts) {
const p = new Promise((resolve, reject) => {
const sock = new net.Socket();
sock.setTimeout(2500);
function kill() {
sock.removeAllListeners();
sock.end();
sock.destroy();
sock.unref();
}
sock.setTimeout(200);
console.log(`[${host}:${SERVER_PORT}] attempting connection`);
sock
.on('connect', () => {
sock.destroy();
.once('connect', () => {
kill();
resolve([host, SERVER_PORT]);
})
.on('error', () => {
sock.destroy();
reject();
.once('error', () => {
kill();
reject(new Error('Error connecting to server'));
})
.on('timeout', () => {
sock.destroy();
reject();
.once('timeout', () => {
kill();
reject(new Error('Timeout connecting to server'));
})
.connect(SERVER_PORT, host);
});
promises.push(p);
}
promises.push(new Promise((resolve) => { setTimeout(() => resolve(undefined), 1000); }));
return Promise.any(promises);
}

Expand Down Expand Up @@ -78,6 +85,9 @@ export default async function loadUsers() {
if (!user.host) {
try {
if (user.type === 'private') {
if (!localServer) {
throw new Error('no local server available, and host unspecified');
}
[user.host, user.port] = localServer;
} else {
[user.host, user.port] = getHostInfoFromType(user.type);
Expand All @@ -101,5 +111,6 @@ export default async function loadUsers() {
}
validUsers.push(user);
}
console.log(`Loaded ${validUsers.length} users (out of ${users.length})`);
return validUsers;
}

0 comments on commit 6dd1b64

Please sign in to comment.