fix: handle discovery sessions in UnBindISCSISession

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>
This commit is contained in:
Lei Xue
2026-03-17 15:01:22 +08:00
parent be3cad5aba
commit a7b58b8eb3
2 changed files with 17 additions and 3 deletions

View File

@@ -376,12 +376,19 @@ func (s *ISCSITargetDriver) handler(events byte, conn *iscsiConnection) {
}
func (s *ISCSITargetDriver) clearHostIP(conn *iscsiConnection) {
if conn.conn == nil {
return
}
IPMutex.Lock()
remoteIP := strings.Split(conn.conn.RemoteAddr().String(), ":")[0]
defer IPMutex.Unlock()
addr := conn.conn.RemoteAddr()
if addr == nil {
return
}
remoteIP := strings.Split(addr.String(), ":")[0]
if CurrentHostIP == remoteIP {
CurrentHostIP = ""
}
IPMutex.Unlock()
}
func (s *ISCSITargetDriver) rxHandler(conn *iscsiConnection) {

View File

@@ -330,10 +330,17 @@ func (s *ISCSITargetDriver) LookupISCSISession(tgtName string, iniName string, i
func (s *ISCSITargetDriver) UnBindISCSISession(sess *ISCSISession) {
target := sess.Target
if target == nil {
// Discovery sessions have no target; just release the TSIH.
s.ReleaseTSIH(sess.TSIH)
return
}
target.SessionsRWMutex.Lock()
defer target.SessionsRWMutex.Unlock()
delete(target.Sessions, sess.TSIH)
scsi.RemoveITNexus(sess.Target.SCSITarget, sess.ITNexus)
if sess.ITNexus != nil {
scsi.RemoveITNexus(target.SCSITarget, sess.ITNexus)
}
s.ReleaseTSIH(sess.TSIH)
log.Infof("session %x unbound from target %s", sess.TSIH, target.SCSITarget.Name)
}