Skip to content

[Bug]: Modbus - Number of threads repetitively increase at each reconnection. #2229

@frankpuppa

Description

@frankpuppa

What happened?

While I was testing the stability of a modbus application based on plc4x, I faced the problem that on successful reconnection attempts the number of live threads keeps increasing continuously. Specifically, there is a pool as such: pool-N-thread-M (probably related to netty) which sits waiting and does not shut down.
The same applies when using the CachedPlcConnectionManager
Connection string: modbus-tcp://127.0.0.1:9081?unit-identifier=1

Here is a sample code with springboot:

 @Scheduled(fixedDelayString = "${scheduling.interval}") 
    public void update() {
        logger.info("Executing update method...")

        try {
            reconnect(false);

            if (plcConnection == null || !plcConnection.isConnected()) {
                logger.info("PLC connection is not alive, exiting...");
                return;
            }

            logger.info("OK");
        } catch (Exception e) {
            logger.error("Error while trying to read ... reconnecting", e);
        }
    }

      protected void reconnect(boolean error) {

        try {
            try {
                if (plcConnection != null && !plcConnection.isConnected()) {
                    logger.info("Reconnecting...");
                    // either with plcConnection.connect() or
                    // connectionManager.getConnection(plcConnectionString); There is a n increment
                    // of threads
                    // plcConnection = connectionManager.getConnection(plcConnectionString);
                    plcConnection.connect();
                    return;
                }
            } catch (Exception e) {
                logger.error("Error: {}", e.getMessage());
                return;
            }

            // Open a new one
            if (plcConnection == null) {
                logger.info("Connecting...");
                plcConnection = connectionManager.getConnection(plcConnectionString);
            }
        } catch (Exception ex) {
            logger.error("Error while trying to reconnect to the plc");
        }
    }

The function runs with a 10 seconds interval, in this time window I simulate the disconnection/reconnection of the cable.
I'm attaching a small video which shows this behavior more clearly.

I understand that this is a corner case but it can lead to a waste of resources which I would like to avoid.

Screencast.2025-08-27.13.38.08.mp4

Thank you.

Version

v0.13.0

Programming Languages

  • plc4c
  • plc4go
  • plc4j
  • plc4net
  • plc4py

Protocols

  • AB-Ethernet
  • ADS /AMS
  • BACnet/IP
  • C-Bus
  • CANopen
  • EtherNet/IP
  • Firmata
  • IEC-69870
  • KNXnet/IP
  • Modbus
  • OPC-UA
  • Profinet
  • S7
  • S7-light

Metadata

Metadata

Assignees

No one assigned

    Labels

    Modbushttps://plc4x.apache.org/users/protocols/modbus.htmljavaPull requests that update Java code

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions