You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2025/03/18 11:24:52 UTC

(plc4x) branch develop updated (515e119c7c -> 72d7a92052)

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


    from 515e119c7c feat(plc4go): add inProgressFor time key for progress warn logs
     new 2630bd10d4 fix(plc4go/spi): multiple potential fixes in Net
     new 72d7a92052 fix(plc4go): ensure channel close is handled correctly

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 plc4go/internal/ads/Interactions.go                |  27 ++-
 .../internal/bacnetip/bacgopes/iocb/iocb_IOCB.go   |   4 +-
 .../bacnetip/bacgopes/iocb/iocb_IOQueue.go         |   2 +-
 .../state_machine/state_machine_StateMachine.go    |   4 +
 plc4go/internal/cbus/Connection.go                 |   8 +
 plc4go/internal/cbus/Discoverer.go                 |   8 +
 plc4go/internal/knxnetip/Discoverer.go             |   4 +
 plc4go/pkg/api/cache/PlcConnectionCache.go         |  37 +++--
 plc4go/pkg/api/cache/PlcConnectionCache_test.go    | 121 ++++++++------
 plc4go/pkg/api/cache/plcConnectionLease_test.go    | 182 +++++++++++----------
 plc4go/spi/utils/Net.go                            |  56 +++----
 11 files changed, 256 insertions(+), 197 deletions(-)


(plc4x) 02/02: fix(plc4go): ensure channel close is handled correctly

Posted by sr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 72d7a92052782e8071afe8b638fa0a32ded7c200
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Mar 18 12:10:58 2025 +0100

    fix(plc4go): ensure channel close is handled correctly
---
 plc4go/internal/ads/Interactions.go                |  27 ++-
 .../internal/bacnetip/bacgopes/iocb/iocb_IOCB.go   |   4 +-
 .../bacnetip/bacgopes/iocb/iocb_IOQueue.go         |   2 +-
 .../state_machine/state_machine_StateMachine.go    |   4 +
 plc4go/internal/cbus/Connection.go                 |   8 +
 plc4go/internal/cbus/Discoverer.go                 |   8 +
 plc4go/internal/knxnetip/Discoverer.go             |   4 +
 plc4go/pkg/api/cache/PlcConnectionCache.go         |  37 +++--
 plc4go/pkg/api/cache/PlcConnectionCache_test.go    | 121 ++++++++------
 plc4go/pkg/api/cache/plcConnectionLease_test.go    | 182 +++++++++++----------
 10 files changed, 228 insertions(+), 169 deletions(-)

diff --git a/plc4go/internal/ads/Interactions.go b/plc4go/internal/ads/Interactions.go
index 2829f9a99a..4b25092c62 100644
--- a/plc4go/internal/ads/Interactions.go
+++ b/plc4go/internal/ads/Interactions.go
@@ -22,6 +22,7 @@ package ads
 import (
 	"context"
 	"fmt"
+	"github.com/apache/plc4x/plc4go/spi/utils"
 	"runtime/debug"
 	"time"
 
@@ -63,9 +64,8 @@ func (m *Connection) ExecuteAdsReadDeviceInfoRequest(ctx context.Context) (model
 				return nil
 			},
 			time.Second); err != nil {
+			m.log.Debug().Err(err).Msg("error during send request")
 			close(responseChannel)
-		} else {
-			//			close(responseChannel)
 		}
 	}()
 	response, err := ReadWithTimeout(ctx, responseChannel)
@@ -109,9 +109,8 @@ func (m *Connection) ExecuteAdsReadRequest(ctx context.Context, indexGroup uint3
 				return nil
 			},
 			time.Second*5); err != nil {
+			m.log.Debug().Err(err).Msg("error during send request")
 			close(responseChannel)
-		} else {
-			//			close(responseChannel)
 		}
 	}()
 	response, err := ReadWithTimeout(ctx, responseChannel)
