Skip to content

fix: remove dead sessions from target session list - #132

Merged
carmark merged 5 commits into
masterfrom
fix/remove-dead-sessions
Mar 17, 2026
Merged

fix: remove dead sessions from target session list#132
carmark merged 5 commits into
masterfrom
fix/remove-dead-sessions

Conversation

@carmark

@carmark carmark commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #42

When an iSCSI connection closes or a logout request is received, the associated session was never removed from the target's Sessions map, the ITNexus was never cleaned up, and the TSIH was never released. This caused resource leaks on every connect/disconnect cycle.

Root Cause Analysis

UnBindISCSISession() was only called from ReInstatement() (session replacement). It was never called from:

  • handler() when connection enters CONN_STATE_CLOSE (normal disconnect, error, etc.)
  • iscsiExecLogout() when initiator sends a logout request

Additionally, ReleaseTSIH() existed but was never called anywhere, so TSIHs were allocated but never freed.

Impact

  • Dead sessions accumulate in target.Sessions map
  • ITNexus entries leak in target.ITNexus map
  • TSIH bitmap slowly exhausts (max 65534 sessions before total exhaustion)
  • Target deletion may fail if orphan sessions exist

Changes

pkg/port/iscsit/session.go:

  • UnBindISCSISession(): Now also calls ReleaseTSIH() to free the TSIH
  • New removeConnectionFromSession(): Removes a connection from its session; if no connections remain, unbinds the session entirely. Uses conn.session = nil guard to prevent double-cleanup.

pkg/port/iscsit/iscsid.go:

  • handler(): Calls removeConnectionFromSession(conn) before closing the TCP connection on CONN_STATE_CLOSE
  • iscsiExecLogout(): Converted from free function to ISCSITargetDriver method; now calls removeConnectionFromSession() to clean up session on logout

Test plan

  • go build ./... passes
  • go test ./pkg/... ./mock/... all pass
  • CI workflow passes

🤖 Generated with Claude Code

carmark and others added 5 commits March 16, 2026 17:22
When an iSCSI connection closes or a logout occurs, the associated
session was never removed from the target's Sessions map, and the
TSIH was never released back to the bitmap allocator. This caused
session and TSIH leaks over repeated connect/disconnect cycles.

Changes:
- Add removeConnectionFromSession() to properly clean up session
  when its last connection is closed
- Call session cleanup from handler() on CONN_STATE_CLOSE
- Convert iscsiExecLogout to a method and add session cleanup on logout
- Release TSIH in UnBindISCSISession to prevent TSIH bitmap exhaustion

Fixes #42

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move session cleanup out of iscsiExecLogout() and keep it only in
the CONN_STATE_CLOSE handler. The logout response must be fully sent
before the session is removed; cleaning up during logout causes the
daemon to hang because subsequent operations reference a nil session.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When a new login arrives while the previous connection's async cleanup
is still running, LookupISCSISession finds the stale session but
LookupConnection returns nil (old connection already closed). This
caused a nil pointer dereference when accessing existConn.session
during session reinstatement.

Fix by checking existConn != nil before reinstatement. If the old
connection is already gone, unbind the stale session and create
a fresh one instead.

Also add sync.Once to removeConnectionFromSession to prevent
concurrent goroutine and main-path cleanup from racing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Restore CurrentHostIP cleanup on connection close (needed for
blockMultipleHostLogin feature). Extract to clearHostIP helper
to reduce duplication.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Discovery sessions have nil Target and nil ITNexus. The cleanup
path crashed with nil pointer dereference when trying to remove
ITNexus or access Target.Sessions for discovery sessions.

Guard against nil Target (just release TSIH) and nil ITNexus.
Also add nil safety to clearHostIP helper.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@carmark
carmark merged commit 7f708d0 into master Mar 17, 2026
1 check passed
@carmark
carmark deleted the fix/remove-dead-sessions branch March 17, 2026 07:10
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.

Remove dead session from the session list in target.

1 participant