-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(client): clean up abort listener in fetchWithTimeout to prevent memory leak #2151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -795,6 +795,13 @@ export class OpenAI { | |
|
|
||
| const security = options.__security ?? { bearerAuth: true }; | ||
| const controller = new AbortController(); | ||
| const abort = this._makeAbort(controller); | ||
| let abortCleanup: (() => void) | undefined; | ||
| if (options.signal) { | ||
| options.signal.addEventListener('abort', abort); | ||
| abortCleanup = () => options.signal!.removeEventListener('abort', abort); | ||
| } | ||
|
|
||
| const response = await this.fetchWithAuth(url, req, timeout, controller, security).catch(castToError); | ||
| const headersTime = Date.now(); | ||
|
|
||
|
|
@@ -949,7 +956,7 @@ export class OpenAI { | |
| }), | ||
| ); | ||
|
|
||
| return { response, options, controller, requestLogID, retryOfRequestLogID, startTime }; | ||
| return { response, options, controller, actionToRunOnComplete: abortCleanup, requestLogID, retryOfRequestLogID, startTime }; | ||
|
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. For successful requests that return a Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| getAPIList<Item, PageClass extends Pagination.AbstractPage<Item> = Pagination.AbstractPage<Item>>( | ||
|
|
@@ -1008,7 +1015,6 @@ export class OpenAI { | |
| ): Promise<Response> { | ||
| const { signal, method, ...options } = init || {}; | ||
| const abort = this._makeAbort(controller); | ||
| if (signal) signal.addEventListener('abort', abort, { once: true }); | ||
|
|
||
| const timeout = setTimeout(abort, ms); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ export class Stream<Item> implements AsyncIterable<Item> { | |
| controller: AbortController, | ||
| client?: OpenAI, | ||
| synthesizeEventData?: boolean, | ||
| cleanup?: () => void, | ||
|
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.
For Useful? React with 👍 / 👎. |
||
| ): Stream<Item> { | ||
| let consumed = false; | ||
| const logger = client ? loggerFor(client) : console; | ||
|
|
||
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.
Because this listener is installed for every attempt but only the final successfully parsed response returns its
actionToRunOnCompletetodefaultParseResponse, retry and terminal error paths inmakeRequestnever remove it. With a request-optionAbortSignal.timeout()that gets a retryable 429/500 (or a final API/connection error), the earlier attempt's listener stays attached and Deno can still remain alive until the timeout; call the cleanup in those paths beforeretryRequestor throwing.Useful? React with 👍 / 👎.