netcgi2_fcgi leaks fd

ygrek <[email protected]> Wed, 26 Aug 2009 22:44:41 +0300
Newsgroups gmane.comp.lang.ocaml.lib.net.devel
Message-ID <[email protected]>
Hello,

 It appears that Netcgi2_fcgi leaks socket fd when Unix.shutdown throws an exception.
Consider

[pid 13392] write(43, "\1\6\0\1\0\33\5\0Content-Type: text/html\r\n"..., 3368) = 3368
[pid 13392] write(43, "\1\7\0\1\0\0\0\0\1\6\0\1\0\0\0\0\1\3\0\1\0\10\0\0\0\0\0\0\0\0\0\0"..., 32) = -1 EPIPE (Broken pipe)
[pid 13392] --- SIGPIPE (Broken pipe) @ 0 (0) ---
[pid 13392] write(43, "\1\7\0\1\0\0\0\0\1\6\0\1\0\0\0\0\1\3\0\1\0\10\0\0\0\0\0\0\0\0\0\0\1"..., 1368) = -1 EPIPE (Broken pipe)
[pid 13392] --- SIGPIPE (Broken pipe) @ 0 (0) ---
[pid 13392] write(43, "\1\7\0\1\0\0\0\0\1\6\0\1\0\0\0\0\1\3\0\1\0\10\0\0\0\0\0\0\0\0\0\0\1"..., 1400) = -1 EPIPE (Broken pipe)
[pid 13392] --- SIGPIPE (Broken pipe) @ 0 (0) ---
[pid 13392] shutdown(43, 2 /* send and receive */) = -1 ENOTCONN (Transport endpoint is not connected)

And close() is not called.
Ignoring SIGPIPE doesn't help either.
As a result the number of open sockets grows and reaches OS limit.
Simple patch to ignore shutdown() exceptions is attached. It does fix this issue.

-- 
 ygrek

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

_______________________________________________
Ocamlnet-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ocamlnet-devel
ocamlnet.diff (text/x-diff, 1003 B)
diff -r -u ocamlnet-2.2.9/src/netcgi2/netcgi_fcgi.ml ocamlnet-2.2.9-fix/src/netcgi2/netcgi_fcgi.ml
--- ocamlnet-2.2.9/src/netcgi2/netcgi_fcgi.ml	2007-11-02 00:12:31.000000000 +0200
+++ ocamlnet-2.2.9-fix/src/netcgi2/netcgi_fcgi.ml	2009-08-26 12:58:16.000000000 +0300
@@ -739,11 +739,11 @@
 
   match fd_cmd' with
     | `Conn_close ->
-	Unix.shutdown fd Unix.SHUTDOWN_ALL;
+	(try Unix.shutdown fd Unix.SHUTDOWN_ALL with _ -> ());
 	Unix.close fd
     | `Conn_close_linger ->
 	Unix.setsockopt_optint fd Unix.SO_LINGER (Some 15);
-	Unix.shutdown fd Unix.SHUTDOWN_ALL;
+	(try Unix.shutdown fd Unix.SHUTDOWN_ALL with _ -> ());
 	Unix.close fd
     | `Conn_keep_alive ->
 	(* The server is supposed to take care of closing [fd].  (Tail
@@ -752,7 +752,7 @@
 	handle_connection fd ~external_server ~max_conns
 	  ~config output_type arg_store exn_handler f
     | `Conn_error e ->
-	Unix.shutdown fd Unix.SHUTDOWN_ALL;
+	(try Unix.shutdown fd Unix.SHUTDOWN_ALL with _ -> ());
 	Unix.close fd;
 	raise e
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkqVkKkACgkQY0a5RXCNWgzSaACgmqhKFttNJ7hsQIeNkM9+fg68
5OMAn2rcplguufLEhNPss+TKKeef4oSe
=KAxM
-----END PGP SIGNATURE-----