Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[xsp,fastcgi] Fix for bug #685069. Close socket and buffer disposal r…

…aces.

socket.Close and server.ReleaseBuffers are now protected with a lock and the socket,
send_buffer, receive_buffer fields are assigned null after connection is torn down.

Connection.Run method doesn't attempt to access null requests anymore.
  • Loading branch information...
commit 451ea5ea78f2e7c47fb12c1377bb95289e246b43 1 parent 7cc57c5
Marek Habersack grendello authored
82 src/Mono.WebServer.FastCgi/Connection.cs
View
@@ -51,7 +51,8 @@ public class Connection
private byte[] receive_buffer;
private byte[] send_buffer;
-
+
+ object connection_teardown_lock = new object ();
#endregion
@@ -60,10 +61,14 @@ public class Connection
public Connection (Socket socket, Server server)
{
+ if (socket == null)
+ throw new ArgumentNullException ("socket");
+ if (server == null)
+ throw new ArgumentNullException ("server");
+
this.socket = socket;
this.server = server;
- server.AllocateBuffers (out receive_buffer,
- out send_buffer);
+ server.AllocateBuffers (out receive_buffer, out send_buffer);
}
#endregion
@@ -77,7 +82,11 @@ public Connection (Socket socket, Server server)
}
public bool IsConnected {
- get {return socket.Connected;}
+ get {
+ if (socket == null)
+ return false;
+ return socket.Connected;
+ }
}
public Server Server {
@@ -94,7 +103,10 @@ public void Run ()
{
Logger.Write (LogLevel.Notice,
Strings.Connection_BeginningRun);
-
+ if (socket == null) {
+ Logger.Write (LogLevel.Notice, Strings.Connection_NoSocketInRun);
+ return;
+ }
do {
Record record;
@@ -207,6 +219,7 @@ public void Run ()
if (request == null) {
StopRun (Strings.Connection_RequestDoesNotExist,
record.RequestID);
+ break;
}
request.AddInputData (record);
@@ -218,6 +231,7 @@ public void Run ()
if (request == null) {
StopRun (Strings.Connection_RequestDoesNotExist,
record.RequestID);
+ break;
}
request.AddFileData (record);
@@ -226,7 +240,7 @@ public void Run ()
// Aborts a request when the server aborts.
case RecordType.AbortRequest:
- if (request != null)
+ if (request == null)
break;
request.Abort (
@@ -251,17 +265,26 @@ public void Run ()
while (!stop && (UnfinishedRequests || keep_alive));
if (requests.Count == 0) {
- try {
- socket.Close ();
- } catch (System.Net.Sockets.SocketException e) {
- // Ignore: "The descriptor is not a socket"
- // error from UnmanagedSocket.Close
- if (e.ErrorCode != 10038)
- throw; // Rethrow other errors
+ lock (connection_teardown_lock) {
+ try {
+ if (socket != null)
+ socket.Close ();
+ } catch (System.Net.Sockets.SocketException e) {
+ // Ignore: "The descriptor is not a socket"
+ // error from UnmanagedSocket.Close
+ if (e.ErrorCode != 10038)
+ throw; // Rethrow other errors
+ } finally {
+ socket = null;
+ }
+ if (!stop)
+ server.EndConnection (this);
+ if (receive_buffer != null && send_buffer != null) {
+ server.ReleaseBuffers (receive_buffer, send_buffer);
+ receive_buffer = null;
+ send_buffer = null;
+ }
}
- server.EndConnection (this);
- server.ReleaseBuffers (receive_buffer,
- send_buffer);
}
Logger.Write (LogLevel.Notice,
@@ -299,7 +322,7 @@ public void Run ()
if (IsConnected)
new Record (1, RecordType.EndRequest, requestID,
- body.GetData ()).Send (socket);
+ body.GetData ()).Send (socket);
int index = GetRequestIndex (requestID);
@@ -308,12 +331,25 @@ public void Run ()
requests.RemoveAt (index);
}
}
-
- if (requests.Count == 0 && (!keep_alive || stop)) {
- socket.Close ();
- server.EndConnection (this);
- server.ReleaseBuffers (receive_buffer,
- send_buffer);
+
+ lock (connection_teardown_lock) {
+ if (requests.Count == 0 && (!keep_alive || stop)) {
+ if (socket != null) {
+ try {
+ socket.Close ();
+ } finally {
+ socket = null;
+ }
+ }
+
+ if (!stop)
+ server.EndConnection (this);
+ if (receive_buffer != null && send_buffer != null) {
+ server.ReleaseBuffers (receive_buffer, send_buffer);
+ receive_buffer = null;
+ send_buffer = null;
+ }
+ }
}
}
1  src/Mono.WebServer.FastCgi/Strings.cs
View
@@ -22,6 +22,7 @@ internal static class Strings {
public static string Connection_AbortRecordReceived = "FastCGI Abort Request";
public static string Connection_UnknownRecordType = "Unknown type, {0}, encountered.";
public static string Connection_Terminating = "Terminating connection.";
+ public static string Connection_NoSocketInRun = "Run called, but no socket found.";
public static string NameValuePair_ParameterRead = "Read parameter. ({0} = {1})";
public static string NameValuePair_DuplicateParameter = "Duplicate name, {0}, encountered. Overwriting existing value.";
public static string NameValuePair_DictionaryContainsNonString = "Dictionary must only contain string values.";
Please sign in to comment.
Something went wrong with that request. Please try again.