I have a program that deals with socket communication asynchronously. The exception I've been getting only occurs in release build (on build machines). The code that causes the problem is really simple routine to start listening for incoming socket connections:
public async Task Listen()
{
try
{
var endpoint = new IPEndPoint(IPAddress.Loopback, Port);
using (Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Socket.Bind(endpoint);
Socket.Listen(Limit);
while (!Shutdown)
{
var socket = await Socket.AcceptAsync().ConfigureAwait(false);
// some code handling connection
var result = await Socket.ReceiveAsync(state).ConfigureAwait(false);
Received(result, state);
}
}
}
catch (ObjectDisposedException) {}
catch (Exception ex) when (ex is SocketException || ex is ApplicationException || ex is ThreadAbortException)
{
OnError?.Invoke(this, ex);
Dispose();
}
}
AcceptAsync and ReceiveAsync are extension methods that use TPL pattern with Task.Factory.FromAsync. The exact exception is following:
Exception Type: System.InvalidProgramException
Exception Message: Common Language Runtime detected an invalid program.
This seem to occur in:
System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start
The exception is generated instantly when call is made to this method. Any ideas what might be wrong?
According to MSDN, this exception informs the user about invalid IL code. So something could be broken in the framework. I recommend you to try your luck at Connect.Microsoft. Also, if you're really interested and are looking for a quick fix you may want to inspect the IL code of the failing methods' chain.