Repository navigation
fix(aibridge/intercept): close upstream response bodies in Responses interceptors - #30287
ibetitsmike wants to merge 3 commits into
Conversation
copyMiddleware hands the OpenAI SDK an io.NopCloser over a TeeReader of the upstream body, so readAll can still drain the body after the SDK closes its view. Nothing closed the real body, so each Responses API interception kept its upstream connection until the request context was canceled. Keep the real body on the copier and close it when the interception returns, before an agentic or failover iteration replaces the copier, and when an SDK retry passes a new response through the middleware. Context cancellation hides the leak on Go 1.26. Go 1.27.1 net/http regressions, fixed on release-branch.go1.27 for Go 1.27.2, stop hiding it, and the aibridge integration tests fail their goleak check.
|
@codex review
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 33bf54e203
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…re a retry SDK retries and centralized key failover run copyMiddleware again on the same copier, but it closed the previous upstream body only after the next attempt returned. The SDK cannot close that body itself through the NopCloser, so each retry held the earlier connection for the whole backoff and attempt. Close the previous body before calling next. The new test sends a retryable 500 through the real SDK and fails when the first upstream connection is still held while the retry is in flight.
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e1b46fadea
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
SDK retries and centralized-key failover reuse one responseCopier. Since the previous commit closes the earlier attempt's body before the next attempt, a retry that got no response left the copier pointing at that closed body. The interceptors then failed to read it, wrote nothing, and the client got an empty 200. copyMiddleware now clears the received flag and the buffer when an attempt starts, so a final attempt without a response takes the interceptors' custom-error path. Clearing the buffer also fixes blocking key failover, which forwarded the failed key's error body concatenated with the successful response.
|
@codex review
|
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
responseCopier.copyMiddlewarehands the OpenAI SDK anio.NopCloserover aTeeReaderof the upstream body, soreadAllcan drain the body after the SDK closes its view. Nothing closed the real body, so every Responses API interception kept its upstream connection until the request context was canceled.The copier now keeps the real body and closes it when the interception returns and before an agentic or failover iteration replaces the copier. SDK retries and key failover reuse one copier, so each new attempt first closes the previous body and clears what the copier recorded about it. Only the final attempt's response is forwarded, and a final attempt without a response gets the same 500 the interceptor already sends when the first attempt gets no response.
Clearing the buffer also fixes a bug already on main: after blocking key failover, the client received the failed key's error body followed by the successful response.
Stack context: second layer of the Go 1.27 upgrade. Context cancellation hides this leak on Go 1.26. Go 1.27.1 has net/http regressions that stop hiding it (golang/go#81411 and three related fixes, all backported to
release-branch.go1.27for Go 1.27.2), andaibridge/internal/integrationtestfails its goleak check. The fix is correct on any Go version, so it lands before the version bump.Testing
go test ./aibridge/...andgo test -race ./aibridge/intercept/...pass on Go 1.26.5, and on Go 1.27.1 with chore: bump Go to 1.27.1 #30288 on top.go test ./aibridge/internal/integrationtestfails goleak without this change and passes with it.TestResponseCopierReleasesUpstreamBeforeRetry,TestResponseCopierDiscardsPreviousAttempt, and the body check added toTestInterception_KeyFailovereach fail without the part of the change they cover.Review