Skip to content

fix(gun): keep timeout-wrapped streams readable#847

Merged
yordis merged 5 commits intomasterfrom
yordis/fixes-709
Apr 22, 2026
Merged

fix(gun): keep timeout-wrapped streams readable#847
yordis merged 5 commits intomasterfrom
yordis/fixes-709

Conversation

@yordis
Copy link
Copy Markdown
Member

@yordis yordis commented Apr 22, 2026

  • Timeout middleware moves request execution into a separate task, so Gun streamed responses need to keep headers readable there while leaving body chunks readable from the original caller.
  • Stream read failures should surface as stream errors instead of crashing with a case clause mismatch.
  • This preserves the Gun streaming path reported in Gun: streaming response body breaks with Tesla.Middleware.Timeout #709.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Copilot AI review requested due to automatic review settings April 22, 2026 07:25
@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 22, 2026

PR Summary

Medium Risk
Changes Tesla.Adapter.Gun message routing for streamed/chunked responses and alters how reply_to is handled, which can impact concurrency and process messaging behavior. Tests cover key scenarios, but regressions could surface under unusual :gun versions or custom reply_to handlers.

Overview
Fixes Gun streamed/chunked responses when requests are executed inside Tesla.Middleware.Timeout’s task by introducing a :tesla_gun_stream_owner handoff and routing :gun messages to both the task (for headers/status) and the original caller (for body chunks).

Tesla.Adapter.Gun now handles {:gun_error, pid, stream, reason} during chunk reads, raises stream errors instead of crashing on mismatched patterns, and preserves explicit reply_to functions/pids while streaming (with version-specific routing for Gun 1.x vs 2.x). New tests validate streaming through timeout, explicit reply_to behavior, and that timeout middleware doesn’t leak Gun-specific adapter opts into other adapters.

Reviewed by Cursor Bugbot for commit 97f1f12. Bugbot is set up for automated code reviews on this repo. Configure here.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Gun streamed responses when used with Tesla.Middleware.Timeout by ensuring streamed body messages remain readable from the original caller process while request execution happens in a spawned timeout task, and by turning stream read failures into stream errors (instead of crashing on pattern mismatches).

Changes:

  • Injects an adapter :reply_to into the request env in Timeout so Gun can forward stream data back to the original caller.
  • Updates Gun adapter streaming to route :gun_data / :gun_error messages to the stream owner and handle stream errors explicitly.
  • Adds a regression test covering Gun streaming through the Timeout middleware.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/tesla/middleware/timeout.ex Adds adapter :reply_to propagation from caller into the spawned timeout task.
lib/tesla/adapter/gun.ex Routes streamed body messages to the intended owner and converts stream read failures into explicit stream errors.
test/tesla/middleware/timeout_test.exs Adds regression coverage for Gun streaming + Timeout and updates stacktrace line assertions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/tesla/middleware/timeout.ex
Comment thread lib/tesla/adapter/gun.ex Outdated
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2d8f6bf. Configure here.

Comment thread lib/tesla/adapter/gun.ex
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an interaction between Tesla.Middleware.Timeout (which executes requests in a separate task) and Tesla.Adapter.Gun streaming responses, preserving Gun’s streaming behavior reported in #709.

Changes:

  • Add a regression test ensuring Gun body_as: :stream remains readable when wrapped by Tesla.Middleware.Timeout.
  • Pass the original caller pid through env.private so the Gun adapter can route stream body messages back to the caller while the timeout task reads the response headers.
  • Handle {:error, reason} from read_chunk/3 in the streaming response path to avoid a CaseClauseError and raise a clearer stream error instead.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/tesla/middleware/timeout_test.exs Adds coverage for Gun streaming under Timeout and verifies adapter opts aren’t mutated.
lib/tesla/middleware/timeout.ex Records the original caller pid in env.private for Gun streaming compatibility through the timeout task.
lib/tesla/adapter/gun.ex Routes streamed body messages to the original caller and raises on stream read errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/tesla/adapter/gun.ex Outdated
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Gun streamed responses when used with Tesla.Middleware.Timeout, ensuring response headers remain readable in the timeout task while streamed body chunks remain readable from the original caller process (per #709). It also ensures stream read failures are raised as stream errors rather than crashing with a CaseClauseError.

Changes:

  • Add :tesla_gun_stream_owner metadata in Tesla.Middleware.Timeout to preserve the original caller as the stream consumer.
  • Update Tesla.Adapter.Gun to route Gun messages appropriately across the timeout task boundary and to handle {:gun_error, pid, stream, reason} during streaming reads.
  • Expand test coverage for Gun streaming + timeout interactions and for preserving adapter opts.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
lib/tesla/middleware/timeout.ex Stores original caller pid in env.private for Gun streaming handoff across the timeout task.
lib/tesla/adapter/gun.ex Wraps/routers Gun reply_to for streamed/chunked responses and converts stream read failures into stream errors.
test/tesla/middleware/timeout_test.exs Adds regression tests for Gun streaming through the Timeout middleware and verifies adapter opts aren’t mutated.
test/tesla/adapter/gun_test.exs Adds tests ensuring explicit reply_to behavior is preserved, including when stream ownership is handed off.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yordis yordis merged commit 8a8ba74 into master Apr 22, 2026
14 checks passed
@yordis yordis deleted the yordis/fixes-709 branch April 22, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants