Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions open-sse/utils/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ export function createSSEStream(options: StreamOptions = {}) {
} catch {}
}
clearIdleTimer();
trackPendingRequest(model, provider, connectionId, false);
controller.error(new Error(failurePayload.message || "Upstream failure"));
return;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/stream-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,3 +972,63 @@ test("createRequestLogger caps retained stream chunk item count", async () => {
"[stream chunk log truncated after 2 chunks]",
]);
});

// T-VERIFY: passthrough mode failure decrements pending requests
// Regression test for missing trackPendingRequest(false) on passthrough failure
import { getPendingRequests, clearPendingRequests } from "../../src/lib/usage/usageHistory.ts";

test("createSSEStream passthrough mode decrements pending requests on failure", async () => {
// Clear any existing pending requests first
clearPendingRequests();
const initial = getPendingRequests();
assert.equal(Object.keys(initial.byModel).length, 0, "should start with no pending requests");

let failurePayload = null;
const testProvider = "openai-compatible-test-failure";
const testModel = "gpt-test";
const testConnectionId = "test-conn-123";

await assert.rejects(
readTransformed(
[
`data: ${JSON.stringify({
type: "response.failed",
response: {
id: "resp_failed_test",
object: "response",
model: testModel,
status: "failed",
error: {
code: "test_failure",
message: "Test failure for pending request tracking",
},
},
})}\n\n`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI_RESPONSES,
provider: testProvider,
model: testModel,
connectionId: testConnectionId,
body: { input: "hello" },
onFailure(payload) {
failurePayload = payload;
},
}
),
/Test failure|Upstream failure/
);

assert.ok(failurePayload, "should report the stream failure");

// Verify pending requests are properly decremented after failure
const pending = getPendingRequests();
const modelKey = `${testModel} (${testProvider})`;
const count = pending.byModel[modelKey] || 0;
assert.equal(
count,
0,
`pending request count for ${modelKey} should be 0 after failure, got ${count}`
);
});