mtconnect / dot_net_sdk

An assembly for the Microsoft .NET framework, based on the MC2 class.
Apache License 2.0
25 stars 23 forks source link

The adapter isn't closing when the agent connession is missing #6

Open lcicala opened 4 years ago

lcicala commented 4 years ago

When the Stop method is called on an instance of the adapter there is a 20-second delay before continuing. After that the application continue running until its closing, but a library thread continue running and it doesn't allow the application to close completely, this issue does not permit the application to run again until the thread is killed by the task manager. This issue is only noticeable when the agent is missing.

lcicala commented 4 years ago

The problem is in the mListenThread thread which calls the TcpListener.AcceptTcpListenerClient method that as reported on the Microsoft official documentation (https://docs.microsoft.com/en-US/dotnet/api/system.net.sockets.tcplistener.accepttcpclient?view=netframework-4.8) is a blocking call, so when the agent is missing the thread is blocked and never ending. The 20-second delay is caused by the Thread.Join method with a timeout of 20000ms called by the adapter Stop method; since the mListenThread is never ending the timeout is fully consumed and the execution continues. In order to fix this issue the TcpListener.AcceptTcpClient call will be executed only when there are pending requests.

hulinning2 commented 4 years ago

You should change to catch the exception since agent is out and the tcp will throw an error. Once you are out the blocking call you can then start listening again. Even agent is still connecting with adapter but Tcp connect could be dropped and create an exception so it would be able to catch an error here and start the listening again.

wsobel commented 4 years ago

This is sample code and we appreciate the feedback.

Please create a fork and send a pull request when you find a solution. Any contributions are greatly appreciated.

Thanks! Will

(Sent from mobile)

On Mar 9, 2020, at 08:30, lcicala notifications@github.com wrote:

 The problem is in the mListenThread thread which calls the TcpListener.AcceptTcpListenerClient method that as reported on the Microsoft official documentation (https://docs.microsoft.com/en-US/dotnet/api/system.net.sockets.tcplistener.accepttcpclient?view=netframework-4.8) is a blocking call, so when the agent is missing the thread is blocked and never ending. The 20-second delay is caused by the Thread.Join method with a timeout of 20000ms called by the adapter Stop method; since the mListenThread is never ending the timeout is fully consumed and the execution continues. In order to fix this issue the TcpListener.AcceptTcpClient call will be executed only when there are pending requests.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

lcicala commented 4 years ago

I've pushed a version that fixes this issue.