Search code examples
macosmonoikvm

Console mono executables do not exit when completed


I have a .NET project that I've been compiling with Visual Studio and running successfully on Windows for years. I'd like to move to use Mac OS X to run this particular program, and so have been working with mono for just a little while do make that possible. The results have been excellent, with the program performing as expected under mono (2.10.9, BTW).

The only hitch is that the program does not exit after it is done. It is a single-threaded process, but when its work is completed (which it reports at the end of the Main(string[]) method), the process does not exit; ^C is required to return to the shell prompt. Adding an explicit System.Environment.Exit(exitCode); call at the end has no effect.

I found only one other tale of this sort of problem from some years ago, here, though that ML thread provided no potential solution.


Solution

  • Rolf (in his comment on the question) was right, the vm was deadlocking upon shutdown.

    I'm using IKVM in this application, and there are certain circumstances where it needs to start a thread at vm shutdown to clean up its JVM<->CLR interop machinery; this apparently does not sit well with mono (but always works in .NET).

    This was discovered on a thread on the IKVM dev list by IKVM's author, who filed this bug with the mono project.

    The workaround in my case was to explicitly call java.lang.System.exit(exitCode) instead of simply letting the program terminate naturally or using System.Environment.Exit(exitCode). This allows IKVM to shut down outside of the vm termination lifecycle, thereby avoiding the mono bug.