Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix event loop shutdown timing fragility (#9616)
Motivation The current event loop shutdown logic is quite fragile and in the epoll/NIO cases relies on the default 1 second wait/select timeout that applies when there are no scheduled tasks. Without this default timeout the shutdown would hang indefinitely. The timeout only takes effect in this case because queued scheduled tasks are first cancelled in SingleThreadEventExecutor#confirmShutdown(), but I _think_ even this isn't robust, since the main task queue is subsequently serviced which could result in some new scheduled task being queued with much later deadline. It also means shutdowns are unnecessarily delayed by up to 1 second. Modifications - Add/extend unit tests to expose the issue - Adjust SingleThreadEventExecutor shutdown and confirmShutdown methods to explicitly add no-op tasks to the taskQueue so that the subsequent event loop iteration doesn't enter blocking wait (as looks like was originally intended) Results Faster and more robust shutdown of event loops, allows removal of the default wait timeout
- Loading branch information
1 parent
45be693
commit 170e4de
Showing
7 changed files
with
217 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
testsuite/src/main/java/io/netty/testsuite/transport/DefaultEventLoopTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2019 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.testsuite.transport; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.DefaultEventLoopGroup; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.ServerChannel; | ||
import io.netty.channel.local.LocalChannel; | ||
import io.netty.channel.local.LocalServerChannel; | ||
|
||
public class DefaultEventLoopTest extends AbstractSingleThreadEventLoopTest { | ||
|
||
@Override | ||
protected EventLoopGroup newEventLoopGroup() { | ||
return new DefaultEventLoopGroup(); | ||
} | ||
|
||
@Override | ||
protected Channel newChannel() { | ||
return new LocalChannel(); | ||
} | ||
|
||
@Override | ||
protected Class<? extends ServerChannel> serverChannelClass() { | ||
return LocalServerChannel.class; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
testsuite/src/main/java/io/netty/testsuite/transport/NioEventLoopTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2019 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.testsuite.transport; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.ServerChannel; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
|
||
public class NioEventLoopTest extends AbstractSingleThreadEventLoopTest { | ||
|
||
@Override | ||
protected EventLoopGroup newEventLoopGroup() { | ||
return new NioEventLoopGroup(); | ||
} | ||
|
||
@Override | ||
protected Channel newChannel() { | ||
return new NioSocketChannel(); | ||
} | ||
|
||
@Override | ||
protected Class<? extends ServerChannel> serverChannelClass() { | ||
return NioServerSocketChannel.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters