fix: improve RADIUS client error handling and socket cleanup (#6783)

Co-authored-by: GitTensor Miner <miner@gittensor.io>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
dive2tech
2026-01-22 12:57:28 +00:00
committed by GitHub
co-authored by GitTensor Miner autofix-ci[bot]
parent a38c6dea25
commit 236d74238c
2 changed files with 43 additions and 13 deletions
+31 -11
View File
@@ -67,23 +67,47 @@ class RadiusClient {
let attempts = 0;
let responseReceived = false;
let timeoutHandle;
let socketClosed = false;
/**
* Safely close socket and clear timeout
* @returns {void}
*/
const cleanup = () => {
if (timeoutHandle) {
clearTimeout(timeoutHandle);
timeoutHandle = null;
}
if (!socketClosed) {
socketClosed = true;
try {
socket.close();
} catch (err) {
// Ignore errors during cleanup
}
}
};
/**
* Send RADIUS request with retry logic
* @returns {void}
*/
const sendRequest = () => {
if (responseReceived || socketClosed) {
return;
}
attempts++;
socket.send(encodedPacket, 0, encodedPacket.length, this.port, this.host, (err) => {
if (err) {
socket.close();
cleanup();
return reject(new Error(`Failed to send RADIUS request: ${err.message}`));
}
// Set timeout for this attempt
timeoutHandle = setTimeout(() => {
if (responseReceived) {
if (responseReceived || socketClosed) {
return;
}
@@ -92,7 +116,7 @@ class RadiusClient {
sendRequest();
} else {
// All retries exhausted
socket.close();
cleanup();
reject(new Error(`RADIUS request timeout after ${attempts} attempts`));
}
}, this.timeout);
@@ -101,23 +125,20 @@ class RadiusClient {
// Handle response
socket.on("message", (msg) => {
if (responseReceived) {
if (responseReceived || socketClosed) {
return;
}
responseReceived = true;
clearTimeout(timeoutHandle);
cleanup();
let response;
try {
response = radius.decode({ packet: msg, secret: secret });
} catch (error) {
socket.close();
return reject(new Error(`RADIUS response decoding failed: ${error.message}`));
}
socket.close();
// Map response code to match node-radius-client format
const responseCode = response.code;
@@ -140,10 +161,9 @@ class RadiusClient {
// Handle socket errors
socket.on("error", (err) => {
if (!responseReceived) {
if (!responseReceived && !socketClosed) {
responseReceived = true;
clearTimeout(timeoutHandle);
socket.close();
cleanup();
reject(new Error(`RADIUS socket error: ${err.message}`));
}
});