libc_r kqueue fd leak

Ed Maste emaste at phaedrus.sandvine.ca
Tue May 24 10:36:49 PDT 2005


On Tue, May 24, 2005 at 12:59:07PM -0400, Ed Maste wrote:

> We discovered a kqueue leak when running one of our 4.x applications on
> FreeBSD 5.3 using the compat libc_r.  It turns out it's caused by libc_r's
> close() failing.

I've attached a patch which stops libc_r close() from bailing if fstat()
returns an error.  This fixes the kqueue leak.  This logic would also have
to make its way into the compat library via 4.x to fully resolve the issue.

--
Ed Maste, Sandvine Incorporated
-------------- next part --------------
--- uthread_close.c.orig	2005-05-24 13:22:14.000000000 -0400
+++ uthread_close.c	2005-05-24 13:21:05.000000000 -0400
@@ -63,8 +63,7 @@
 	 * Lock the file descriptor while the file is closed and get
 	 * the file descriptor status:
 	 */
-	else if (((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) &&
-	    ((ret = __sys_fstat(fd, &sb)) == 0)) {
+	else if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
 		/*
 		 * Check if the file should be left as blocking.
 		 *
@@ -85,7 +84,8 @@
 		 * using, which would then cause any reads to block
 		 * indefinitely.
 		 */
-		if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode))
+		if (__sys_fstat(fd, &sb) == 0 &&
+		    (S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode))
 		    && (_thread_fd_getflags(fd) & O_NONBLOCK) == 0) {
 			/* Get the current flags: */
 			flags = __sys_fcntl(fd, F_GETFL, NULL);


More information about the freebsd-stable mailing list