Skip to content

fix(telegram): cancel unread response body on media fetch error - #110039

Closed
Monkey-wusky wants to merge 1 commit into
openclaw:mainfrom
Monkey-wusky:fix/telegram-cancel-media-harness-body
Closed

Monkey-wusky wants to merge 1 commit into
openclaw:mainfrom
Monkey-wusky:fix/telegram-cancel-media-harness-body

Conversation

@Monkey-wusky

@Monkey-wusky Monkey-wusky commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

When defaultReadRemoteMediaBuffer in the Telegram e2e harness receives a non-OK response (e.g. 404, 503), it throws immediately without canceling the response body. The unconsumed stream holds a connection open until garbage collection.

Why This Change Was Made

Follows the same pattern as #109519 (Google Chat), #109508 (docs search), #109677 (Chutes), #109701 (MSTeams consent upload), and #109728 (APNs relay) — cancel the response body before throwing so that upstream streams are drained and connections released.

User Impact

No user-facing change. This is test harness infrastructure used by the Telegram bot media e2e tests. Prevents resource leaks from unconsumed response bodies.

Evidence

Real HTTP server proof

$ node scripts/proofs/telegram-media-cancel-proof.mjs
Server: http://127.0.0.1:34072

=== BEFORE (no body.cancel — throw immediately) ===
  Response: 503 Service Unavailable

=== AFTER (body?.cancel().catch(() => undefined) before throw) ===
  Response: 503 Service Unavailable
  body?.cancel() executed ✓
  [server] /new → client canceled stream ✓

=== Error message preserved after cancel ===
  Thrown: Failed to fetch media from http://127.0.0.1:34072/error: HTTP 503 Service Unavailable

The proof script starts a real HTTP server returning 503 with a slow stream, then exercises the exact cancel-before-throw pattern from this PR. The server confirms the client canceled the stream (client canceled stream ✓), demonstrating that body?.cancel() reaches the server and releases the connection. The error message is preserved after cancel.

Unit test

New test ("cancels response body on non-ok status before throwing") in bot.media.stickers-and-fragments.e2e.test.ts verifies:

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: XS labels Jul 17, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. labels Jul 17, 2026
@clawsweeper

clawsweeper Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 31, 2026, 1:51 PM ET / 17:51 UTC.

ClawSweeper review

What this changes

The branch cancels an unread response body before the Telegram media e2e harness throws for a failed download, with a regression test that observes the cancellation.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

This PR remains necessary: current main's Telegram media e2e harness still throws on a non-OK media response without releasing its unread body. The proposed change fixes that exact harness-owned path, follows the repository’s established cancellation pattern, and does not introduce a code-level review finding; it needs a current-head CI refresh before merge.

Priority: P3
Reviewed head: c80958051dca3beb6d296a466ed48da353f8c0fa

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A narrow, source-aligned cleanup with credible real-server proof; only current-head CI verification remains before merge.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The PR body provides after-fix terminal evidence from a real local HTTP server that the error response stream is cancelled and the original error message remains intact; redact any private endpoint details if posting refreshed proof.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The PR body provides after-fix terminal evidence from a real local HTTP server that the error response stream is cancelled and the original error message remains intact; redact any private endpoint details if posting refreshed proof.
Evidence reviewed 6 items Current-main defect: The harness fetches a response and immediately throws for a non-OK status without reading or cancelling response.body; the PR’s one-line change targets that exact branch.
Canonical cleanup contract: The shared media reader consumes a bounded error-body snippet before raising its HTTP error, while the HTTP-body utilities explicitly cancel abandoned streams on exceptional paths. The harness is the remaining simple reader that skips either action.
Established sibling pattern: Current OpenClaw code uses await response.body?.cancel().catch(() => undefined) on comparable non-OK paths, including the Telegram API fetch path and multiple extension HTTP clients.
Findings None None.
Security None None.

How this fits together

Telegram media tests route a mocked remote media download through a harness reader before media resolution continues. The reader either buffers successful media or reports a failed HTTP response, so its error path owns cleanup of an unread stream.

flowchart LR
  A[Telegram media test] --> B[Media resolver]
  B --> C[Harness media reader]
  C --> D{HTTP response OK?}
  D -->|Yes| E[Buffer media]
  D -->|No| F[Cancel unread body]
  F --> G[Report HTTP error]
Loading

Before merge

  • Resolve merge risk (P1) - The branch is behind current main and its compact CI shard plus aggregate gate are failing; the supplied context does not include failure logs, so maintainer merge gating should use a fresh exact-head run rather than infer a code defect.
  • Complete next step (P2) - No discrete repair is proven from the failing CI summary; the remaining action is a maintainer-facing exact-head CI refresh and normal merge verification.
Agent review details

Security

None.

PR surface

Source +1, Tests +22. Total +23 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 1 0 +1
Tests 1 23 1 +22
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 24 1 +23

Review metrics

None.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep the harness-local cancellation and regression assertion, then refresh the branch against current main and merge once the exact-head compact check is understood and green.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep the harness-local cancellation and regression assertion, then refresh the branch against current main and merge once the exact-head compact check is understood and green.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: return a non-OK Response with an unread stream to defaultReadRemoteMediaBuffer; current main throws before consuming or cancelling it. The PR’s supplied real-server transcript independently shows the intended cancellation result, although this read-only review did not execute it.

