I’m using a 3rd party library that maintains a TCP connection to a remote server and manages the communication protocol. Sometimes it loses its connection, so I have a thread that monitors the connection status and tries to reconnect. Usually it can reconnect.
The problem is sometimes calls to Connect() never return. I restart the process it will work, so I think there is something goofy happening in the library, either deadlock or some static variable gets put in a weird state. I decompiled the code, but it’s a little too “creative” to be understandable.
I need to write this in a way so that I never have to restart the process on account of this 3rd party library. The library itself has 2 callbacks of Action{T}. Is running the library inside its own app domain a good choice? This way if a connection goes down and will not restart, I can kill the appdomain and start a new one. What considerations should I make?
Hung threads are very problematic. They invariably hang on some kind of native call, one that Thread.Abort() cannot reach. Trying to unload the AppDomain will not work when the thread can’t be aborted.
Running the code in a separate process is then the better solution, you can always shoot the process with Process.Kill(). Talk to it with something like WCF.