Skip to content

Commit

Permalink
Close socket after every query when running with Deno
Browse files Browse the repository at this point in the history
This should mean the socket doesn't hang in CLI or when using gamedig as
a library from Deno.
  • Loading branch information
Douile committed Sep 24, 2023
1 parent f4cc0e3 commit d8e60a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 0 additions & 6 deletions bin/gamedig.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,4 @@ gamedig.query(options)

printOnPretty({error: error});
}
})
.then(() => {
// https://github.com/denoland/deno/issues/20138
if (typeof Deno !== "undefined") {
gamedig.queryRunner.udpSocket?.socket?.close();
}
});
13 changes: 12 additions & 1 deletion lib/QueryRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,22 @@ export default class QueryRunner {
for (const attempt of attempts) {
for (let retry = 0; retry < numRetries; retry++) {
attemptNum++;
let result;
try {
return await this._attempt(attempt);
result = await this._attempt(attempt);
} catch (e) {
e.stack = 'Attempt #' + attemptNum + ' - Port=' + attempt.port + ' Retry=' + (retry) + ':\n' + e.stack;
errors.push(e);
} finally {
// Deno doesn't support unref, so we must close the socket after every connection
// https://github.com/denoland/deno/issues/20138
if (typeof Deno !== "undefined") {
this.udpSocket.socket.close();
delete this.udpSocket;
}
}
if (result) {
return result;
}
}
}
Expand Down

0 comments on commit d8e60a0

Please sign in to comment.