Skip to content

Commit

Permalink
Fixed removal of the AbortSignal event listener (#3227)
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar authored Jul 17, 2024
1 parent bc2df53 commit 0a59297
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/modules/network/implementation/libp2p-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,17 @@ class Libp2pService {
let readResponseStart;
let readResponseEnd;
let response;
const abortSignalEventListener = async () => {
stream.abort();
response = null;
};
const timeoutController = new TimeoutController(timeout);
try {
readResponseStart = Date.now();

timeoutController.signal.addEventListener(
'abort',
async () => {
stream.abort();
response = null;
},
{ once: true },
);
timeoutController.signal.addEventListener('abort', abortSignalEventListener, {
once: true,
});

response = await this._readMessageFromStream(
stream,
Expand All @@ -420,12 +419,12 @@ class Libp2pService {
throw Error('Message timed out!');
}

timeoutController.signal.removeEventListener('abort');
timeoutController.signal.removeEventListener('abort', abortSignalEventListener);
timeoutController.clear();

readResponseEnd = Date.now();
} catch (error) {
timeoutController.signal.removeEventListener('abort');
timeoutController.signal.removeEventListener('abort', abortSignalEventListener);
timeoutController.clear();

readResponseEnd = Date.now();
Expand Down

0 comments on commit 0a59297

Please sign in to comment.