From 9349727e95b602bd529b9a5b6e99cc3b49fc385a Mon Sep 17 00:00:00 2001 From: Bart Van Bos Date: Mon, 16 Nov 2026 03:50:37 +0100 Subject: [PATCH] fix(scanner): ErrNotConnected when the session ends after Forward_Open (v0.8.3) Open took the target's address from the client's session after the Forward_Open reply. When the session had ended in between, the address was nil and the I/O connection was refused with ErrInvalidOption ("production without an interval or destination"), an error about the configuration. Open now reports ErrNotConnected and tries the Forward_Close, as on its other failures after a Forward_Open. The chaos test of e2e met this on the pin pass pull request #33 (ci, Lint & Coverage), as a connection killed at that moment. TestOpenSessionEndedAfterForwardOpen ends the session at that step through a package variable (remoteAddr) and fails without the fix with the error CI saw. Co-Authored-By: Claude Opus 5.5 --- RELEASE.md | 31 +++++++++++++++++++++++++++++++ scanner/scanner.go | 14 +++++++++++++- scanner/scanner_test.go | 20 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/RELEASE.md b/RELEASE.md index 20b1be9..42bfd2e 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,5 +1,36 @@ # go-ethernetip Releases +## v0.8.3 + +**Date:** 2026-11-16 +**Previous release:** v0.8.2 + +## Summary + +A patch release on v0.8.2. No exported identifier is added, removed or +changed. + +## Changes + +### Fixes + +- **scanner**: `Open` reports `ErrNotConnected` when the client's session + ends between the Forward_Open reply and the opening of the I/O + connection. It returned `ErrInvalidOption` ("production without an + interval or destination"), an error about the configuration, for what + is a lost connection: the target's address, taken from the session, was + gone. The Forward_Close is still tried, as on the other failures after + a Forward_Open. The chaos test of `e2e` met this as a connection killed + at that moment. + +### Tests + +- `TestOpenSessionEndedAfterForwardOpen`: a session that has ended after + the reply gives `ErrNotConnected`, not `ErrInvalidOption`, and one + Forward_Close request. + +--- + ## v0.8.2 **Date:** 2026-11-16 diff --git a/scanner/scanner.go b/scanner/scanner.go index c60497a..dc089ea 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -479,7 +479,15 @@ func (io *IO) open(ctx context.Context) error { _ = c.ForwardClose(ctx, cip.ForwardClose{Triad: fo.Triad, Path: path}) return err } - target := targetIP(c.RemoteAddr()) + target := targetIP(remoteAddr(c)) + if !target.IsValid() { + // The session ended after the reply: the I/O connection has no + // target to send to or accept from. That is a connection error, not + // an invalid option; the Forward_Close goes out if the client + // reconnects in time, else the target's connection times out. + _ = c.ForwardClose(ctx, cip.ForwardClose{Triad: fo.Triad, Path: path}) + return fmt.Errorf("%w: the session ended after the Forward_Open reply", ethernetip.ErrNotConnected) + } dst := netip.AddrPortFrom(target, encap.IOPort) var group netip.Addr for _, it := range rItems { @@ -599,6 +607,10 @@ func checkAPIs(reply cip.ForwardOpenReply, otRPI, floor time.Duration) error { return nil } +// remoteAddr is the target's end of the client's session, or nil when the +// client has none. A test replaces it to end the session at a given step. +var remoteAddr = (*client.Client).RemoteAddr + func targetIP(a net.Addr) netip.Addr { if ta, ok := a.(*net.TCPAddr); ok { if ip, ok := netip.AddrFromSlice(ta.IP); ok { diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 582a9eb..1ab6f5f 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -581,6 +581,26 @@ func okReply(fo cip.ForwardOpen) cip.ForwardOpenReply { return cip.ForwardOpenReply{OTConnID: 0x77, TOConnID: fo.TOConnID, Triad: fo.Triad, OTAPI: fo.OTRPI, TOAPI: fo.TORPI} } +// The session ends between the Forward_Open reply and the opening of the +// I/O connection (the chaos test met it as a connection killed at that +// moment): Open reports ErrNotConnected, not ErrInvalidOption, and tries +// the Forward_Close. +func TestOpenSessionEndedAfterForwardOpen(t *testing.T) { + f := newFakeTarget(t, func(fo cip.ForwardOpen) (cip.ForwardOpenReply, []encap.Item) { return okReply(fo), nil }) + c := dial(t, f.ln.Addr().String()) + s := newScanner(t, c) + saved := remoteAddr + remoteAddr = func(*client.Client) net.Addr { return nil } + t.Cleanup(func() { remoteAddr = saved }) + io, err := s.Open(ctxT(t), owner(5*time.Millisecond)) + if !errors.Is(err, ethernetip.ErrNotConnected) || errors.Is(err, ethernetip.ErrInvalidOption) { + t.Fatalf("Open = %v, %v; want ErrNotConnected", io, err) + } + if f.closes.Load() != 1 { + t.Fatalf("%d Forward_Close requests, want 1", f.closes.Load()) + } +} + func TestMulticastWithoutGroup(t *testing.T) { f := newFakeTarget(t, func(fo cip.ForwardOpen) (cip.ForwardOpenReply, []encap.Item) { // A malformed item and a unicast T->O address: no group.