Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkStream disposed #82

Open
manuelelucchi opened this issue Oct 17, 2022 · 4 comments
Open

NetworkStream disposed #82

manuelelucchi opened this issue Oct 17, 2022 · 4 comments

Comments

@manuelelucchi
Copy link

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

@Apollo3zehn
Copy link
Owner

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?

@manuelelucchi
Copy link
Author

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

@manuelelucchi
Copy link
Author

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(unit, address, count, token)

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

@sharpbetter
Copy link

sharpbetter commented Mar 8, 2023

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 _networkStreamnot ready ,

image

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants