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

IEC104 Client Hangs On Connection.Close() #31

Closed
iman7107 opened this issue Mar 26, 2024 · 9 comments
Closed

IEC104 Client Hangs On Connection.Close() #31

iman7107 opened this issue Mar 26, 2024 · 9 comments
Labels

Comments

@iman7107
Copy link

Hi.
When I call Connection.Close(); method for IEC104 Client, the system runs into hang. in the server side shows that client disconnected. but my form freezes in client side. I have Traced the Close() method and freezing occurs after execute line workerThread.Join();.
How can I solve the problem.
Thanks.

@ibekaddour
Copy link

ibekaddour commented Mar 27, 2024

Hello,
Could you please provide more details about the issue, also how you are running the client and server.
Thank you.

@iman7107
Copy link
Author

Hi again.
I use Axon Test software as RTU(Server) IEC104 simulator.
This is my Connect method body:

 con = new Connection(IPTextBox.Text, Convert.ToInt32(PortTextBox.Text));
            con.DebugOutput = true;
            con.SetASDUReceivedHandler(asduReceivedHandler, null);
            con.SetConnectionHandler(ConnectionHandler, null);
            try
            {
                con.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

and I do something in ASDURecieveHandler.
everything is OK. But when I click on Disconnect Button my form run ino Hang.
This is my Disconnect method body:

     try
                {
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Close Connection Error : " + Environment.NewLine + ex.ToString(), "Close Connection Error");
                }


I changed my code and now my Disconnect method body is like this:

     try
                {
                    con.Cancel();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Close Connection Error : " + Environment.NewLine + ex.ToString(), "Close Connection Error");
                }

and now it is OK. Should I call con.Cancel() before con.Close() ? I saw Cancel method close the Socket and because of this, I called Cancel() method before Close(). Did I do correct way?

@juwiefer-btc
Copy link

Your call to Close() is not within the ASDURecieveHandler, right? I guess that would cause a deadlock. But calling Cancel() should not help in that case.
I don't think that you are supposed to call Cancel() before Close(), but that might still be a viable workaround.

I'm not sure if the way "running = false" is set in one thread to signal another thread to stop is correct without any synchronization.

@iman7107
Copy link
Author

Yes. That's right. I call Close() Method in button_Click event, not in ASDURecieverHandler.
So, how can I Close the connection in IEC104 Client? I sometimes need to disconnect my client.
What solution do you suggest?

@ibekaddour
Copy link

First you need to ensure that the connection object is not null before attempting to close it.
if (con != null)

What you can try then is calling the Disconnect() method, or maybe after calling Close() or Disconnect() you can call Dispose() this dispose of the connection object after closing it.

@iman7107
Copy link
Author

Disconnect()? I can't find Disconnect method.
I can call Dispose() as long as Close() method completes. But the problem is that it does not come out of Close() method.

@ibekaddour
Copy link

Since the issue is coming from workerThread.Join(); You can put a condition that will wait for the worker thread to finish within a specified timeout. If the thread does not finish within the timeout, it will close the connection.

@mzillgith
Copy link
Contributor

Could it be that the connection thread is stuck in the connection handler? When you are calling the UI in the connection handler then it could be a deadlock because the workerThread.Join is also running in the UI thread.

@iman7107
Copy link
Author

Hi.
I changed my code and moved my connect and disconnect method into another thread (Background worker) and my problem solved.
Thanks a lot.

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

No branches or pull requests

4 participants