Skip to content

Commit 096cd23

Browse files
fix(github-copilot): cancel unread device-flow error bodies before throwing (#112268)
* fix(github-copilot): cancel unread device-flow error bodies before throwing postGitHubDeviceFlowForm now drops the unread response stream before it raises on a non-OK HTTP status. GitHub OAuth error responses ship a JSON body (`{error, error_description}`) that this helper never consumes, so the underlying fetch connection previously stayed open holding the payload until the caller's release() ran, keeping the device-flow socket half-drained on every 4xx/5xx. Mirrors the recently-merged qqbot/kilocode/tlon fixes for the same 'cancel body before throwing' pattern (#110008, #109950, #112059). * style(github-copilot): clarify response release ordering --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 397e09e commit 096cd23

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎extensions/github-copilot/login.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,35 @@ describe("runGitHubCopilotDeviceFlow — HTTP error propagation", () => {
130130
);
131131
});
132132

133+
it("cancels the unread device code body before throwing on non-OK", async () => {
134+
let canceled = false;
135+
const body = new ReadableStream<Uint8Array>({
136+
start(controller) {
137+
controller.enqueue(
138+
new TextEncoder().encode(
139+
JSON.stringify({ error: "server_error", error_description: "boom" }),
140+
),
141+
);
142+
},
143+
cancel() {
144+
canceled = true;
145+
},
146+
});
147+
mocks.fetchWithSsrFGuard.mockImplementation(async () => ({
148+
response: new Response(body, {
149+
status: 502,
150+
headers: { "Content-Type": "application/json" },
151+
}),
152+
finalUrl: DEVICE_CODE_URL,
153+
release: vi.fn(async () => {}),
154+
}));
155+
156+
await expect(runGitHubCopilotDeviceFlow({ showCode: vi.fn() })).rejects.toThrow(
157+
"GitHub device code failed: HTTP 502",
158+
);
159+
expect(canceled).toBe(true);
160+
});
161+
133162
it("throws with failureLabel on non-OK access token response", async () => {
134163
let callIdx = 0;
135164
mocks.fetchWithSsrFGuard.mockImplementation(async () => {
@@ -145,6 +174,40 @@ describe("runGitHubCopilotDeviceFlow — HTTP error propagation", () => {
145174
);
146175
});
147176

177+
it("cancels the unread access token body before throwing on non-OK", async () => {
178+
let canceled = false;
179+
let callIdx = 0;
180+
mocks.fetchWithSsrFGuard.mockImplementation(async () => {
181+
callIdx += 1;
182+
if (callIdx === 1) {
183+
return guardResponse(VALID_DEVICE_CODE_BODY);
184+
}
185+
const body = new ReadableStream<Uint8Array>({
186+
start(controller) {
187+
controller.enqueue(
188+
new TextEncoder().encode(JSON.stringify({ error: "bad_verification_code" })),
189+
);
190+
},
191+
cancel() {
192+
canceled = true;
193+
},
194+
});
195+
return {
196+
response: new Response(body, {
197+
status: 401,
198+
headers: { "Content-Type": "application/json" },
199+
}),
200+
finalUrl: ACCESS_TOKEN_URL,
201+
release: vi.fn(async () => {}),
202+
};
203+
});
204+
205+
await expect(runDeviceFlowAfterFirstPoll({ showCode: vi.fn(async () => {}) })).rejects.toThrow(
206+
"GitHub device token failed: HTTP 401",
207+
);
208+
expect(canceled).toBe(true);
209+
});
210+
148211
it("rejects a malformed access token response", async () => {
149212
let callIdx = 0;
150213
mocks.fetchWithSsrFGuard.mockImplementation(async () => {

‎extensions/github-copilot/login.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ async function postGitHubDeviceFlowForm(params: {
161161
});
162162
try {
163163
if (!response.ok) {
164+
// Release closes the dispatcher, so cancel its unread response body first.
165+
await response.body?.cancel().catch(() => undefined);
164166
throw new Error(`${params.failureLabel}: HTTP ${response.status}`);
165167
}
166168
return parseJsonResponse(

0 commit comments

Comments
 (0)