From nobody@FreeBSD.org  Fri Mar 19 21:29:41 2004
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 0211316A4CE
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 19 Mar 2004 21:29:41 -0800 (PST)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id D744943D2F
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 19 Mar 2004 21:29:40 -0800 (PST)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.10/8.12.10) with ESMTP id i2K5Te72099313
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 19 Mar 2004 21:29:40 -0800 (PST)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.10/8.12.10/Submit) id i2K5TeSr099312;
	Fri, 19 Mar 2004 21:29:40 -0800 (PST)
	(envelope-from nobody)
Message-Id: <200403200529.i2K5TeSr099312@www.freebsd.org>
Date: Fri, 19 Mar 2004 21:29:40 -0800 (PST)
From: Pavlin Radoslavov <pavlin@icir.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: NetworkInterface.getNetworkInterfaces() leaks file descriptors
X-Send-Pr-Version: www-2.3

>Number:         64492
>Category:       java
>Synopsis:       NetworkInterface.getNetworkInterfaces() leaks file descriptors
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    phantom
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Mar 19 21:30:22 PST 2004
>Closed-Date:    Wed Apr 20 21:34:23 GMT 2005
>Last-Modified:  Wed Apr 20 21:34:23 GMT 2005
>Originator:     Pavlin Radoslavov
>Release:        4.8-RELEASE
>Organization:
ICSI
>Environment:
FreeBSD xorp8.icir.org 4.8-RELEASE FreeBSD 4.8-RELEASE #0: Wed May 28 11:19:23 PDT 2003     root@xorp8.icir.org:/usr/src/sys/compile/XORP8-4.8  i386
     
>Description:
Every time NetworkInterface.getNetworkInterfaces() is called
on a system with IPv6 enabled it leaks a file descriptor (UDP socket).

This is on a system with lastest port collection with
jdk14 and with the lastest jdk14 patches
(bsd-jdk14-patches-6.tar.gz)

>How-To-Repeat:
Write a simple program that repeatedly calls
NetworkInterface.getNetworkInterfaces()
Run the program and then in parallel list the number of
open UDP socket. E.g.:

lsof | egrep java | egrep UDP

The output should be a growing list of opened UDP sockets
COMMAND   PID   USER   FD   TYPE     DEVICE   SIZE/OFF   NODE NAME
..
java    35251 pavlin    8u  IPv4 0xddf80a00        0t0    UDP *:*
java    35251 pavlin    9u  IPv4 0xddf80ac0        0t0    UDP *:*
java    35251 pavlin   10u  IPv4 0xddf80b80        0t0    UDP *:*
java    35251 pavlin   11u  IPv4 0xddf80c40        0t0    UDP *:*
java    35251 pavlin   12u  IPv4 0xddf80d00        0t0    UDP *:*
java    35251 pavlin   13u  IPv4 0xddf80dc0        0t0    UDP *:*
..

>Fix:
The problem can be traced down to the BSD-specific patch applied
by bsd-jdk14-patches-6.tar.gz
More specifically, in the BSD iplementation of
enumIPv4Interfaces() inside
/usr/ports/java/jdk14/work/j2se/src/solaris/native/java/net/NetworkInterface.c

Every time this function is called it leaks a file descriptor
if IPv6 is enabled: first an IPv4 SOCK_DGRAM socket is opened,
but then variable "sock" is reasigned to an IPv6 socket without
closing the IPv4 socket. The simple patch below fixes the problem:

--- work/j2se/src/solaris/native/java/net/NetworkInterface.c.leak       Wed Feb 25 14:19:21 2004
+++ work/j2se/src/solaris/native/java/net/NetworkInterface.c    Fri Mar 19 20:17:33 2004
@@ -605,6 +605,7 @@
     /*
      * Do this check early to avoid unnecessary work.
      */
+    close(sock);       /* XXX: close the IPv4 socket */
     if (ipv6_available()) {
        sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
        if (sock < 0) {

A better solution is to close "sock" soon after every time
it is opened because it is not used for anything more than
to verify that an IPv4/IPv6 socket can be opened.

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-java->phantom 
Responsible-Changed-By: glewis 
Responsible-Changed-When: Thu Apr 1 17:08:16 PST 2004 
Responsible-Changed-Why:  
Over to maintainer. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=64492 
State-Changed-From-To: open->closed 
State-Changed-By: glewis 
State-Changed-When: Wed Apr 20 21:33:37 GMT 2005 
State-Changed-Why:  
A change based on this patch was applied in patchset 7.  Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=64492 
>Unformatted:
