-
-
Notifications
You must be signed in to change notification settings - Fork 957
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix memory leak bug when re-using abortSignal
(#2160)
#2161
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,9 +251,8 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
this.destroy(new AbortError(this)); | ||
} | ||
|
||
this.options.signal?.addEventListener('abort', () => { | ||
this.destroy(new AbortError(this)); | ||
}); | ||
this.abortHandler = this.abortHandler.bind(this); | ||
this.options.signal?.addEventListener('abort', this.abortHandler, {once: true}); | ||
|
||
// Important! If you replace `body` in a handler with another stream, make sure it's readable first. | ||
// The below is run only once. | ||
|
@@ -545,6 +544,10 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
return this; | ||
} | ||
|
||
private abortHandler(): void { | ||
this.destroy(new AbortError(this)); | ||
} | ||
|
||
private async _finalizeBody(): Promise<void> { | ||
const {options} = this; | ||
const {headers} = options; | ||
|
@@ -664,6 +667,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
response.once('end', () => { | ||
this._responseSize = this._downloadedSize; | ||
this.emit('downloadProgress', this.downloadProgress); | ||
this.removeListener('abort', this.abortHandler); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be called on the signal instead |
||
}); | ||
|
||
response.once('error', (error: Error) => { | ||
|
@@ -674,6 +678,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
response.destroy(); | ||
|
||
this._beforeError(new ReadError(error, this)); | ||
this.removeListener('abort', this.abortHandler); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
|
||
response.once('aborted', () => { | ||
|
@@ -684,6 +689,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
message: 'The server aborted pending request', | ||
code: 'ECONNRESET', | ||
}, this)); | ||
this.removeListener('abort', this.abortHandler); | ||
}); | ||
|
||
this.emit('downloadProgress', this.downloadProgress); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit