Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Fix for endless receive loop #4

merged 1 commit into from

2 participants


When heavily loading the FastCGI server resulting in connections hanging up gracefully on the remote end, the Sockets.Socket behavior is to immediately return 0 from calls to Receive. This behavior was not handled, resulting in CPU usage spinning out of control and the FastCGI server becoming partly or completely unavailable, depending on the number of threads and threads affected by the problem.

This fix is a patch that solves this problem under a few assumptions as documented in the commit. A more fit exception is probably required.

@gonzalop gonzalop merged commit a641207 into mono:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 31, 2011
  1. @nickbruun

    Fix for data errors

    nickbruun authored
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 2 deletions.
  1. +21 −2 src/Mono.WebServer.FastCgi/StandardSocket.cs
23 src/Mono.WebServer.FastCgi/StandardSocket.cs
@@ -76,7 +76,26 @@ public override void Close ()
public override int Receive (byte [] buffer, int offset, int size, System.Net.Sockets.SocketFlags flags)
- return socket.Receive (buffer, offset, size, flags);
+ // According to the MSDN specification, a call to Sockets.Socket.Receive
+ // ( will return
+ // 0 immediately, if the remote end has read all incoming data and
+ // gracefully closed the connection.
+ //
+ // As all calls to this function are synchronous in the current FastCGI
+ // implementation, we can safely assume that a Receive of 0 bytes would
+ // only arise in this case.
+ // All other errors are expected to throw an exception.
+ int received = socket.Receive(buffer, offset, size, flags);
+ if (received == 0)
+ {
+ // Note: a better error message would probably be deemed fit.
+ socket.Close();
+ throw new Exception("Remote end hung up.");
+ }
+ return received;
public override int Send (byte [] data, int offset, int size, System.Net.Sockets.SocketFlags flags)
@@ -105,4 +124,4 @@ public override Socket EndAccept (IAsyncResult asyncResult)
Something went wrong with that request. Please try again.