Skip to content

Add atomic idle session close - #1

Merged
DiamondGo merged 2 commits into
mainfrom
fix/atomic-idle-close
Aug 31, 2026
Merged

DiamondGo merged 2 commits into
mainfrom
fix/atomic-idle-close

Conversation

@DiamondGo

@DiamondGo DiamondGo commented Aug 31, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • add CloseSessionIfNoPollInFlight, which atomically closes only the current, idle store entry
  • synchronize batch poll, stream upload/download, WebSocket attachment, sweeper eviction, and unconditional close under one lifecycle protocol
  • preserve exactly-once removal, pipe close, and OnDisconnect, with application callbacks outside internal locks
  • document PollInFlight as an observational snapshot rather than a lifecycle decision API
  • include .gitignore update for the cross-repository plans directory
  • release as v0.1.6

Tests

  • go test ./... -count=5
  • go test -race ./... -count=1
  • atomic close and lifecycle race tests repeated 500 times

Coverage includes batch and stream polls, WebSocket attach/detach, store identity, closed-session HTTP behavior, and races against normal close, DELETE, and the sweeper.

Summary by Sourcery

Make idle session closure atomic and race-safe across all session lifecycle operations.

New Features:

  • Add an atomic API for closing the current session only when no poll or persistent transport is attached.

Bug Fixes:

  • Prevent session lifecycle races from causing duplicate removal, pipe closure, or disconnect notifications.
  • Ensure closed sessions reject new poll and WebSocket attachments with appropriate HTTP responses.

Enhancements:

  • Unify polls, streaming transports, WebSocket attachments, sweeper eviction, and explicit closes under a synchronized lifecycle protocol.
  • Clarify that PollInFlight is an observational snapshot and not a safe close decision mechanism.

Tests:

  • Add extensive coverage for atomic close behavior, store identity, transport attachment races, closed-session behavior, and exactly-once disconnect handling.

Chores:

  • Ignore the cross-repository plans directory.
  • Release version 0.1.6.

Add CloseSessionIfNoPollInFlight and synchronize poll, stream, WebSocket, sweeper, and close lifecycle transitions. Cover transport races and exactly-once disconnect behavior, fix race-prone test state, and ignore cross-repo plans.
@sourcery-ai

sourcery-ai Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds an atomic idle-session close path and applies a single store-to-session lifecycle locking protocol across polls, WebSockets, unconditional close, and sweeping, while keeping pipe cleanup and callbacks outside locks and adding broad race-focused test coverage.

Sequence diagram for atomic idle session close

sequenceDiagram
    participant Caller
    participant SessionStore
    participant Session
    participant Hooks

    Caller->>SessionStore: CloseSessionIfNoPollInFlight(st, h, s, reason)
    SessionStore->>SessionStore: removeAndMarkClosed(s, true)
    SessionStore->>Session: lock lifecycle state
    alt current entry and pollInFlight == 0
        SessionStore->>Session: mark closed and delete from store
        SessionStore-->>Caller: true
        Caller->>Session: closePipes()
        Caller->>Hooks: OnDisconnect(s, reason)
    else replaced, closed, or attached transport
        SessionStore-->>Caller: false
    end
Loading

File-Level Changes

Change Details Files
Introduces an atomic conditional close operation for idle, current sessions.
  • Adds CloseSessionIfNoPollInFlight with store-identity validation and an idle check.
  • Moves removal and closed-state transition into one store/session lock-protected operation.
  • Ensures pipe closure and disconnect callbacks run after internal locks are released.
handler.go
session.go
store.go
Unifies poll and persistent transport lifecycle tracking under the session mutex.
  • Adds synchronized begin/end operations for batch and stream polls.
  • Tracks WebSocket attachment in the same in-flight counter and rejects attachment to closed sessions.
  • Defines PollInFlight as an observational snapshot rather than a safe close decision mechanism.
handler.go
session.go
websocket.go
Makes unconditional close and sweeper eviction follow the same exactly-once lifecycle protocol.
  • Validates current store identity before removal.
  • Performs final closed, idle, and expiry checks while holding store and session locks.
  • Preserves single pipe close and OnDisconnect behavior during close, DELETE, eviction, and attachment races.
handler.go
store.go
Adds extensive coverage for atomic close behavior and lifecycle races.
  • Tests batch and stream polls, WebSocket attachment/detachment, stale session identity, closed-session HTTP behavior, and exactly-once callbacks.
  • Adds repeated linearizability and race tests against normal close, DELETE, and sweeper eviction.
  • Updates existing tests to use synchronized lifecycle helpers and avoids relay test dependence on sweeper timing.
atomic_idle_close_test.go
handler_test.go
session_test.go
store_test.go
websocket_test.go
relay_topology_test.go
client_test.go
Updates repository metadata and release version.
  • Ignores the cross-repository plans directory.
  • Releases the package as v0.1.6.
.gitignore

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 found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="session.go" line_range="58-59" />
<code_context>
 	// drops immediately, which is what turns a silent client death into a fast
 	// detection instead of a session_timeout wait (A3).
-	pollInFlight atomic.Int32
+	pollInFlight int32

 	mu         sync.Mutex
</code_context>
<issue_to_address>
**nitpick:** The field comment still says `pollInFlight` counts only long polls and describes a parked TCP connection, but the new implementation also counts send-only requests, stream requests before they park, and WebSocket attachments. Users relying on the field's documented meaning can incorrectly treat a nonzero value as proof of a parked long poll or miss that it represents a WebSocket transport.

**Suggested fix:** Update the field comment to describe all lifecycle attachments counted by `pollInFlight`, or move the lifecycle-specific explanation to `PollInFlight` and remove the stale long-poll-only wording.

```suggestion
	// pollInFlight counts active poll requests and persistent transports attached
	// to the session, including send-only requests, stream requests before they
	// park, and WebSocket attachments. A nonzero value means a client transport
	// is currently attached and prevents eviction; it does not necessarily mean
	// a long poll is parked.
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread session.go Resolved
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@DiamondGo
DiamondGo merged commit fd04def into main Aug 31, 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