Skip to content

fix: prevent channel error leaks and iterator panics - #21565

Merged
wangq8 merged 2 commits into
infiniflow:mainfrom
buua436:go132
Oct 29, 2026
Merged

wangq8 merged 2 commits into
infiniflow:mainfrom
buua436:go132

Conversation

@buua436

@buua436 buua436 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

WhatsApp runtime errors could expose a tenant-configured gateway's response body through the tenant-readable last_error field. Closing an iterator could also close the shared channel while the runner was still sending or closing it, causing a panic.

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 68c0a46f-3905-43c8-9419-7cb20963aafb
Commits

Reviewing files that changed from the base of the PR and between 0a594f4 and 7280b30.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 32c06fc5-9b69-477c-b20e-fa2e2a824f77
Commits

Reviewing files that changed from the base of the PR and between 88321d6 and 0a594f4.

Files selected for processing: 5
  • internal/channels/whatsapp.go
  • internal/channels/whatsapp_security_test.go
  • internal/harness/core/async_iterator_test.go
  • internal/harness/core/utils.go
  • internal/harness/core/workflow_stress_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

The PR changes WhatsApp HTTP error handling so errors for HTTP 400 and above omit the response body. It also changes async iterator and generator shutdown coordination and adds tests for gateway errors, untrusted destinations, buffered items, and blocked sends.

Changes: WhatsApp gateway errors

Layer / File(s) Summary
HTTP status errors and security tests
internal/channels/whatsapp.go, internal/channels/whatsapp_security_test.go
The request helper returns an error containing the HTTP status for responses of 400 or above before reading the body. Tests check that LastError omits the response body and that a request to an untrusted destination is rejected.

Async iterator lifecycle

Layer / File(s) Summary
Iterator shutdown and generator sends
internal/harness/core/utils.go, internal/harness/core/async_iterator_test.go, internal/harness/core/workflow_stress_test.go
The iterator and generator use shutdown and completion signals to coordinate closure and in-flight sends. Tests cover buffered-item draining, blocked sends, post-closure sends, and handleIter panic safety.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant AsyncGenerator
  participant AsyncIterator
  Caller->>AsyncGenerator: Start a send
  Caller->>AsyncIterator: Close iterator
  AsyncIterator-->>AsyncGenerator: Signal iterator shutdown
  AsyncGenerator-->>Caller: Stop the in-flight send
  Caller->>AsyncGenerator: Close generator
  AsyncGenerator->>AsyncGenerator: Wait for in-flight sends
  AsyncGenerator-->>Caller: Signal completion
Loading

Suggested labels: 🐞 bug, go

Suggested reviewers: jay77721, yuzhichang

Merge Risk: Minimal · previous review 0a594

The gateway error and iterator shutdown fixes appear ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly summarizes both primary fixes: preventing channel error leaks and iterator panics.
Description check Passed The description explains both problems and identifies the change as a bug fix. It does not use the template's exact "### Summary" heading, but it provides the required summary information.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 9 functions across 5 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the gateway’s note,
And keeps its body out of sight.
It waits for sends to find their stop,
Then drains the buffered bits just right.
“All closed,” it says, and hops from view.

Comment @coderabbitai help to get the list of available commands.

@buua436 buua436 added the ci Continue Integration label Oct 27, 2026
@buua436
buua436 requested a review from wangq8 October 27, 2026 11:36

@Lynn-Inf Lynn-Inf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Three non-blocking notes:

  1. The WhatsApp error now carries only the status code, which loses the gateway's explanation for operators debugging a 401/500, and the error path no longer reads the body to EOF, so the transport will not reuse that connection. Keeping the bounded io.LimitReader read and logging the (truncated) body at debug level — rather than putting it into the returned error — would preserve both debuggability and keep-alive without re-introducing the tenant-visible leak.

  2. AsyncIterator.Close() intentionally stops closing ch, using stop/done instead, but that is not stated anywhere the next reader will look. Other code in the package still reads it.ch directly (e.g. model_chain_test.go:275), so a comment on Close() saying the channel is never closed and termination is signalled through stop/done would prevent a future caller from waiting on a close that never happens.

  3. Dropping a Send after close is the intended behaviour, but it leaves no trace if it happens because of a lifecycle bug elsewhere. A debug log on the discarded path (with the iterator/generator identity) would make those cases visible without changing behaviour.

@wangq8
wangq8 merged commit 6ef2656 into infiniflow:main Oct 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants