Skip to content

Fix idle WebSocket detach during resume - #4

Merged
DiamondGo merged 1 commit into
mainfrom
fix/websocket-resume-detach
Sep 8, 2026
Merged

DiamondGo merged 1 commit into
mainfrom
fix/websocket-resume-detach

Conversation

@DiamondGo

@DiamondGo DiamondGo commented Sep 8, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • give each server-side WebSocket attachment a handler-owned cancellation context
  • cancel and interrupt the old writer when /resume kicks an attachment, while preserving harmless ACK/state interrupts
  • cancel the sibling pump whenever either WebSocket pump exits
  • add a regression test with a heartbeat longer than the detach budget, proving an idle WebSocket detaches and its replacement replays data

Root cause

v0.2.1 correctly stopped treating every errPipeInterrupted as a transport failure. However, when /resume closed and interrupted an idle server WebSocket with no ACK due, the write pump cleared the interrupt and parked in nextOut again. Repeated resume attempts kept waking and re-parking it, so the old attachment never detached and /resume returned 503 until the grace period expired.

The attachment-owned context now distinguishes replacement/shutdown from a harmless interrupt. The interrupt wakes nextOut, and the cancelled context makes the old write pump return immediately.

Tests

  • go test ./...
  • go test -race ./...
  • targeted WebSocket resume/interrupt tests with -count=5
  • regression test with -count=20
  • HttpBroker TestIntegration_ResumableWebSocket with -count=3 against this branch
  • full HttpBroker go test ./... against this branch

Summary by Sourcery

Ensure idle resumable WebSocket connections detach cleanly during resume so replacement connections can reconnect and replay pending data.

Bug Fixes:

  • Fix resumable WebSocket attachments that remain stuck after an idle /resume replacement, allowing the old connection to detach and the replacement to succeed immediately.

Enhancements:

  • Give each WebSocket attachment explicit lifecycle cancellation and coordinate cancellation of both pumps when either pump exits.

Tests:

  • Add a regression test covering idle WebSocket detachment and replay of pending data on the replacement connection.

@sourcery-ai

sourcery-ai Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Fix resumable WebSocket replacement by pairing outbound-pipe interrupts with an attachment-owned cancellation context, ensuring an idle old writer exits instead of re-parking; add regression coverage that verifies resume succeeds and the replacement replays buffered data.

Sequence diagram for resumable WebSocket replacement

sequenceDiagram
    participant Replacement as ReplacementWebSocket
    participant Handler as WebSocketHandler
    participant Attachment as Attachment
    participant OldWriter as OldWritePump
    participant Pipe as OutboundPipe
    participant OldSocket as OldWebSocket

    Replacement->>Handler: /resume
    Handler->>Attachment: attach(attachBoth, kick)
    Attachment->>Handler: kick old attachment
    Handler->>OldWriter: cancel()
    Handler->>OldSocket: CloseNow()
    Handler->>Pipe: interruptOut()
    Pipe-->>OldWriter: errPipeInterrupted
    OldWriter->>OldWriter: ctx.Err()
    OldWriter-->>Handler: return context.Canceled
    Handler-->>Replacement: resume succeeds
Loading

Sequence diagram for replacement replaying buffered WebSocket data

sequenceDiagram
    participant Client as ResumingClient
    participant NewHandler as NewWebSocketHandler
    participant Session as Session
    participant NewWriter as NewWritePump
    participant ClientSocket as NewWebSocket

    Client->>NewHandler: /resume
    NewHandler->>Session: attach(attachBoth, kick)
    NewHandler->>NewWriter: wsWritePumpResumable()
    NewWriter->>Session: nextOut()
    Session-->>NewWriter: buffered data
    NewWriter->>ClientSocket: Write buffered data
    ClientSocket-->>Client: replayed data
Loading

File-Level Changes

Change Details Files
Add an attachment-owned cancellation path so replacement and shutdown reliably terminate both WebSocket pumps.
  • Create a handler-owned context before publishing the attachment.
  • Cancel the context when an attachment is kicked or either pump exits.
  • Wake a writer blocked in the resumable outbound wait after pump termination.
  • Treat an interrupted outbound wait as terminal when its attachment context is cancelled, while preserving harmless interrupt handling otherwise.
websocket.go
Add regression coverage for detaching an idle WebSocket and replaying its buffered data through a replacement connection.
  • Use a heartbeat interval longer than the detach grace period to keep the write pump parked.
  • Resume the session and verify the old idle attachment is detached successfully.
  • Connect a replacement WebSocket and verify it replays the previously sent sequenced data.
resume_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot 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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@DiamondGo
DiamondGo merged commit 96c3a37 into main Sep 8, 2026
2 checks passed
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.

1 participant