# HG changeset patch # User vtewari # Date 1510700337 0 # Node ID 88d3306d6ee8e2e9b8429f2fd630ac9e33d11629 # Parent ce91f79634952b9405e174d26b94480a2fb0c24f 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set Reviewed-by: chegar, dsamersoff, msheppar, clanger diff -r ce91f7963495 -r 88d3306d6ee8 src/solaris/native/java/net/SocketInputStream.c --- a/src/solaris/native/java/net/SocketInputStream.c Thu Jul 06 13:34:55 2017 -0700 +++ b/src/solaris/native/java/net/SocketInputStream.c Tue Nov 14 22:58:57 2017 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -52,6 +52,42 @@ IO_fd_fdID = NET_GetFileDescriptorID(env); } +#if !defined(__solaris__) +static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) { + int result = 0; + long prevtime = NET_GetCurrentTime(), newtime; + while (timeout > 0) { + result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime); + if (result <= 0) { + if (result == 0) { + JNU_ThrowByName(env, "java/net/SocketTimeoutException", "Read timed out"); + } else if (result == -1) { + if (errno == EBADF) { + JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); + } else if (errno == ENOMEM) { + JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); + } else { + JNU_ThrowByNameWithMessageAndLastError + (env, "java/net/SocketException", "select/poll failed"); + } + } + return -1; + } + result = NET_NonBlockingRead(fd, bufP, len); + if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) { + newtime = NET_GetCurrentTime(); + timeout -= newtime - prevtime; + if (timeout > 0) { + prevtime = newtime; + } + } else { + break; + } + } + return result; +} +#endif + /* * Class: java_net_SocketInputStream * Method: socketRead0 @@ -99,6 +135,7 @@ bufP = BUF; } +#if defined(__solaris__) if (timeout) { nread = NET_Timeout(fd, timeout); if (nread <= 0) { @@ -126,7 +163,19 @@ } nread = NET_Read(fd, bufP, len); - +#else + if (timeout) { + nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout); + if ((*env)->ExceptionCheck(env)) { + if (bufP != BUF) { + free(bufP); + } + return nread; + } + } else { + nread = NET_Read(fd, bufP, len); + } +#endif if (nread <= 0) { if (nread < 0) { diff -r ce91f7963495 -r 88d3306d6ee8 src/solaris/native/java/net/bsd_close.c --- a/src/solaris/native/java/net/bsd_close.c Thu Jul 06 13:34:55 2017 -0700 +++ b/src/solaris/native/java/net/bsd_close.c Tue Nov 14 22:58:57 2017 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -292,6 +292,10 @@ BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); } +int NET_NonBlockingRead(int s, void* buf, size_t len) { + BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT)); +} + int NET_ReadV(int s, const struct iovec * vector, int count) { BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); } @@ -344,8 +348,8 @@ * Auto restarts with adjusted timeout if interrupted by * signal other than our wakeup signal. */ -int NET_Timeout(int s, long timeout) { - long prevtime = 0, newtime; +int NET_Timeout0(int s, long timeout, long currentTime) { + long prevtime = currentTime, newtime; struct timeval t, *tp = &t; fd_set fds; fd_set* fdsp = NULL; @@ -366,9 +370,6 @@ */ if (timeout > 0) { /* Timed */ - struct timeval now; - gettimeofday(&now, NULL); - prevtime = now.tv_sec * 1000 + now.tv_usec / 1000; t.tv_sec = timeout / 1000; t.tv_usec = (timeout % 1000) * 1000; } else if (timeout < 0) { diff -r ce91f7963495 -r 88d3306d6ee8 src/solaris/native/java/net/linux_close.c --- a/src/solaris/native/java/net/linux_close.c Thu Jul 06 13:34:55 2017 -0700 +++ b/src/solaris/native/java/net/linux_close.c Tue Nov 14 22:58:57 2017 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -326,6 +326,10 @@ BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); } +int NET_NonBlockingRead(int s, void* buf, size_t len) { + BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT) ); +} + int NET_ReadV(int s, const struct iovec * vector, int count) { BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); } @@ -377,8 +381,8 @@ * Auto restarts with adjusted timeout if interrupted by * signal other than our wakeup signal. */ -int NET_Timeout(int s, long timeout) { - long prevtime = 0, newtime; +int NET_Timeout0(int s, long timeout, long currentTime) { + long prevtime = currentTime, newtime; struct timeval t; fdEntry_t *fdEntry = getFdEntry(s); @@ -390,14 +394,6 @@ return -1; } - /* - * Pick up current time as may need to adjust timeout - */ - if (timeout > 0) { - gettimeofday(&t, NULL); - prevtime = t.tv_sec * 1000 + t.tv_usec / 1000; - } - for(;;) { struct pollfd pfd; int rv; diff -r ce91f7963495 -r 88d3306d6ee8 src/solaris/native/java/net/net_util_md.c --- a/src/solaris/native/java/net/net_util_md.c Thu Jul 06 13:34:55 2017 -0700 +++ b/src/solaris/native/java/net/net_util_md.c Tue Nov 14 22:58:57 2017 +0000 @@ -33,6 +33,7 @@ #include #include #include +#include #ifndef _ALLBSD_SOURCE #include @@ -1723,3 +1724,20 @@ return timeout; } + +#if !defined(__solaris__) +long NET_GetCurrentTime() { + struct timeval time; + gettimeofday(&time, NULL); + return (time.tv_sec * 1000 + time.tv_usec / 1000); +} + +int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime) { + return NET_Timeout0(s, timeout, currentTime); +} + +int NET_Timeout(int s, long timeout) { + long currentTime = (timeout > 0) ? NET_GetCurrentTime() : 0; + return NET_Timeout0(s, timeout, currentTime); +} +#endif diff -r ce91f7963495 -r 88d3306d6ee8 src/solaris/native/java/net/net_util_md.h --- a/src/solaris/native/java/net/net_util_md.h Thu Jul 06 13:34:55 2017 -0700 +++ b/src/solaris/native/java/net/net_util_md.h Tue Nov 14 22:58:57 2017 +0000 @@ -46,9 +46,13 @@ close subroutine does not return until the select call returns. ... */ -#if defined(__linux__) || defined(MACOSX) || defined (_AIX) +#if !defined(__solaris__) extern int NET_Timeout(int s, long timeout); +extern int NET_Timeout0(int s, long timeout, long currentTime); extern int NET_Read(int s, void* buf, size_t len); +extern int NET_NonBlockingRead(int s, void* buf, size_t len); +extern int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime); +extern long NET_GetCurrentTime(); extern int NET_RecvFrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); extern int NET_ReadV(int s, const struct iovec * vector, int count);