Bug 59608 - Attribute MethodImplOptions.AggressiveInlining causes method to always return NULL
Summary: Attribute MethodImplOptions.AggressiveInlining causes method to always return...
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: 5.2 (2017-04)
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Zoltan Varga
Depends on:
Reported: 2017-09-20 15:13 UTC by Mikhail Moussikhine
Modified: 2017-10-06 14:21 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Mikhail Moussikhine 2017-09-20 15:13:57 UTC
Using aggressive method inlining in some case optimizes the method out of existence.

I do not know if this issue manifests itself only in this particular case or there are other circumstances in which the same behavior can be observed, but here is the specific use case from the Akka.NET project:

private static Tcp.SocketCompleted ResolveMessage(SocketAsyncEventArgs e)
    switch (e.LastOperation)
        case SocketAsyncOperation.Receive:
        case SocketAsyncOperation.ReceiveFrom:
        case SocketAsyncOperation.ReceiveMessageFrom:
            return Tcp.SocketReceived.Instance;
        case SocketAsyncOperation.Send:
        case SocketAsyncOperation.SendTo:
        case SocketAsyncOperation.SendPackets:
            return Tcp.SocketSent.Instance;
        case SocketAsyncOperation.Accept:
            return Tcp.SocketAccepted.Instance;
        case SocketAsyncOperation.Connect:
            return Tcp.SocketConnected.Instance;
            throw new NotSupportedException($"Socket operation {e.LastOperation} is not supported");

Attribute [MethodImpl(MethodImplOptions.AggressiveInlining)] causes this method to ALWAYS return NULL under at least Mono I have encountered the problem on Ubuntu 17.04, but others seem to be having the same issue on Windows too.

Akka.NET bug report is here:

Removing the attribute or disabling inline optimization (mono -O=all,-inline ...) alleviates the problem.
Comment 1 Zoltan Varga 2017-09-23 00:00:10 UTC
Could you attach a complete testcase ? I couldn't create one from that code fragment.
Comment 2 Mikhail Moussikhine 2017-09-23 05:13:11 UTC
The original Akka bug report has one:


Would that suffice?
Comment 3 Zoltan Varga 2017-09-23 08:37:46 UTC
That code doesn't compile either, it has no using directives.
Comment 4 Mikhail Moussikhine 2017-09-23 16:50:08 UTC

Steps to reproduce:


1. 'Debug' Server
2. 'Run' Client

Client will send "Ping"
Server will receive "Ping" and respond "Pong"
Client will receive "Pong"

3. Stop Server


1. 'Run' Server
2. 'Run' Client

Client will send "Ping"
Server will crash

The important part of the exception trace is "Akka.IO.TcpExt.OnComplete".


Method OnComplete calls ResolveMessage, which in 'Run' mode always returns NULL.
Comment 5 Zoltan Varga 2017-09-23 21:46:50 UTC
I can reproduce, needed to compile the testcase with csc /optimize.

using System;
using System.Runtime.CompilerServices;
using System.Net;
using System.Net.Sockets;

class Args {
	public SocketAsyncOperation LastOperation { get; set; }

public class Tests
	private static int ResolveMessage(Args e)
    switch (e.LastOperation)
        case SocketAsyncOperation.Receive:
            return 0;
        case SocketAsyncOperation.Send:
            return 1;
        case SocketAsyncOperation.Accept:
            return 2;
        case SocketAsyncOperation.Connect:
            return 3;
            throw new NotSupportedException($"Socket operation {e.LastOperation} is not supported");

public static void Main () {
	var e = new Args () { LastOperation = SocketAsyncOperation.Connect };
	Console.WriteLine (ResolveMessage(e));
Comment 6 Zoltan Varga 2017-09-27 21:11:30 UTC
Comment 7 Marek Safar 2017-10-06 14:18:53 UTC
Closing the issue has been fixed