@@ -155,9 +154,8 @@ func (m *Connection) ExecuteAdsWriteRequest(ctx context.Context, indexGroup uint
 				return nil
 			},
 			time.Second); err != nil {
+			m.log.Debug().Err(err).Msg("error during send request")
 			close(responseChannel)
-		} else {
-			//			close(responseChannel)
 		}
 	}()
 	response, err := ReadWithTimeout(ctx, responseChannel)
@@ -201,9 +199,8 @@ func (m *Connection) ExecuteAdsReadWriteRequest(ctx context.Context, indexGroup
 				return nil
 			},
 			time.Second); err != nil {
+			m.log.Debug().Err(err).Msg("error during send request")
 			close(responseChannel)
-		} else {
-			//			close(responseChannel)
 		}
 	}()
 	response, err := ReadWithTimeout(ctx, responseChannel)
@@ -247,9 +244,8 @@ func (m *Connection) ExecuteAdsAddDeviceNotificationRequest(ctx context.Context,
 				return nil
 			},
 			time.Second); err != nil {
+			m.log.Debug().Err(err).Msg("error during send request")
 			close(responseChannel)
-		} else {
-			//			close(responseChannel)
 		}
 	}()
 	response, err := ReadWithTimeout(ctx, responseChannel)
@@ -293,9 +289,8 @@ func (m *Connection) ExecuteAdsDeleteDeviceNotificationRequest(ctx context.Conte
 				return nil
 			},
 			time.Second); err != nil {
+			m.log.Debug().Err(err).Msg("error during send request")
 			close(responseChannel)
-		} else {
-			//			close(responseChannel)
 		}
 	}()
 	response, err := ReadWithTimeout(ctx, responseChannel)
@@ -311,9 +306,13 @@ func ReadWithTimeout[T spi.Message](ctx context.Context, ch <-chan T) (T, error)
 
 	select {
 	case m := <-ch:
+		if utils.IsNil(m) {
+			var zero T
+			return zero, fmt.Errorf("channel closed")
+		}
 		return m, nil
 	case <-timeout.Done():
-		var t T
-		return t, fmt.Errorf("timeout")
+		var zero T
+		return zero, fmt.Errorf("timeout")
 	}
 }
