Search code examples
asteriskasteriskamiasternet

How do I prevent or catch "Unable to run: socket is null" from AsterNet?


Using AsterNet to connect to Asterisk, I've been seeing the following:

Unhandled Exception:

System.SystemException: Unable to run: socket is null.
at AsterNET.Manager.ManagerReader.Run() in
e:\Projects\Github\AsterNET\Asterisk.2013\Asterisk.NET\Manager\ManagerReader.cs:line 197
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback,
Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state,
Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

You can see the line that throws this exception in the AsterNet code at ManagerReader.cs, line 197. It looks like this happens when connect() (in ManagerConnection.cs) launches a new thread, but mrSocket is somehow null in ManagerReader.cs. But this exception gets thrown from that new thread, so I can't catch it and handle the error. It just crashes my application.

So I have two questions. First, how do I prevent this from happening? Am I doing something wrong with how I connect or reconnect? Second, is it possible to catch this somehow and try the connection again?


Solution

  • This was happening because I was trying to maintain the connection myself and I didn't have to. I had written a loop to check the connection every so often and reconnect if it dropped and send pings to keep it alive. You can see an example of what I was doing here:

    https://gist.github.com/squaregear/80821130d221727c3836dba3224bf93e

    But I didn't need to do that. The current versions of AsterNet do all that for you. And when I was also trying to do it manually, it was interfering with the steps the library was taking. So the bottom line is, just let the AsterNet library maintain your connection for you.

    You can see my discussion with the maintainers here:

    https://github.com/AsterNET/AsterNET/issues/107