7

在 Mac OS 上调查这个 JDK 错误时,我遇到了我不明白的系统调用跟踪输出:

由于 Mac OS X 上的“java.net.SocketException Invalid argument”,Tomcat 启动失败

简短版本:在 Mac OS 上,JDKselect()使用poll(). 因此,如果分配了超过 1024 个文件描述符,我们推断select()调用NET_Timeout失败导致 SocketException 和“无效参数”消息。但是,当我跟踪系统调用时,我没有看到系统调用的证据select(),或者任何失败并设置 EINVAL 的调用,所以我认为这是一个潜在的原因。

我也没有看到我创建的减少测试用例的调用,现在我明白了这个问题:

import java.io.*;
import java.net.*;

public class SelectTest {
  public static void main(String[] args) throws Exception {
    for(int i = 0; i < 1024; i++) {
      new FileInputStream("/dev/null");
    }
    ServerSocket socket = new ServerSocket(8080);
    socket.accept();
  }
}

在带有 JDK 1.7u5 及更高版本的 Mac OS 上导致此异常:

Exception in thread "main" java.net.SocketException: Invalid argument
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
    at java.net.ServerSocket.implAccept(ServerSocket.java:522)
    at java.net.ServerSocket.accept(ServerSocket.java:490)
    at SelectTest.main(SelectTest.java:12)

为什么select()我在使用 运行测试时看不到任何(或任何其他失败的)调用的证据sudo dtruss -a java SelectTest

        PID/THRD  RELATIVE  ELAPSD    CPU SYSCALL(args)                  = return
45563/0x63a513:     85544       6      4 bind(0x412, 0x10DFC7738, 0x1C)          = 0 0
45563/0x63a513:     85605       6      3 listen(0x412, 0x32, 0x32)               = 0 0
45563/0x63a513:     85619       2      0 lseek(0x4, 0x37377AD, 0x0)              = 57898925 0
45563/0x63a513:     85622       4      2 read(0x4, "PK\003\004\n\0", 0x1E)               = 30 0
45563/0x63a513:     85622       1      0 lseek(0x4, 0x37377E0, 0x0)              = 57898976 0
45563/0x63a513:     85627       5      4 read(0x4, "\312\376\272\276\0", 0x3447)                 = 13383 0
45563/0x63a513:     86150      37     33 write(0x2, "Exception in thread \"main\" ble\001\0", 0x1B)              = 27 0
4

1 回答 1

0

我希望ServerSocket.accept准确地调用你所看到的。

与 Java 对应的 Berkeley 套接字调用acceptlisten()accept()

不幸的是,我无法在 linux 上重现异常。如果我在得到之前使用了所有的 fd new ServerSocket(8080)

Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnet.so: /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnet.so: cannot open shared object file: Too many open files
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1939)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1864)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1846)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:67)
    at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:47)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.AbstractPlainSocketImpl.<clinit>(AbstractPlainSocketImpl.java:80)
    at java.net.ServerSocket.setImpl(ServerSocket.java:289)
    at java.net.ServerSocket.<init>(ServerSocket.java:230)
    at java.net.ServerSocket.<init>(ServerSocket.java:128)
    at SelectTest.main(SelectTest.java:15)
于 2013-05-22T14:48:58.147 回答