Skip to content

Commit 8442fd4

Browse files
authored
Merge pull request #442 from pshipton/epollwaitretry
epoll_wait() retry on EINVAL
2 parents 62fd7aa + 289ba8f commit 8442fd4

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • src/java.base/linux/native/libnio/ch

src/java.base/linux/native/libnio/ch/EPoll.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2026, 2026 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2632
#include <dlfcn.h>
2733
#include <unistd.h>
2834
#include <sys/types.h>
2935
#include <sys/epoll.h>
36+
#include <time.h>
3037

3138
#include "jni.h"
3239
#include "jni_util.h"
@@ -83,11 +90,41 @@ Java_sun_nio_ch_EPoll_wait(JNIEnv *env, jclass clazz, jint epfd,
8390
jlong address, jint numfds, jint timeout)
8491
{
8592
struct epoll_event *events = jlong_to_ptr(address);
93+
struct timespec starttime;
94+
if (-1 != timeout) {
95+
clock_gettime(CLOCK_MONOTONIC, &starttime);
96+
}
8697
int res = epoll_wait(epfd, events, numfds, timeout);
8798
if (res < 0) {
8899
if (errno == EINTR) {
89100
return IOS_INTERRUPTED;
90101
} else {
102+
if (EINVAL == errno) {
103+
jint newtimeout = -1;
104+
if (-1 != timeout) {
105+
time_t elapsedsec = 0;
106+
long elapsedmillis = 0;
107+
struct timespec endtime;
108+
clock_gettime(CLOCK_MONOTONIC, &endtime);
109+
elapsedsec = endtime.tv_sec - starttime.tv_sec - 1;
110+
elapsedmillis = (endtime.tv_nsec + 1000000000 - starttime.tv_nsec) / 1000000;
111+
newtimeout = timeout - (elapsedsec * 1000) - elapsedmillis;
112+
if (newtimeout < 0) {
113+
newtimeout = 0;
114+
}
115+
}
116+
res = epoll_wait(epfd, events, numfds, newtimeout);
117+
if (res < 0) {
118+
if (errno == EINTR) {
119+
return IOS_INTERRUPTED;
120+
} else {
121+
JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
122+
return IOS_THROWN;
123+
}
124+
} else {
125+
return res;
126+
}
127+
}
91128
JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
92129
return IOS_THROWN;
93130
}

0 commit comments

Comments
 (0)