Resolving Kestrel issues for ASP.NET 5 on Mac OSx

Running ASP.NET 5 on a Mac is so refreshing! Is it? Really? Well, yes and no. Yes, if you like experimenting and fiddling with the file system, using experimental software and enjoy troubleshooting a lot. It is not a great experience if you expect things to work straight out of the box and you don't want to mess around with the setup and environment configuration.

This post is about a couple of this things that I stumbled across a few times while developing and testing ASP.NET on my Mac. Kestrel,in case you haven't used it before, is a development web server for ASP.NET 5 based on the open source libuv library.

Error: Address already in use

For this first one, I'm not sure if this is a bug specific to my machine to do the setup/environment/installed software, but Kestrel seems to throw a EADDRINUSE address already in use exception when issuing a k kestrel command more than one time sequentially.

When I launch Kestrel for the first time from the Terminal, it runs successfully binding to the right port. However, if I stop Kestrel using CTRL + Z in order to perform code changes and then build with kpm build, then if I try to launch the web server with k kestrel, then I get the error I mentioned above.

This could be a bug with Kestrel itself or an environmental problem on my machine. Whatever the case, the way to resolve this issue is to issue the following commands:

Get the port in use:

sudo lsof -iTCP -sTCP:LISTEN -P | grep :5004  

This should return a message similar to this:

mono-sgen 1199 christosmatskas   13u  IPv4 0x7f8e51e882a1db91      0t0  TCP *:5004 (LISTEN)  

The important part here is the second string in the sequence which denotes the port currently in use. To kill the connection and allow Kestrel to run again, issue the following command in the Terminal:

kill -9 3138  

Error: Too many files open

Another issue, which seems to be related to the Mono framework this time, is the Too many open files exception you get when trying to launch Kestrel again using the Terminal. When you issue the command k kestrel you are presented with this lovely error message:

Christoss-MBP:helloworld christosmatskas$ k kestrel  
System.IO.IOException: Too many open files  
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions)

Rest of stacktrace omitted for clarity...  

The juicy part is at the top of message, i.e Too many open files. To resolve this problem, you need to issue the following command in the Terminal:

export MONO_MANAGED_WATCHER=disabled  

I hope this helps you resolve your problems, should you experience the same issues.

  • Share this post on
comments powered by Disqus