wzcheng99andClaude Opus 4.7 1d21b0ff68 BUG-005 fix: session layer stops closing the Provider-cached Backend
m01 Path B reconnect matrix cycle 2 exposed: kernel gets
  nvme1n1: I/O Cmd(0x2) @ LBA 0, I/O Error (sct 0x3 / sc 0x2)
on the very first Read of a second nvme connect after a clean
disconnect.

SCT=3 SC=2 = ANA Asymmetric Access Inaccessible, mapped from
frontend.ErrBackendClosed via nvme.NewBackendClosed(). NOT a
data-integrity bug — bytes on disk are intact. Pure Backend
lifecycle bookkeeping.

## Root cause

Session-layer code was calling Backend.Close() when a TCP session
ended:

  - core/frontend/nvme/target.go handleConn: defer backend.Close()
  - core/frontend/iscsi/session.go close(): s.backend.Close()

Fine under memback (Provider.Open creates a fresh backend struct
per call). Broken under DurableProvider which MUST cache one
Backend per volumeID because LogicalStorage is an exclusive
resource. The per-session Close flipped the CACHED backend
closed → next session received the same closed handle → all I/O
returned ErrBackendClosed → NVMe mapped to SCT=3 SC=2.

V2 structurally avoided this: Subsystem.Dev is registered via
AddVolume / unregistered via RemoveVolume (admin-level), and
Controller.shutdown() explicitly does NOT touch the device. See
weed/storage/blockvol/nvme/{server.go,controller.go}.

V3 had the right intent (T3a mini plan §3: "Backend.Close marks
closed but does NOT tear down storage; Provider retains storage
handle") but the interface godoc didn't document WHO is allowed
to call Close. T2 target authors treated Backend.Close as
session-owned by pattern-matching memback's accidental shape.

## Changes

Production:
  core/frontend/nvme/target.go        -1 line (removed defer)
  core/frontend/iscsi/session.go      -3 lines (removed conditional close)
  core/frontend/types.go              +11 lines (Backend godoc —
                                       Provider ownership clause)

Tests:
  core/frontend/durable/bug005_session_reuse_test.go   new (~120 LOC)
    - TestT3_Bug005_ProviderCachedBackend_ReusableAcrossSessions
      Matrix both impls. Pins: after session 1's "close", session 2's
      Open returns a USABLE Backend (not ErrBackendClosed). Sits at
      Provider level — doesn't need a real target session to exercise
      the invariant the fix establishes.
    - TestT3_Bug005_ExplicitBackendClose_IsAllowed
      Counterfactual: explicit Close by a caller who owns the handle
      is still legal; subsequent I/O returns ErrBackendClosed. This
      documents what Close IS for (Provider.Close path) vs what it is
      NOT for (implicit session teardown).

## Why existing tests missed it

All T3a/T3b/T3c in-process tests are:
  - single-session (no disconnect-reconnect cycle), OR
  - direct-handler (SCSIHandler.HandleCommand / IOHandler.Handle),
    bypassing target.handleConn's defer

Only "real target × session 1 disconnect × session 2 open"
exercises cross-session cache poisoning. Same L1-passes-L2-fails
shape as BUG-001.

## Contract going forward

frontend.Backend godoc now states:

  "Backend.Close() is OWNED BY THE PROVIDER. Session-layer
   consumers MUST NOT call Close on a Backend obtained from
   Provider.Open — treat the returned Backend as a BORROWED
   reference..."

Backend.Close itself stays legal; explicit-owner Close is still
supported (Provider.Close path, tests). The fix is about NOT
having session/target teardown silently call it.

## Regression

  go test ./core/... -count=1 → 17/17 packages ok, 0 FAIL
  BUG-005 regression tests: 2 × 2 impls = 4 subtests green
  Prior T3 suites: unchanged (no impact)

## Ledger row queued ACTIVE

INV-DURABLE-SESSION-LIFECYCLE-001: Across session disconnect +
reconnect, the Provider-cached Backend remains operational; I/O
on the reconnected session MUST NOT receive ErrBackendClosed
caused by prior session teardown.

## T3-end three-sign impact

- Path A rename still stands (INV-DURABLE-CLEAN-RESTART-001 etc.)
- New row INV-DURABLE-SESSION-LIFECYCLE-001 added to closure
- G4 partial-pass gate updated: "clean restart OK + cross-session
  reconnect OK"; real-kill + ENOSPC remain T3-DEF-3/4 inventory
  items for post-T3 batch
- m01 re-verification pending QA

## References

  sw-block/design/bugs/005_backend_close_cross_session.md (full post-mortem)
  V2 reference: weed/storage/blockvol/nvme/{server.go,controller.go}
  Memory: feedback_porting_discipline.md updated — new-abstraction
          ownership drift citation added (third citation; BUG-001 +
          pragmatic-evolution-revert + this one)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 20:25:27 -07:00
Languages
Go 84.5%
Rust 7.9%
Shell 2.7%
templ 1.6%
Java 1%
Other 2%