diff --git a/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOCB.go b/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOCB.go
index 95d3a30436..5efdffb02c 100644
--- a/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOCB.go
+++ b/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOCB.go
@@ -91,7 +91,7 @@ type IOCB struct {
 	ioCallback     []func() `ignore:"true"`
 	ioQueue        []IOCBContract
 	ioTimeout      *time.Timer
-	ioTimoutCancel chan any
+	ioTimoutCancel chan struct{}
 	priority       int
 
 	wg sync.WaitGroup
@@ -250,7 +250,7 @@ func (i *IOCB) SetTimeout(delay time.Duration) {
 	} else {
 		now := GetTaskManagerTime()
 		i.ioTimeout = time.NewTimer(delay)
-		i.ioTimoutCancel = make(chan any)
+		i.ioTimoutCancel = make(chan struct{})
 		i.wg.Add(1)
 		go func() {
 			defer i.wg.Done()
diff --git a/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOQueue.go b/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOQueue.go
index e0cfc525fd..440e7afc26 100644
--- a/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOQueue.go
+++ b/plc4go/internal/bacnetip/bacgopes/iocb/iocb_IOQueue.go
@@ -86,7 +86,7 @@ func (i *IOQueue) Get(block bool, delay *time.Duration) (IOCBContract, error) {
 	// wait for something to be in the queue
 	if len(i.Queue) == 0 {
 		if delay != nil {
-			gotSomething := make(chan any)
+			gotSomething := make(chan struct{})
 			i.wg.Add(1)
 			go func() {
 				defer i.wg.Done()
diff --git a/plc4go/internal/bacnetip/bacgopes/tests/state_machine/state_machine_StateMachine.go b/plc4go/internal/bacnetip/bacgopes/tests/state_machine/state_machine_StateMachine.go
index 3d34f4b2f0..c711f01309 100644
--- a/plc4go/internal/bacnetip/bacgopes/tests/state_machine/state_machine_StateMachine.go
+++ b/plc4go/internal/bacnetip/bacgopes/tests/state_machine/state_machine_StateMachine.go
@@ -580,6 +580,10 @@ func (s *stateMachine) gotoState(state State) error {
 		for s.running {
 			select {
 			case pdu := <-s.transitionQueue:
+				if pdu == nil {
+					s.log.Trace().Msg("nil value received, channel closed")
+					break queueRead
+				}
 				if _debug != nil {
 					_debug("    - pdu: %r", pdu)
 				}
diff --git a/plc4go/internal/cbus/Connection.go b/plc4go/internal/cbus/Connection.go
index 7569e05404..163aa75bdd 100644
--- a/plc4go/internal/cbus/Connection.go
+++ b/plc4go/internal/cbus/Connection.go
@@ -288,6 +288,10 @@ func (c *Connection) startSubscriptionHandler() {
 		salLogger.Debug().Msg("SAL handler started")
 		for c.IsConnected() {
 			for monitoredSal := range c.messageCodec.monitoredSALs {
+				if monitoredSal == nil {
+					salLogger.Trace().Msg("monitoredSal chan closed")
+					break
+				}
 				salLogger.Trace().
 					Stringer("monitoredSal", monitoredSal).
 					Msg("got a SAL")
@@ -328,6 +332,10 @@ func (c *Connection) startSubscriptionHandler() {
 		mmiLogger.Debug().Msg("default MMI started")
 		for c.IsConnected() {
 			for calReply := range c.messageCodec.monitoredMMIs {
+				if calReply == nil {
+					mmiLogger.Trace().Msg("channel closed")
+					break
+				}
 				mmiLogger.Trace().Msg("got a MMI")
 				handled := false
 				for _, subscriber := range c.subscribers {
diff --git a/plc4go/internal/cbus/Discoverer.go b/plc4go/internal/cbus/Discoverer.go
index 24b398cabf..0f92d88f86 100644
--- a/plc4go/internal/cbus/Discoverer.go
+++ b/plc4go/internal/cbus/Discoverer.go
@@ -158,6 +158,10 @@ func (d *Discoverer) Discover(ctx context.Context, callback func(event apiModel.
 							addressLogger.Debug().Err(err).Msg("ending")
 							return
 						}
+						if ip == nil {
+							addressLogger.Trace().Msg("nil value received, channel closed")
+							continue
+						}
 						addressLogger.Trace().IPAddr("ip", ip).Msg("Handling found ip")
 						d.transportInstanceCreationQueue.Submit(
 							ctx,
@@ -199,6 +203,10 @@ func (d *Discoverer) Discover(ctx context.Context, callback func(event apiModel.
 		}()
 		deviceScanWg := new(sync.WaitGroup)
 		for transportInstance := range transportInstances {
+			if transportInstance == nil {
+				d.log.Trace().Msg("nil value received, channel closed")
+				continue
+			}
 			if err := ctx.Err(); err != nil {
 				d.log.Debug().Err(err).Msg("ending")
 				return
diff --git a/plc4go/internal/knxnetip/Discoverer.go b/plc4go/internal/knxnetip/Discoverer.go
index 8164587724..ff19b16e34 100644
--- a/plc4go/internal/knxnetip/Discoverer.go
+++ b/plc4go/internal/knxnetip/Discoverer.go
@@ -178,6 +178,10 @@ func (d *Discoverer) Discover(ctx context.Context, callback func(event apiModel.
 			}
 		}()
 		for transportInstance := range transportInstances {
+			if transportInstance == nil {
+				d.log.Trace().Msg("channel closed")
+				break
+			}
 			d.deviceScanningQueue.Submit(ctx, d.deviceScanningWorkItemId.Add(1), d.createDeviceScanDispatcher(ctx, transportInstance.(*udp.TransportInstance), callback))
 		}
 	}()
diff --git a/plc4go/pkg/api/cache/PlcConnectionCache.go b/plc4go/pkg/api/cache/PlcConnectionCache.go
index 65a15e645e..7a07f0aff6 100644
--- a/plc4go/pkg/api/cache/PlcConnectionCache.go
+++ b/plc4go/pkg/api/cache/PlcConnectionCache.go
@@ -49,13 +49,14 @@ func NewPlcConnectionCache(driverManager plc4go.PlcDriverManager, withConnection
 	}
 	maxLeaseTime := 5 * time.Second
 	cc := &plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  maxLeaseTime,
-		maxWaitTime:   maxLeaseTime * 5,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
-		log:           log,
+		driverManager:       driverManager,
+		maxLeaseTime:        maxLeaseTime,
+		maxWaitTime:         maxLeaseTime * 5,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
+		log:                 log,
 		// _options:   _options, // TODO: we might want to migrate the connection cache options to proper options
 	}
 	for _, option := range withConnectionCacheOptions {
@@ -66,15 +67,22 @@ func NewPlcConnectionCache(driverManager plc4go.PlcDriverManager, withConnection
 
 type WithConnectionCacheOption func(plcConnectionCache *plcConnectionCache)
 
-func WithMaxLeaseTime(duration time.Duration) WithConnectionCacheOption {
+func WithMaxLeaseTime(maxLeaseTime time.Duration) WithConnectionCacheOption {
 	return func(plcConnectionCache *plcConnectionCache) {
-		plcConnectionCache.maxLeaseTime = duration
+		plcConnectionCache.maxLeaseTime = maxLeaseTime
 	}
 }
 
-func WithMaxWaitTime(duration time.Duration) WithConnectionCacheOption {
+func WithMaxWaitTime(maxWaitTime time.Duration) WithConnectionCacheOption {
 	return func(plcConnectionCache *plcConnectionCache) {
-		plcConnectionCache.maxLeaseTime = duration
+		plcConnectionCache.maxLeaseTime = maxWaitTime
+	}
+}
+
+// WithMaxResponseGrabTimeout defines the time a client has to grab the response from the chan before the connection expires (10ms by default)
+func WithMaxResponseGrabTimeout(responseGrabTimeout time.Duration) WithConnectionCacheOption {
+	return func(plcConnectionCache *plcConnectionCache) {
+		plcConnectionCache.responseGrabTimeout = responseGrabTimeout
 	}
 }
 
@@ -106,8 +114,9 @@ type plcConnectionCache struct {
 
 	// Maximum duration a connection can be used per lease.
 	// If the connection is used for a longer time, it is forcefully removed from the client.
-	maxLeaseTime time.Duration
-	maxWaitTime  time.Duration
+	maxLeaseTime        time.Duration
+	maxWaitTime         time.Duration
+	responseGrabTimeout time.Duration
 
 	cacheLock   lock.RWMutex
 	connections map[string]*connectionContainer
@@ -199,7 +208,7 @@ func (c *plcConnectionCache) GetConnectionWithContext(ctx context.Context, conne
 				Str("connectionString", connectionString).
 				Stringer("connectionResponse", connectionResponse).
 				Msg("Successfully got lease to connection")
-			responseTimeout := time.NewTimer(10 * time.Millisecond)
+			responseTimeout := time.NewTimer(c.responseGrabTimeout)
 			select {
 			case ch <- connectionResponse:
 				if c.tracer != nil {
diff --git a/plc4go/pkg/api/cache/PlcConnectionCache_test.go b/plc4go/pkg/api/cache/PlcConnectionCache_test.go
index acc92f478b..d0f7ac1ff9 100644
--- a/plc4go/pkg/api/cache/PlcConnectionCache_test.go
+++ b/plc4go/pkg/api/cache/PlcConnectionCache_test.go
@@ -217,6 +217,10 @@ func (c *plcConnectionCache) readFromPlc(t *testing.T, preConnectJob func(), con
 	connectionResultChan := c.GetConnection(connectionString)
 	select {
 	case connectResult := <-connectionResultChan:
+		if connectResult == nil {
+			t.Errorf("channel closed")
+			return nil
+		}
 		if connectResult.GetErr() != nil {
 			t.Errorf("PlcConnectionCache.GetConnection() error = %v", connectResult.GetErr())
 			return nil
@@ -295,12 +299,13 @@ func TestPlcConnectionCache_ReusingAnExistingConnection(t *testing.T) {
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -372,12 +377,13 @@ func TestPlcConnectionCache_MultipleConcurrentConnectionRequests(t *testing.T) {
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -467,12 +473,13 @@ func TestPlcConnectionCache_ConnectWithError(t *testing.T) {
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -507,12 +514,13 @@ func TestPlcConnectionCache_ReturningConnectionWithPingError(t *testing.T) {
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -568,12 +576,13 @@ func TestPlcConnectionCache_PingTimeout(t *testing.T) {
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -617,12 +626,13 @@ func TestPlcConnectionCache_SecondCallGetNewConnectionAfterPingTimeout(t *testin
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -705,12 +715,13 @@ func TestPlcConnectionCache_FistReadGivesUpBeforeItGetsTheConnectionSoSecondOneT
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -756,12 +767,13 @@ func TestPlcConnectionCache_SecondConnectionGivenUpWaiting(t *testing.T) {
 	})
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  5 * time.Second,
-		maxWaitTime:   25 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        5 * time.Second,
+		maxWaitTime:         25 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
@@ -836,12 +848,13 @@ func TestPlcConnectionCache_MaximumWaitTimeReached(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	cache.EnableTracer()
 
diff --git a/plc4go/pkg/api/cache/plcConnectionLease_test.go b/plc4go/pkg/api/cache/plcConnectionLease_test.go
index 7c6c91f332..cf64bbcb7f 100644
--- a/plc4go/pkg/api/cache/plcConnectionLease_test.go
+++ b/plc4go/pkg/api/cache/plcConnectionLease_test.go
@@ -42,12 +42,13 @@ func TestLeasedPlcConnection_IsTraceEnabled(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -116,12 +117,13 @@ func TestLeasedPlcConnection_GetTracer(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -164,12 +166,13 @@ func TestLeasedPlcConnection_GetConnectionId(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -212,12 +215,13 @@ func TestLeasedPlcConnection_Connect(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -258,12 +262,13 @@ func TestLeasedPlcConnection_BlockingClose(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -305,12 +310,13 @@ func TestLeasedPlcConnection_Close(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -352,12 +358,13 @@ func TestLeasedPlcConnection_IsConnected(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -391,12 +398,13 @@ func TestLeasedPlcConnection_Ping(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -439,12 +447,13 @@ func TestLeasedPlcConnection_GetMetadata(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -491,12 +500,13 @@ func TestLeasedPlcConnection_ReadRequestBuilder(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -540,12 +550,13 @@ func TestLeasedPlcConnection_WriteRequestBuilder(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -589,12 +600,13 @@ func TestLeasedPlcConnection_SubscriptionRequestBuilder(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -638,12 +650,13 @@ func TestLeasedPlcConnection_UnsubscriptionRequestBuilder(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()
@@ -695,12 +708,13 @@ func TestLeasedPlcConnection_BrowseRequestBuilder(t *testing.T) {
 	driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
 	// Reduce the max lease time as this way we also reduce the max wait time.
 	cache := plcConnectionCache{
-		driverManager: driverManager,
-		maxLeaseTime:  1 * time.Second,
-		maxWaitTime:   5 * time.Second,
-		cacheLock:     lock.NewCASMutex(),
-		connections:   make(map[string]*connectionContainer),
-		tracer:        nil,
+		driverManager:       driverManager,
+		maxLeaseTime:        1 * time.Second,
+		maxWaitTime:         5 * time.Second,
+		responseGrabTimeout: 10 * time.Millisecond,
+		cacheLock:           lock.NewCASMutex(),
+		connections:         make(map[string]*connectionContainer),
+		tracer:              nil,
 	}
 	t.Cleanup(func() {
 		<-cache.Close()


(plc4x) 01/02: fix(plc4go/spi): multiple potential fixes in Net

Posted by sr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 2630bd10d401fd4bb2e4c518cd45c9701f94eb13
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Mar 18 11:48:54 2025 +0100

    fix(plc4go/spi): multiple potential fixes in Net
---
 plc4go/spi/utils/Net.go | 56 ++++++++++++++++++++++++-------------------------
 1 file changed, 28 insertions(+), 28 deletions(-)

diff --git a/plc4go/spi/utils/Net.go b/plc4go/spi/utils/Net.go
index 817d604f95..e4bfaeda24 100644
--- a/plc4go/spi/utils/Net.go
+++ b/plc4go/spi/utils/Net.go
@@ -52,13 +52,11 @@ func GetIPAddresses(localLog zerolog.Logger, ctx context.Context, netInterface n
 					Msg("panic-ed")
 			}
 		}()
-		wg := new(sync.WaitGroup)
+		addressWg := new(sync.WaitGroup)
 		for _, address := range addrs {
 			// Check if context has been cancelled before continuing
-			select {
-			case <-ctx.Done():
-				return
-			default:
+			if err := ctx.Err(); err != nil {
+				break
 			}
 
 			var ipnet *net.IPNet
@@ -68,28 +66,30 @@ func GetIPAddresses(localLog zerolog.Logger, ctx context.Context, netInterface n
 			case *net.IPNet:
 				ipnet = v
 			default:
+				localLog.Trace().Stringer("address", address).Msg("Ignoring unknown address")
 				continue
 			}
 
 			// Skip loop-back and IPv6
 			if ipnet.IP.IsLoopback() || ipnet.IP.To4() == nil {
+				localLog.Trace().Stringer("ipNet", ipnet).Msg("Skipping IP address")
 				continue
 			}
 
 			localLog.Debug().Stringer("IP", ipnet.IP).Stringer("Mask", ipnet.Mask).Msg("Expanding local subnet")
 			if useArpBasedScan {
-				if err := lockupIpsUsingArp(localLog, ctx, netInterface, ipnet, foundIps, wg); err != nil {
+				if err := lockupIpsUsingArp(localLog, ctx, netInterface, ipnet, foundIps, addressWg); err != nil {
 					localLog.Error().Err(err).Msg("failing to resolve using arp scan. Falling back to ip based scan")
-					useArpBasedScan = false
+					useArpBasedScan = false // fallback to non arg scan
 				}
 			}
 			if !useArpBasedScan {
-				if err := lookupIps(localLog, ctx, ipnet, foundIps, wg); err != nil {
+				if err := lookupIps(localLog, ctx, ipnet, foundIps, addressWg); err != nil {
 					localLog.Error().Err(err).Msg("error looking up ips")
 				}
 			}
 		}
-		wg.Wait()
+		addressWg.Wait()
 		localLog.Trace().Msg("Closing found ips channel")
 		close(foundIps)
 	}()
@@ -102,13 +102,16 @@ func GetIPAddresses(localLog zerolog.Logger, ctx context.Context, netInterface n
 func lockupIpsUsingArp(localLog zerolog.Logger, ctx context.Context, netInterface net.Interface, ipNet *net.IPNet, foundIps chan net.IP, wg *sync.WaitGroup) error {
 	// We add on signal for error handling
 	wg.Add(1)
-	go func() { wg.Done() }()
+	defer wg.Done()
 	localLog.Debug().
 		Str("netInterfaceName", netInterface.Name).
 		Stringer("ipNet", ipNet).
 		Msg("Scanning for alive IP addresses for interface netInterfaceName and net: ipNet")
 	// First find the pcap device name for the given interface.
-	allDevs, _ := pcap.FindAllDevs()
+	allDevs, err := pcap.FindAllDevs()
+	if err != nil {
+		return errors.Wrap(err, "error finding all interfaces")
+	}
 	var devName string
 	for _, dev := range allDevs {
 		for _, devAddress := range dev.Addresses {
@@ -130,11 +133,11 @@ func lockupIpsUsingArp(localLog zerolog.Logger, ctx context.Context, netInterfac
 	}
 
 	// Start up a goroutine to read in packet data.
-	stop := make(chan struct{}, 1)
-	// As we don't know how much the handler will find we use a value of 1 and set that to done after the 10 sec in the cleanup function directly after
-	wg.Add(1)
+	stop := make(chan struct{})
 	// Handler for processing incoming ARP responses.
+	wg.Add(1)
 	go func(handle *pcap.Handle, iface net.Interface, stop chan struct{}) {
+		defer wg.Done()
 		defer func() {
 			if err := recover(); err != nil {
 				localLog.Error().
@@ -148,6 +151,8 @@ func lockupIpsUsingArp(localLog zerolog.Logger, ctx context.Context, netInterfac
 		for {
 			var packet gopacket.Packet
 			select {
+			case <-ctx.Done():
+				return
 			case <-stop:
 				return
 			case packet = <-in:
@@ -182,7 +187,6 @@ func lockupIpsUsingArp(localLog zerolog.Logger, ctx context.Context, netInterfac
 		wg.Add(1)
 		go func() {
 			defer wg.Done()
-			wg.Done()
 			time.Sleep(10 * time.Second)
 			handle.Close()
 			close(stop)
@@ -215,10 +219,8 @@ func lockupIpsUsingArp(localLog zerolog.Logger, ctx context.Context, netInterfac
 		// Send one ARP packet for every possible address.
 		for ip := IncrementIP(addr.IP.Mask(ipNet.Mask)); addr.Contains(ip) && addr.Contains(IncrementIP(DuplicateIP(ip))); ip = IncrementIP(ip) {
 			// Check if context has been cancelled before continuing
-			select {
-			case <-ctx.Done():
-				return nil
-			default:
+			if err := ctx.Err(); err != nil {
+				return err
 			}
 			arp.DstProtAddress = ip
 			if err := gopacket.SerializeLayers(buf, opts, &eth, &arp); err != nil {
@@ -240,27 +242,25 @@ func lockupIpsUsingArp(localLog zerolog.Logger, ctx context.Context, netInterfac
 
 // Simply takes the IP address and the netmask and schedules one discovery task for every possible IP
 func lookupIps(localLog zerolog.Logger, ctx context.Context, ipnet *net.IPNet, foundIps chan net.IP, wg *sync.WaitGroup) error {
-	localLog.Debug().
-		Stringer("ipnet", ipnet).
-		Msg("Scanning all IP addresses for network")
+	localLog.Debug().Stringer("ipnet", ipnet).Msg("Scanning all IP addresses for network")
 	// expand CIDR-block into one target for each IP
 	// Remark: The last IP address a network contains is a special broadcast address. We don't want to check that one.
 	for ip := IncrementIP(ipnet.IP.Mask(ipnet.Mask)); ipnet.Contains(ip) && ipnet.Contains(IncrementIP(DuplicateIP(ip))); ip = IncrementIP(ip) {
 		// Check if context has been cancelled before continuing
-		select {
-		case <-ctx.Done():
-			return nil
-		default:
+		if err := ctx.Err(); err != nil {
+			return err
 		}
 
-		wg.Add(1)
 		timeout := time.NewTimer(2 * time.Second)
+		wg.Add(1)
 		go func(ip net.IP) {
-			defer func() { wg.Done() }()
+			defer wg.Done()
 			select {
 			case <-ctx.Done():
+				localLog.Trace().Msg("context done")
 			case foundIps <- ip:
 			case <-timeout.C:
+				localLog.Trace().Msg("timed out waiting for IP address")
 			}
 		}(DuplicateIP(ip))
 		localLog.Trace().Stringer("IP", ip).Msg("Expanded CIDR")