Skip to content

Commit 37fa377

Browse files
committed
Disable the Netty recycler in the client
The Netty recycler is nothing but trouble, so let us disable this by default in the client too. Relates #24793
1 parent 9046fca commit 37fa377

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

client/transport/src/main/java/org/elasticsearch/transport/client/PreBuiltTransportClient.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,29 @@ public class PreBuiltTransportClient extends TransportClient {
5757
}
5858

5959
/**
60-
* Netty wants to do some unsafe things like use unsafe and replace a private field. This method disables these things by default, but
61-
* can be overridden by setting the corresponding system properties.
60+
* Netty wants to do some unwelcome things like use unsafe and replace a private field, or use a poorly considered buffer recycler. This
61+
* method disables these things by default, but can be overridden by setting the corresponding system properties.
6262
*/
63-
@SuppressForbidden(reason = "set system properties to configure Netty")
6463
private static void initializeNetty() {
65-
final String noUnsafeKey = "io.netty.noUnsafe";
66-
final String noUnsafe = System.getProperty(noUnsafeKey);
67-
if (noUnsafe == null) {
68-
// disable Netty from using unsafe
69-
// while permissions are needed to set this, if a security exception is thrown the permission needed can either be granted or
70-
// the system property can be set directly before starting the JVM; therefore, we do not catch a security exception here
71-
System.setProperty(noUnsafeKey, Boolean.toString(true));
72-
}
64+
/*
65+
* We disable three pieces of Netty functionality here:
66+
* - we disable Netty from being unsafe
67+
* - we disable Netty from replacing the selector key set
68+
* - we disable Netty from using the recycler
69+
*
70+
* While permissions are needed to read and set these, the permissions needed here are innocuous and thus should simply be granted
71+
* rather than us handling a security exception here.
72+
*/
73+
setSystemPropertyIfUnset("io.netty.noUnsafe", Boolean.toString(true));
74+
setSystemPropertyIfUnset("io.netty.noKeySetOptimization", Boolean.toString(true));
75+
setSystemPropertyIfUnset("io.netty.recycler.maxCapacityPerThread", Integer.toString(0));
76+
}
7377

74-
final String noKeySetOptimizationKey = "io.netty.noKeySetOptimization";
75-
final String noKeySetOptimization = System.getProperty(noKeySetOptimizationKey);
76-
if (noKeySetOptimization == null) {
77-
// disable Netty from replacing the selector key set
78-
// while permissions are needed to set this, if a security exception is thrown the permission needed can either be granted or
79-
// the system property can be set directly before starting the JVM; therefore, we do not catch a security exception here
80-
System.setProperty(noKeySetOptimizationKey, Boolean.toString(true));
78+
@SuppressForbidden(reason = "set system properties to configure Netty")
79+
private static void setSystemPropertyIfUnset(final String key, final String value) {
80+
final String currentValue = System.getProperty(key);
81+
if (currentValue == null) {
82+
System.setProperty(key, value);
8183
}
8284
}
8385

0 commit comments

Comments
 (0)