threads/81534: [PATCH] libc_r close() will fail on any fd type that doesn't support fstat()

Ed Maste emaste at
Thu May 26 15:20:02 PDT 2005

>Number:         81534
>Category:       threads
>Synopsis:       [PATCH] libc_r close() will fail on any fd type that doesn't support fstat()
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-threads
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 26 22:20:00 GMT 2005
>Originator:     Ed Maste
>Release:        5.3-RELEASE
Sandvine Incorporated
FreeBSD 5.3-RELEASE-p2 FreeBSD 5.3-RELEASE-p2 #2: Fri Dec  3 13:30:36 EST 2004     root at  i386

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.

The libc_r close (in uthread_close.c) calls fstat() on the file
descriptor.  On 4.x this succeeds, while on 5.x the fstat() on the
kqueue() fd returns -1 with errno=0.  The close() in libc_r then
returns this error without doing the actual close syscall.

I built the test application shown below on a 4.7 and 5.3 machine
and fstat returns 0 on 4.7, -1 on 5.3.  If the test app is linked against libc_r then the close() fails too.

This has been discussed in a thread at

== kqueue.c ==

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/event.h>
#include <errno.h>

int main()
    struct stat sb;
    int kq=kqueue();
    printf("fstat returns %d (%d)\n", fstat(kq, &sb), errno);
    printf("close returns %d (%d)\n", close(kq), errno);

jmg@ has already committed a fix to have kqueue_stat not return a
failure (threads/75795).  

I posted a libc_r patch to deal with the issue at
As jmg points out, "Who knows what else doesn't implement _stat and
can't be closed.."

More information about the freebsd-threads mailing list