Repository: netty/netty
Issue: Incorrect use of timerfd_settime system call in epoll-based backend.

### Expected behavior
The work of EpollEventLoop without the appearance of any exceptions in the log.
### Actual behavior
Occasionally, this exception is logged:
```[NettyIO0] WARN  i.netty.channel.epoll.EpollEventLoop - Unexpected exception in the selector loop.
io.netty.channel.ChannelException: timerfd_settime() failed: Illegal argument
    at io.netty.channel.epoll.Native.epollWait0(Native Method) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
    at io.netty.channel.epoll.Native.epollWait(Native.java:124) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
    at io.netty.channel.epoll.EpollEventLoop.epollWait(EpollEventLoop.java:231) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:248) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131) [netty-all-4.0.52.Final.jar:4.0.52.Final]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
```

Moreover, the application's performance slows down, because in the EpollEventLoop.run() the specified exception causes the thread to sleep for a second. Then, the exception is thrown again, and so on.

I consider this behavior a bug.
### Possible workarounds
*1. Downgrade Netty version to 4.0.50.*
The bug appeared in [this commit](https://github.com/netty/netty/commit/35e7e2aa20c9b77b2ea681a40fa9b361c56d5f39), so the latest Netty version (in branch 4.0), not affected by the problem, is 4.0.50.
*2. Switch Netty version to latest 4.1.x release.*
It seems that in the 4.1 branch the bug is fixed (see #7970).
### Netty version
4.0.52-4.0.56
### OS version
Any Linux version that supports the timerfd_settime system call.

Issue comments:
Thanks for reporting but 4.0.x is EOL for over an year. Please upgrade to 4.1.x

> Am 18.07.2018 um 16:59 schrieb Dmitry Shatov <notifications@github.com>:
> 
> Expected behavior
> 
> The work of EpollEventLoop without the appearance of any exceptions in the log.
> 
> Actual behavior
> 
> Occasionally, this exception is logged:
> 
> io.netty.channel.ChannelException: timerfd_settime() failed: Illegal argument
>     at io.netty.channel.epoll.Native.epollWait0(Native Method) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
>     at io.netty.channel.epoll.Native.epollWait(Native.java:124) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
>     at io.netty.channel.epoll.EpollEventLoop.epollWait(EpollEventLoop.java:231) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
>     at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:248) ~[netty-all-4.0.52.Final.jar:4.0.52.Final]
>     at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131) [netty-all-4.0.52.Final.jar:4.0.52.Final]
>     at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
> Moreover, the application's performance slows down, because in the EpollEventLoop.run() the specified exception causes the thread to sleep for a second. Then, the exception is thrown again, and so on.
> 
> I consider this behavior a bug.
> 
> Possible workarounds
> 
> 1. Downgrade Netty version to 4.0.50.
> The bug appeared in this commit, so the latest Netty version (in branch 4.0), not affected by the problem, is 4.0.50.
> 2. Switch Netty version to latest 4.1.x release.
> It seems that in the 4.1 branch the bug is fixed (see #7970).
> 
> Netty version
> 
> 4.0.52-4.0.56
> 
> OS version
> 
> Any Linux version that supports the timerfd_settime system call.
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub, or mute the thread.


Pull request review comments:

Patch that resolved the issue:
diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java
index 4ab1e6d6387..53938f72bbf 100644
--- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java
+++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java
@@ -229,7 +229,7 @@ private int epollWait(boolean oldWakeup) throws IOException {
         long totalDelay = delayNanos(System.nanoTime());
         int delaySeconds = (int) min(totalDelay / 1000000000L, Integer.MAX_VALUE);
         return Native.epollWait(epollFd, events, timerFd, delaySeconds,
-                (int) min(totalDelay - delaySeconds * 1000000000L, Integer.MAX_VALUE));
+                (int) min(totalDelay - delaySeconds * 1000000000L, 999999999L));
     }
 
     private int epollWaitNow() throws IOException {


Test patch: