惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
博客园_首页
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
B
Blog
D
DataBreaches.Net
腾讯CDC
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
月光博客
月光博客
V
V2EX
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
The Cloudflare Blog
博客园 - 叶小钗
Y
Y Combinator Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(e2e): cancel chat-tools response reads on timeout · o...
vincentkoc · 2026-06-19 · via Recent Commits to openclaw:main

@@ -19,7 +19,35 @@ if (!Number.isFinite(maxBodyBytes) || maxBodyBytes <= 0) {

1919

throw new Error(`invalid OPENCLAW_OPENAI_CHAT_TOOLS_MAX_BODY_BYTES: ${maxBodyBytes}`);

2020

}

212122-

async function readBoundedResponseText(response, byteLimit) {

22+

function cancelReaderSoon(reader) {

23+

void Promise.resolve()

24+

.then(() => reader.cancel())

25+

.catch(() => undefined);

26+

}

27+28+

async function readResponseChunk(reader, timeoutPromise, markCanceled) {

29+

const readPromise = reader.read();

30+

if (!timeoutPromise) {

31+

return await readPromise;

32+

}

33+34+

let waitingForRead = true;

35+

const timeoutReadPromise = timeoutPromise.catch((error) => {

36+

if (waitingForRead) {

37+

markCanceled();

38+

cancelReaderSoon(reader);

39+

}

40+

throw error;

41+

});

42+43+

try {

44+

return await Promise.race([readPromise, timeoutReadPromise]);

45+

} finally {

46+

waitingForRead = false;

47+

}

48+

}

49+50+

async function readBoundedResponseText(response, byteLimit, timeoutPromise) {

2351

const contentLength = response.headers?.get?.("content-length");

2452

if (contentLength && /^\d+$/u.test(contentLength)) {

2553

const parsedContentLength = Number(contentLength);

@@ -35,67 +63,88 @@ async function readBoundedResponseText(response, byteLimit) {

3563

}

3664

const chunks = [];

3765

let totalBytes = 0;

38-

for (;;) {

39-

const { done, value } = await reader.read();

40-

if (done) {

41-

break;

66+

let canceled = false;

67+

try {

68+

for (;;) {

69+

const { done, value } = await readResponseChunk(reader, timeoutPromise, () => {

70+

canceled = true;

71+

});

72+

if (done) {

73+

break;

74+

}

75+

totalBytes += value.byteLength;

76+

if (totalBytes > byteLimit) {

77+

canceled = true;

78+

await reader.cancel();

79+

throw new Error(`chat completions response body exceeded ${byteLimit} bytes`);

80+

}

81+

chunks.push(Buffer.from(value));

4282

}

43-

totalBytes += value.byteLength;

44-

if (totalBytes > byteLimit) {

45-

await reader.cancel();

46-

throw new Error(`chat completions response body exceeded ${byteLimit} bytes`);

83+

} finally {

84+

if (!canceled) {

85+

reader.releaseLock();

4786

}

48-

chunks.push(Buffer.from(value));

4987

}

5088

return Buffer.concat(chunks, totalBytes).toString("utf8");

5189

}

52905391

const controller = new AbortController();

54-

const timeout = setTimeout(() => controller.abort(), timeoutSeconds * 1000);

92+

const timeoutError = new Error(`chat completions request timed out after ${timeoutSeconds}s`);

93+

let timeout;

94+

const timeoutPromise = new Promise((_, reject) => {

95+

timeout = setTimeout(() => {

96+

controller.abort(timeoutError);

97+

reject(timeoutError);

98+

}, timeoutSeconds * 1000);

99+

timeout.unref?.();

100+

});

55101

const started = Date.now();

56102

let response;

57103

let text;

58104

try {

59-

response = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {

60-

method: "POST",

61-

headers: {

62-

authorization: `Bearer ${token}`,

63-

"content-type": "application/json",

64-

"x-openclaw-model": backendModel,

65-

},

66-

body: JSON.stringify({

67-

model: "openclaw",

68-

stream: false,

69-

messages: [

70-

{

71-

role: "user",

72-

content:

73-

"Use the get_weather tool exactly once for Paris, France. Return the tool call only.",

74-

},

75-

],

76-

tool_choice: "auto",

77-

tools: [

78-

{

79-

type: "function",

80-

function: {

81-

name: "get_weather",

82-

description: "Return weather for a city.",

83-

strict: true,

84-

parameters: {

85-

type: "object",

86-

additionalProperties: false,

87-

properties: {

88-

city: { type: "string", description: "City and country." },

105+

response = await Promise.race([

106+

fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {

107+

method: "POST",

108+

headers: {

109+

authorization: `Bearer ${token}`,

110+

"content-type": "application/json",

111+

"x-openclaw-model": backendModel,

112+

},

113+

body: JSON.stringify({

114+

model: "openclaw",

115+

stream: false,

116+

messages: [

117+

{

118+

role: "user",

119+

content:

120+

"Use the get_weather tool exactly once for Paris, France. Return the tool call only.",

121+

},

122+

],

123+

tool_choice: "auto",

124+

tools: [

125+

{

126+

type: "function",

127+

function: {

128+

name: "get_weather",

129+

description: "Return weather for a city.",

130+

strict: true,

131+

parameters: {

132+

type: "object",

133+

additionalProperties: false,

134+

properties: {

135+

city: { type: "string", description: "City and country." },

136+

},

137+

required: ["city"],

89138

},

90-

required: ["city"],

91139

},

92140

},

93-

},

94-

],

141+

],

142+

}),

143+

signal: controller.signal,

95144

}),

96-

signal: controller.signal,

97-

});

98-

text = await readBoundedResponseText(response, maxBodyBytes);

145+

timeoutPromise,

146+

]);

147+

text = await readBoundedResponseText(response, maxBodyBytes, timeoutPromise);

99148

} finally {

100149

clearTimeout(timeout);

101150

}