AleiynikovPavel opened a new issue, #1443: URL: https://github.com/apache/plc4x/issues/1443
### What happened? Error decoding TCP packet after N read requests. ## Example code causing the bug ### Description - I get an error both on the real device and on the [Modbus Slave Simulator](https://www.modbusdriver.com/diagslave.html) running with option `-m enc` - I **always** get it at the **40th** iteration of the loop presented in the code example - There is no error if you execute only one request per loop iteration (comment out the second request) - There are also no errors in `modbus-tcp` mode - There is also no error if both the first and second commands request the same number of registers ### Code ```java package org.test.plc4j; import org.apache.plc4x.java.DefaultPlcDriverManager; import org.apache.plc4x.java.api.exceptions.PlcConnectionException; import java.util.concurrent.ExecutionException; public class Main { public static void main(String[] args) throws PlcConnectionException { var connection = new DefaultPlcDriverManager().getConnection("modbus-rtu:tcp://localhost:502"); for (int i = 0; i < 45; i++) { try { connection.readRequestBuilder() .addTagAddress("1", "input-register:1[125]").build().execute().get(); } catch (InterruptedException | ExecutionException e) { System.err.println("Failed command 1 on iteration " + i); } try { connection.readRequestBuilder() .addTagAddress("1", "input-register:1[50]").build().execute().get(); } catch (InterruptedException | ExecutionException e) { System.err.println("Failed command 2 on iteration " + i); } } } } ``` ### Output log ```bash 21:49:50.787 [nioEventLoopGroup-2-1] WARN org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec -- Error decoding package with content [0104fa000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]: Index 240 out of bounds for length 240 java.lang.ArrayIndexOutOfBoundsException: Index 240 out of bounds for length 240 at com.github.jinahya.bit.io.ArrayByteInput.read(ArrayByteInput.java:106) at com.github.jinahya.bit.io.DefaultBitInput.read(DefaultBitInput.java:57) at com.github.jinahya.bit.io.AbstractBitInput.unsigned8(AbstractBitInput.java:63) at com.github.jinahya.bit.io.AbstractBitInput.unsigned16(AbstractBitInput.java:92) at com.github.jinahya.bit.io.AbstractBitInput.readInt(AbstractBitInput.java:135) at com.github.jinahya.bit.io.AbstractBitInput.readInt(AbstractBitInput.java:118) at com.github.jinahya.bit.io.AbstractBitInput.readByte(AbstractBitInput.java:106) at org.apache.plc4x.java.spi.generation.ReadBufferByteBased.readSignedByte(ReadBufferByteBased.java:388) at org.apache.plc4x.java.spi.generation.ReadBuffer.readByte(ReadBuffer.java:44) at org.apache.plc4x.java.spi.generation.ReadBufferByteBased.readByteArray(ReadBufferByteBased.java:126) at org.apache.plc4x.java.modbus.readwrite.ModbusPDUReadInputRegistersResponse.staticParseModbusPDUBuilder(ModbusPDUReadInputRegistersResponse.java:112) at org.apache.plc4x.java.modbus.readwrite.ModbusPDU.staticParse(ModbusPDU.java:171) at org.apache.plc4x.java.modbus.readwrite.ModbusRtuADU.lambda$0(ModbusRtuADU.java:132) at org.apache.plc4x.java.spi.codegen.io.DataReaderComplexDefault.read(DataReaderComplexDefault.java:70) at org.apache.plc4x.java.spi.codegen.io.DataReaderComplexDefault.read(DataReaderComplexDefault.java:61) at org.apache.plc4x.java.spi.codegen.fields.FieldReaderSimple.lambda$0(FieldReaderSimple.java:34) at org.apache.plc4x.java.spi.codegen.FieldCommons.switchParseByteOrderIfNecessary(FieldCommons.java:52) at org.apache.plc4x.java.spi.codegen.fields.FieldReaderSimple.readSimpleField(FieldReaderSimple.java:34) at org.apache.plc4x.java.spi.codegen.fields.FieldReaderFactory.readSimpleField(FieldReaderFactory.java:133) at org.apache.plc4x.java.modbus.readwrite.ModbusRtuADU.staticParseModbusADUBuilder(ModbusRtuADU.java:129) at org.apache.plc4x.java.modbus.readwrite.ModbusADU.staticParse(ModbusADU.java:117) at org.apache.plc4x.java.modbus.readwrite.ModbusADU.staticParse(ModbusADU.java:103) at org.apache.plc4x.java.modbus.rtu.ModbusRtuDriver.lambda$0(ModbusRtuDriver.java:130) at org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec.decode(GeneratedDriverByteToMessageCodec.java:87) at io.netty.handler.codec.ByteToMessageCodec$1.decode(ByteToMessageCodec.java:42) at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:529) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:468) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290) at io.netty.handler.codec.ByteToMessageCodec.channelRead(ByteToMessageCodec.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:833) 21:49:50.789 [nioEventLoopGroup-2-1] DEBUG org.apache.plc4x.java.spi.codegen.fields.FieldReaderSimple -- reading field address 21:49:50.790 [nioEventLoopGroup-2-1] DEBUG org.apache.plc4x.java.spi.codegen.fields.FieldReaderSimple -- reading field pdu 21:49:50.790 [nioEventLoopGroup-2-1] DEBUG org.apache.plc4x.java.spi.codegen.fields.FieldReaderDiscriminator -- reading field errorFlag 21:49:50.790 [nioEventLoopGroup-2-1] DEBUG org.apache.plc4x.java.spi.codegen.fields.FieldReaderDiscriminator -- reading field functionFlag 21:49:50.790 [nioEventLoopGroup-2-1] WARN org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec -- Error decoding package with content [00000000000000000000000000f0a3]: Unsupported case for discriminated type parameters [errorFlag=false functionFlag=0 response=true] org.apache.plc4x.java.spi.generation.ParseException: Unsupported case for discriminated type parameters [errorFlag=false functionFlag=0 response=true] at org.apache.plc4x.java.modbus.readwrite.ModbusPDU.staticParse(ModbusPDU.java:310) at org.apache.plc4x.java.modbus.readwrite.ModbusRtuADU.lambda$0(ModbusRtuADU.java:132) at org.apache.plc4x.java.spi.codegen.io.DataReaderComplexDefault.read(DataReaderComplexDefault.java:70) at org.apache.plc4x.java.spi.codegen.io.DataReaderComplexDefault.read(DataReaderComplexDefault.java:61) at org.apache.plc4x.java.spi.codegen.fields.FieldReaderSimple.lambda$0(FieldReaderSimple.java:34) at org.apache.plc4x.java.spi.codegen.FieldCommons.switchParseByteOrderIfNecessary(FieldCommons.java:52) at org.apache.plc4x.java.spi.codegen.fields.FieldReaderSimple.readSimpleField(FieldReaderSimple.java:34) at org.apache.plc4x.java.spi.codegen.fields.FieldReaderFactory.readSimpleField(FieldReaderFactory.java:133) at org.apache.plc4x.java.modbus.readwrite.ModbusRtuADU.staticParseModbusADUBuilder(ModbusRtuADU.java:129) at org.apache.plc4x.java.modbus.readwrite.ModbusADU.staticParse(ModbusADU.java:117) at org.apache.plc4x.java.modbus.readwrite.ModbusADU.staticParse(ModbusADU.java:103) at org.apache.plc4x.java.modbus.rtu.ModbusRtuDriver.lambda$0(ModbusRtuDriver.java:130) at org.apache.plc4x.java.spi.GeneratedDriverByteToMessageCodec.decode(GeneratedDriverByteToMessageCodec.java:87) at io.netty.handler.codec.ByteToMessageCodec$1.decode(ByteToMessageCodec.java:42) at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:529) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:468) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290) at io.netty.handler.codec.ByteToMessageCodec.channelRead(ByteToMessageCodec.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:833) Failed command 1 on iteration 20 ``` ### Full log file - Full log of the program: [out.txt](https://github.com/apache/plc4x/files/14500068/out.txt) ### Version v0.12.0 ### Programming Languages - [X] plc4j - [ ] plc4go - [ ] plc4c - [ ] plc4net ### Protocols - [ ] AB-Ethernet - [ ] ADS /AMS - [ ] BACnet/IP - [ ] CANopen - [ ] DeltaV - [ ] DF1 - [ ] EtherNet/IP - [ ] Firmata - [ ] KNXnet/IP - [X] Modbus - [ ] OPC-UA - [ ] S7 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@plc4x.apache.org.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org