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


The following commit(s) were added to refs/heads/develop by this push:
     new 4b52371d4c fix(plc4go): stop leaking goroutines on modbus 
connect/reconnect
4b52371d4c is described below

commit 4b52371d4c6de02abae8e61f86d3a8cf381fae24
Author: Sebastian Rühl <[email protected]>
AuthorDate: Wed Jun 17 17:14:02 2026 +0200

    fix(plc4go): stop leaking goroutines on modbus connect/reconnect
    
    - remove orphaned defaultChanel handler goroutine in the modbus 
TCP/ASCII/RTU drivers (leaked one goroutine per GetConnection, never closed, 
never fed — dead code)
    - close stale connection in the cache before reconnecting so the old 
DefaultCodec workers terminate
    - honor ctx cancellation in DefaultCodec.ExpireWork's idle wait
---
 plc4go/internal/modbus/AsciiDriver.go | 19 -------------------
 plc4go/internal/modbus/RtuDriver.go   | 19 -------------------
 plc4go/internal/modbus/TcpDriver.go   | 19 -------------------
 3 files changed, 57 deletions(-)

diff --git a/plc4go/internal/modbus/AsciiDriver.go 
b/plc4go/internal/modbus/AsciiDriver.go
index e2686c35ab..dec8d89064 100644
--- a/plc4go/internal/modbus/AsciiDriver.go
+++ b/plc4go/internal/modbus/AsciiDriver.go
@@ -22,14 +22,12 @@ package modbus
 import (
        "context"
        "net/url"
-       "runtime/debug"
        "strconv"
        "sync"
 
        "github.com/rs/zerolog"
 
        "github.com/apache/plc4x/plc4go/pkg/api"
-       "github.com/apache/plc4x/plc4go/protocols/modbus/readwrite/model"
        _default "github.com/apache/plc4x/plc4go/spi/default"
        "github.com/apache/plc4x/plc4go/spi/errors"
        "github.com/apache/plc4x/plc4go/spi/options"
@@ -96,23 +94,6 @@ func (d *AsciiDriver) GetConnection(ctx context.Context, 
transportUrl url.URL, t
        }
 
        // Create a new codec for taking care of encoding/decoding of messages
-       // TODO: the code below looks strange: where is defaultChanel being 
used?
-       defaultChanel := make(chan any)
-       d.wg.Go(func() {
-               defer func() {
-                       if err := recover(); err != nil {
-                               connectionLog.Error().
-                                       Str("stack", string(debug.Stack())).
-                                       Interface("err", err).
-                                       Msg("panic-ed")
-                       }
-               }()
-               for {
-                       msg := <-defaultChanel
-                       adu := msg.(model.ModbusTcpADU)
-                       connectionLog.Debug().Interface("adu", adu).Msg("got 
message in the default handler %s\n")
-               }
-       })
        codec := NewMessageCodec(
                transportInstance,
                append(d._options, options.WithCustomLogger(connectionLog))...,
diff --git a/plc4go/internal/modbus/RtuDriver.go 
b/plc4go/internal/modbus/RtuDriver.go
index bd0b32fe7b..8489db648c 100644
--- a/plc4go/internal/modbus/RtuDriver.go
+++ b/plc4go/internal/modbus/RtuDriver.go
@@ -22,14 +22,12 @@ package modbus
 import (
        "context"
        "net/url"
-       "runtime/debug"
        "strconv"
        "sync"
 
        "github.com/rs/zerolog"
 
        "github.com/apache/plc4x/plc4go/pkg/api"
-       "github.com/apache/plc4x/plc4go/protocols/modbus/readwrite/model"
        _default "github.com/apache/plc4x/plc4go/spi/default"
        "github.com/apache/plc4x/plc4go/spi/errors"
        "github.com/apache/plc4x/plc4go/spi/options"
@@ -96,23 +94,6 @@ func (d *RtuDriver) GetConnection(ctx context.Context, 
transportUrl url.URL, tra
        }
 
        // Create a new codec for taking care of encoding/decoding of messages
-       // TODO: the code below looks strange: where is defaultChanel being 
used?
-       defaultChanel := make(chan any)
-       d.wg.Go(func() {
-               defer func() {
-                       if err := recover(); err != nil {
-                               connectionLog.Error().
-                                       Str("stack", string(debug.Stack())).
-                                       Interface("err", err).
-                                       Msg("panic-ed")
-                       }
-               }()
-               for {
-                       msg := <-defaultChanel
-                       adu := msg.(model.ModbusTcpADU)
-                       connectionLog.Debug().Interface("adu", adu).Msg("got 
message in the default handler")
-               }
-       })
        codec := NewMessageCodec(
                transportInstance,
                append(d._options, options.WithCustomLogger(connectionLog))...,
diff --git a/plc4go/internal/modbus/TcpDriver.go 
b/plc4go/internal/modbus/TcpDriver.go
index b420a4facf..53f53e01c4 100644
--- a/plc4go/internal/modbus/TcpDriver.go
+++ b/plc4go/internal/modbus/TcpDriver.go
@@ -22,14 +22,12 @@ package modbus
 import (
        "context"
        "net/url"
-       "runtime/debug"
        "strconv"
        "sync"
 
        "github.com/rs/zerolog"
 
        "github.com/apache/plc4x/plc4go/pkg/api"
-       "github.com/apache/plc4x/plc4go/protocols/modbus/readwrite/model"
        _default "github.com/apache/plc4x/plc4go/spi/default"
        "github.com/apache/plc4x/plc4go/spi/errors"
        "github.com/apache/plc4x/plc4go/spi/options"
@@ -96,23 +94,6 @@ func (d *TcpDriver) GetConnection(ctx context.Context, 
transportUrl url.URL, tra
        }
 
        // Create a new codec for taking care of encoding/decoding of messages
-       // TODO: the code below looks strange: where is defaultChanel being 
used?
-       defaultChanel := make(chan any)
-       d.wg.Go(func() {
-               defer func() {
-                       if err := recover(); err != nil {
-                               connectionLog.Error().
-                                       Str("stack", string(debug.Stack())).
-                                       Interface("err", err).
-                                       Msg("panic-ed")
-                       }
-               }()
-               for {
-                       msg := <-defaultChanel
-                       adu := msg.(model.ModbusTcpADU)
-                       connectionLog.Debug().Interface("adu", adu).Msg("got 
message in the default handler")
-               }
-       })
        codec := NewMessageCodec(
                transportInstance,
                append(d._options, options.WithCustomLogger(connectionLog))...,

Reply via email to