Is this the best way to solve the issue?

Yes. The non-OK branch that owns the unread response is the narrowest cleanup boundary, and the awaited cancellation matches established OpenClaw response-body cleanup without changing the existing error contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 8a4da1d19fad.

Labels

Label justifications:

  • P3: This is a narrow test-harness resource-cleanup fix with no user-facing configuration, delivery, or runtime behavior change.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body provides after-fix terminal evidence from a real local HTTP server that the error response stream is cancelled and the original error message remains intact; redact any private endpoint details if posting refreshed proof.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides after-fix terminal evidence from a real local HTTP server that the error response stream is cancelled and the original error message remains intact; redact any private endpoint details if posting refreshed proof.

Evidence

What I checked:

  • Current-main defect: The harness fetches a response and immediately throws for a non-OK status without reading or cancelling response.body; the PR’s one-line change targets that exact branch. (extensions/telegram/src/bot.media.e2e-harness.ts:47, c630e4aec3aa)
  • Canonical cleanup contract: The shared media reader consumes a bounded error-body snippet before raising its HTTP error, while the HTTP-body utilities explicitly cancel abandoned streams on exceptional paths. The harness is the remaining simple reader that skips either action. (src/media/fetch.ts:261, 8a4da1d19fad)
  • Established sibling pattern: Current OpenClaw code uses await response.body?.cancel().catch(() => undefined) on comparable non-OK paths, including the Telegram API fetch path and multiple extension HTTP clients. (extensions/telegram/src/api-fetch.ts:76, 8a4da1d19fad)
  • Current-main provenance: Blame attributes the current harness reader, including its non-OK branch, to the harness-introducing commit by Vincent Koc. That commit is reachable from current main, so the claimed defect is still present rather than historical branch drift. (extensions/telegram/src/bot.media.e2e-harness.ts:40, c630e4aec3aa)
  • Release and current-main check: The latest release tag is v2026.7.1 at 2d2ddc43d0dcf71f31283d780f9fe9ff4cc04fe4; the current harness was committed later on July 31, 2026, so this PR is neither already shipped nor already implemented on main. (extensions/telegram/src/bot.media.e2e-harness.ts:40, c630e4aec3aa)
  • Contributor proof: The PR body includes terminal output from a real local HTTP server showing a 503 response, execution of body.cancel(), server-observed client cancellation, and preservation of the original error message. The prepared downloadable artifact only points to the test URL https://example.com/file.jpg and failed with 404, so it is not used as proof. (extensions/telegram/src/bot.media.stickers-and-fragments.e2e.test.ts:274, c80958051dca)

Likely related people:

  • Vincent Koc: git blame assigns the complete current media-reader implementation, including the non-OK branch, to the commit that introduced this harness on current main. (role: current harness introducer; confidence: high; commits: c630e4aec3aa; files: extensions/telegram/src/bot.media.e2e-harness.ts, extensions/telegram/src/bot.media.stickers-and-fragments.e2e.test.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Rebase or refresh against current main and obtain the compact shard’s exact failure output; merge once the current head is green or the failure is shown unrelated.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (6 earlier review cycles)
  • reviewed 2026-07-17T14:27:46.682Z sha b48e8fa214d58d83948950959e1e5f5e8004a913 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-17T14:51:49.653Z sha b48e8fa214d58d83948950959e1e5f5e8004a913 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-17T15:30:40.394Z sha 170dd43739cff0a6fdc713fd18546bb27c0bc454 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-17T16:12:36.522Z sha 170dd43739cff0a6fdc713fd18546bb27c0bc454 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-20T06:56:16.331Z sha c809580 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-31T09:17:43.133Z sha c809580 :: needs maintainer review before merge. :: none

@Monkey-wusky

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 17, 2026
@Monkey-wusky
Monkey-wusky force-pushed the fix/telegram-cancel-media-harness-body branch from b48e8fa to 170dd43 Compare July 17, 2026 15:25
@Monkey-wusky
Monkey-wusky force-pushed the fix/telegram-cancel-media-harness-body branch from 170dd43 to c809580 Compare July 20, 2026 06:52
@steipete

steipete commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Thanks, @Monkey-wusky, for following up on response-body cleanup.

I checked this against current main, the actual production Telegram media path, and a real HTTP server. This change only reaches the Vitest E2E harness; no production media download imports it. Real Telegram downloads use saveRemoteMedia, whose existing assertMediaResponseOk reads a bounded error-body snippet and whose finally releases the guarded fetch.

For the harness itself, the existing tests use finite in-memory mocked responses or mocked errors. Running the unchanged harness against a real finite HTTP 404 showed that the next healthy request reused the exact same TCP connection, so the existing scenario does not leak a socket. The indefinitely streaming response used to motivate this PR is not produced by these tests.

The proposed awaited response.body.cancel() also introduces a failure mode: a valid stream whose cancellation promise does not settle leaves the harness pending indefinitely instead of surfacing the HTTP error, which can freeze the suite.

Since there is no user-facing bug, the real production owner already handles errors correctly, and the change adds suite-hang risk to a mocked-only path, I’m closing this rather than landing it. The failing CI is secondary to that behavioral concern. Thank you again for the cleanup work.

@steipete steipete closed this Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants