Skip to content

Commit

Permalink
8312065: Socket.connect does not timeout when profiling
Browse files Browse the repository at this point in the history
Reviewed-by: phh, vtewari, apangin
  • Loading branch information
Long Yang authored and Paul Hohensee committed Sep 15, 2023
1 parent fee0229 commit 1ce12c4
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 74 deletions.
48 changes: 24 additions & 24 deletions src/java.base/aix/native/libnet/aix_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,50 +388,50 @@ int NET_SocketClose(int fd) {
/************** Basic I/O operations here ***************/

/*
* Macro to perform a blocking IO operation. Restarts
* automatically if interrupted by signal (other than
* our wakeup signal)
* Macro to perform a blocking IO operation.
* If interrupted by signal (other than our wakeup signal), and if RETRY is true,
* then restarts automatically
*/
#define BLOCKING_IO_RETURN_INT(FD, FUNC) { \
int ret; \
threadEntry_t self; \
fdEntry_t *fdEntry = getFdEntry(FD); \
if (fdEntry == NULL) { \
errno = EBADF; \
return -1; \
} \
do { \
startOp(fdEntry, &self); \
ret = FUNC; \
endOp(fdEntry, &self); \
} while (ret == -1 && errno == EINTR); \
return ret; \
#define BLOCKING_IO_RETURN_INT(FD, FUNC, RETRY) { \
int ret; \
threadEntry_t self; \
fdEntry_t *fdEntry = getFdEntry(FD); \
if (fdEntry == NULL) { \
errno = EBADF; \
return -1; \
} \
do { \
startOp(fdEntry, &self); \
ret = FUNC; \
endOp(fdEntry, &self); \
} while ((RETRY) && ret == -1 && errno == EINTR); \
return ret; \
}

int NET_Read(int s, void* buf, size_t len) {
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0), JNI_TRUE );
}

int NET_NonBlockingRead(int s, void* buf, size_t len) {
BLOCKING_IO_RETURN_INT(s, recv(s, buf, len, MSG_NONBLOCK));
BLOCKING_IO_RETURN_INT(s, recv(s, buf, len, MSG_NONBLOCK), JNI_TRUE );
}

int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t *fromlen) {
BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) );
BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen), JNI_TRUE );
}

int NET_Send(int s, void *msg, int len, unsigned int flags) {
BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags), JNI_TRUE );
}

int NET_SendTo(int s, const void *msg, int len, unsigned int
flags, const struct sockaddr *to, int tolen) {
BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen), JNI_TRUE );
}

int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) {
BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen), JNI_TRUE );
}

int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
Expand Down Expand Up @@ -489,7 +489,7 @@ int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
}

int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout), JNI_FALSE );
}

/*
Expand Down
50 changes: 25 additions & 25 deletions src/java.base/linux/native/libnet/linux_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,58 +345,58 @@ int NET_SocketClose(int fd) {
/************** Basic I/O operations here ***************/

/*
* Macro to perform a blocking IO operation. Restarts
* automatically if interrupted by signal (other than
* our wakeup signal)
* Macro to perform a blocking IO operation.
* If interrupted by signal (other than our wakeup signal), and if RETRY is true,
* then restarts automatically
*/
#define BLOCKING_IO_RETURN_INT(FD, FUNC) { \
int ret; \
threadEntry_t self; \
fdEntry_t *fdEntry = getFdEntry(FD); \
if (fdEntry == NULL) { \
errno = EBADF; \
return -1; \
} \
do { \
startOp(fdEntry, &self); \
ret = FUNC; \
endOp(fdEntry, &self); \
} while (ret == -1 && errno == EINTR); \
return ret; \
#define BLOCKING_IO_RETURN_INT(FD, FUNC, RETRY) { \
int ret; \
threadEntry_t self; \
fdEntry_t *fdEntry = getFdEntry(FD); \
if (fdEntry == NULL) { \
errno = EBADF; \
return -1; \
} \
do { \
startOp(fdEntry, &self); \
ret = FUNC; \
endOp(fdEntry, &self); \
} while ((RETRY) && ret == -1 && errno == EINTR); \
return ret; \
}

int NET_Read(int s, void* buf, size_t len) {
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0), JNI_TRUE );
}

int NET_NonBlockingRead(int s, void* buf, size_t len) {
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT) );
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT), JNI_TRUE );
}

int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t *fromlen) {
BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) );
BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen), JNI_TRUE );
}

int NET_Send(int s, void *msg, int len, unsigned int flags) {
BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags), JNI_TRUE );
}

int NET_SendTo(int s, const void *msg, int len, unsigned int
flags, const struct sockaddr *to, int tolen) {
BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen), JNI_TRUE );
}

int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) {
BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen), JNI_TRUE );
}

int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen), JNI_TRUE );
}

int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout), JNI_FALSE );
}

/*
Expand Down
50 changes: 25 additions & 25 deletions src/java.base/macosx/native/libnet/bsd_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,58 +349,58 @@ int NET_SocketClose(int fd) {
/************** Basic I/O operations here ***************/

/*
* Macro to perform a blocking IO operation. Restarts
* automatically if interrupted by signal (other than
* our wakeup signal)
* Macro to perform a blocking IO operation.
* If interrupted by signal (other than our wakeup signal), and if RETRY is true,
* then restarts automatically
*/
#define BLOCKING_IO_RETURN_INT(FD, FUNC) { \
int ret; \
threadEntry_t self; \
fdEntry_t *fdEntry = getFdEntry(FD); \
if (fdEntry == NULL) { \
errno = EBADF; \
return -1; \
} \
do { \
startOp(fdEntry, &self); \
ret = FUNC; \
endOp(fdEntry, &self); \
} while (ret == -1 && errno == EINTR); \
return ret; \
#define BLOCKING_IO_RETURN_INT(FD, FUNC, RETRY) { \
int ret; \
threadEntry_t self; \
fdEntry_t *fdEntry = getFdEntry(FD); \
if (fdEntry == NULL) { \
errno = EBADF; \
return -1; \
} \
do { \
startOp(fdEntry, &self); \
ret = FUNC; \
endOp(fdEntry, &self); \
} while ((RETRY) && ret == -1 && errno == EINTR); \
return ret; \
}

int NET_Read(int s, void* buf, size_t len) {
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0), JNI_TRUE );
}

int NET_NonBlockingRead(int s, void* buf, size_t len) {
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT));
BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT), JNI_TRUE);
}

int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t *fromlen) {
BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) );
BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen), JNI_TRUE );
}

int NET_Send(int s, void *msg, int len, unsigned int flags) {
BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags), JNI_TRUE );
}

int NET_SendTo(int s, const void *msg, int len, unsigned int
flags, const struct sockaddr *to, int tolen) {
BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen), JNI_TRUE );
}

int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) {
BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen), JNI_TRUE );
}

int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen), JNI_TRUE );
}

int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout), JNI_FALSE );
}

/*
Expand Down
88 changes: 88 additions & 0 deletions test/jdk/java/net/Socket/B8312065.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2023, Alibaba Group Holding Limited. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8312065
* @summary Socket.connect does not timeout as expected when profiling (i.e. keep receiving signal)
* @requires (os.family != "windows")
* @compile NativeThread.java
* @run main/othervm/native/timeout=120 -Djdk.net.usePlainSocketImpl B8312065
*/

import sun.misc.Signal;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;

public class B8312065 {
public static void main(String[] args) throws Exception {
System.loadLibrary("NativeThread");

// Setup SIGPIPE handler
Signal.handle(new Signal("PIPE"), System.out::println);

long osThreadId = NativeThread.getID();

int timeoutMillis = 2000;
int n = 10;
Thread t = new Thread(() -> {
// Send SIGPIPE to the thread every second
for (int i = 0; i < n; i++) {
if (NativeThread.signal(osThreadId, NativeThread.SIGPIPE) != 0) {
System.out.println("Test FAILED: failed to send signal");
System.exit(1);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Test FAILED: unexpected interrupt");
System.exit(1);
}
}
System.out.println("Test FAILED: Socket.connect blocked " + n + " seconds, " +
"expected around " + timeoutMillis / 1000 + " seconds");
System.exit(1);
});
t.setDaemon(true);
t.start();

long startTime = System.nanoTime();

try {
Socket socket = new Socket();
// There is no good way to mock SocketTimeoutException, just assume 192.168.255.255 is not in use
socket.connect(new InetSocketAddress("192.168.255.255", 8080), timeoutMillis);
} catch (SocketTimeoutException e) {
long duration = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
if (duration >= timeoutMillis) {
System.out.println("Test passed");
} else {
System.out.println("Test FAILED: duration " + duration + " ms, expected >= " + timeoutMillis + " ms");
System.exit(1);
}
}
}
}

1 comment on commit 1ce12c4

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.