bin/58687: gethostbyname leaks kqueue file descriptors with pthreads and static linking

Jonathan Lennox lennox at cs.columbia.edu
Wed Oct 29 08:40:24 PST 2003


>Number:         58687
>Category:       bin
>Synopsis:       gethostbyname leaks kqueue file descriptors with pthreads and static linking
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 29 08:40:20 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Jonathan Lennox
>Release:        FreeBSD 5.1-RELEASE-p10 i386
>Organization:
Columbia University
>Environment:
System: FreeBSD cnr.cs.columbia.edu 5.1-RELEASE-p10 FreeBSD 5.1-RELEASE-p10 #8: Sun Oct 5 23:47:09 EDT 2003 lennox at cnr.cs.columbia.edu:/usr/obj/usr/src/sys/CNR i386

The system has been patched by adding
src/lib/libc_r/uthread/uthread_kqueue.c, aligning it with -CURRENT.  (This
is mp's patch of 2003-07-25.)

>Description:

When linking with a static libc_r and libc, gethostbyname() leaks kqueue
file descriptors.  (I actually first observed this with libc_r_p.a and
libc_p.a, i.e. compiling and linking with -pg, but the same thing happens
when linking with -static.)

This is because &_kqueue isn't included in the 'references' array in
uthread_init.c.  There's no outstanding unresolved link to _kqueue when
libc_r is linked in, so the linker only picks up the version from libc.

See also closed PRs kern/55007 and bin/55879, which are this same problem
with dynamic libraries, now fixed in -CURRENT and -STABLE.

>How-To-Repeat:

Compile the following program with static libc and libc_r, either with
-static or -pg:

----------
#include <stdio.h>
#include <pthread.h>
#include <netdb.h>

void* the_thread(void* dummy)
{
  int i;
  for (i = 0; i < 50; i++) {
    gethostbyname("www.freebsd.org");
  } 

  return NULL;
}

int main(void)
{
  pthread_t dummy;
  pthread_create(&dummy, NULL, &the_thread, NULL);

  getchar();

  return 0;
}
----------

Before the program exits, use lsof to observe its open file descriptors.
Notice that it has 50 KQUEUE descriptors open.

In -CURRENT, this problem does not occur if you use dynamic libc and libc_r,
though it does in 5.1-RELEASE.  


>Fix:

Apply the following patch and rebuild:

--- lib/libc_r/uthread/uthread_init.c.orig	Wed Oct 29 11:00:53 2003
+++ lib/libc_r/uthread/uthread_init.c	Wed Oct 29 11:01:21 2003
@@ -99,6 +99,7 @@
 	&_getsockopt,
 	&_ioctl,
 	&_kevent,
+	&_kqueue,
 	&_listen,
 	&_nanosleep,
 	&_open,


As a workaround:

Add the command-line option -u kqueue (-Wl,-u,kqueue from gcc) when
linking.
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list