From 3011c0e51f8472265d42429174c89367861ad51e Mon Sep 17 00:00:00 2001 From: chegar Date: Tue, 16 Apr 2013 13:26:30 +0100 Subject: [PATCH] 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows Reviewed-by: alanb, dsamersoff --- .../java/net/DualStackPlainSocketImpl.java | 3 +- .../native/java/net/SocketInputStream.c | 55 ++++++++++--------- test/java/net/Socket/asyncClose/Race.java | 6 +- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/windows/classes/java/net/DualStackPlainSocketImpl.java b/src/windows/classes/java/net/DualStackPlainSocketImpl.java index 4073b9d5d..54121b463 100644 --- a/src/windows/classes/java/net/DualStackPlainSocketImpl.java +++ b/src/windows/classes/java/net/DualStackPlainSocketImpl.java @@ -152,8 +152,9 @@ class DualStackPlainSocketImpl extends AbstractPlainSocketImpl if (!fd.valid()) return; - close0(fdAccess.get(fd)); + final int nativefd = fdAccess.get(fd); fdAccess.set(fd, -1); + close0(nativefd); } void socketShutdown(int howto) throws IOException { diff --git a/src/windows/native/java/net/SocketInputStream.c b/src/windows/native/java/net/SocketInputStream.c index e7bb043d1..223424ab4 100644 --- a/src/windows/native/java/net/SocketInputStream.c +++ b/src/windows/native/java/net/SocketInputStream.c @@ -134,32 +134,35 @@ Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this, (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP); } else { if (nread < 0) { - /* - * Recv failed. - */ - switch (WSAGetLastError()) { - case WSAEINTR: - JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", - "socket closed"); - break; - - case WSAECONNRESET: - case WSAESHUTDOWN: - /* - * Connection has been reset - Windows sometimes reports - * the reset as a shutdown error. - */ - JNU_ThrowByName(env, "sun/net/ConnectionResetException", - ""); - break; - - case WSAETIMEDOUT : - JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", - "Read timed out"); - break; - - default: - NET_ThrowCurrent(env, "recv failed"); + // Check if the socket has been closed since we last checked. + // This could be a reason for recv failing. + if ((*env)->GetIntField(env, fdObj, IO_fd_fdID) == -1) { + NET_ThrowSocketException(env, "Socket closed"); + } else { + switch (WSAGetLastError()) { + case WSAEINTR: + JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", + "socket closed"); + break; + + case WSAECONNRESET: + case WSAESHUTDOWN: + /* + * Connection has been reset - Windows sometimes reports + * the reset as a shutdown error. + */ + JNU_ThrowByName(env, "sun/net/ConnectionResetException", + ""); + break; + + case WSAETIMEDOUT : + JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", + "Read timed out"); + break; + + default: + NET_ThrowCurrent(env, "recv failed"); + } } } } diff --git a/test/java/net/Socket/asyncClose/Race.java b/test/java/net/Socket/asyncClose/Race.java index f8869394b..5a9585682 100644 --- a/test/java/net/Socket/asyncClose/Race.java +++ b/test/java/net/Socket/asyncClose/Race.java @@ -23,8 +23,8 @@ /* * @test - * @bug 8006395 - * @summary Race in async socket close on Linux + * @bug 8006395 8012244 + * @summary Tests racing code that reads and closes a Socket */ import java.io.InputStream; @@ -58,7 +58,7 @@ public class Race { Thread.sleep(50); } catch (Exception x) { if (!(x instanceof SocketException - && x.getMessage().equals("Socket closed"))) + && x.getMessage().equalsIgnoreCase("socket closed"))) x.printStackTrace(); // ok, expect Socket closed } -- GitLab