FluentModbus
FluentModbus copied to clipboard
NetworkStream disposed
Hi, we are using FluentModbus to connect 24/24 7/7 on a machine. The problem is, after some days of works the library fails with the NetworkStream disposed. Do you have any idea on when this could happen? I have no further informations as for now since it's difficult to reproduce, we'll try with continuous connections and disconnections to avoid the object being disposed
Hi, maybe a stack trace could help finding the cause of the issue. Disposing should only happen if the connection times out, I do not think that there is an OutOfMemoryException
or a stack overflow that could also maybe lead to disposed network stream.
Which version of FluentModbus do you use and how are you working with the client? Are you disposing the client maybe accidentially?
I'm trying to get the stacktrace. Apart from this, the usage is something like this
while (!token.IsCancellationRequested)
{
while (!client.IsConnected)
{
try
{
client.Connect();
}
catch (Exception e)
{
// log
}
await Task.Delay(Configuration.Polling, token);
}
try
{
// do stuff
}
catch (Exception e)
{
// log
}
await Task.Delay(Configuration.Polling, token);
resetCounter++;
if (resetCounter== resetMax)
{
resetCounter= 0;
client.Disconnect();
}
}
if (client.IsConnected)
{
client.Disconnect();
}
client.Dispose();
And, since the error is repeated continuously, there should not be accidental disposing I guess
Hi @Apollo3zehn, we managed to corner the issue a bit more. After some tweaking, it's now a NullReferenceException inside the ModbusTcpClient while awaiting ReadHoldingRegistersAsync
in FluentModbus.ModbusTcpClient.<TransceiveFrameAsync>d__40.MoveNext()
in FluentModbus.ModbusClient.<ReadHoldingRegistersAsync>d__30.MoveNext()
in FluentModbus.ModbusClient.<ReadHoldingRegistersAsync>d__29`1.MoveNext()
in MyNamespace.MyClass.MyMethod.<ReadStandardMatrixAsync>d__7.MoveNext() in
where the parameters are:
- unit: 1
- startingAddress: 1000
- count: 100
and the session properties are:
- IsConnected: true
- ConnectTimeout: 1000
- ReadTimeout: -1
- WriteTimeout: -1
This happens really randomly so it's really difficult to reproduce
hi, when I quickly switch between server-side connection states, this problem may occur
i think, IsConnected
check TcpClient
connected
when TcpClient
connection is ready , but _networkStream
not ready ,
I did this
FluentModbus.ModbusTcpClient client = null;
while (true)
{
try
{
if (client == null || !client.IsConnected)
{
Console.WriteLine("reconnect ...");
client = new FluentModbus.ModbusTcpClient();
client.Connect("127.0.0.1:502");
Thread.Sleep(200);
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
continue;
}
try
{
var source = client.ReadHoldingRegisters<byte>(1, 40100, 10);
Console.WriteLine(BytesToString(source));
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
client.Disconnect(); // Add this line
}
Thread.Sleep(50);
}