From 62446827df079c8fc57d5c5dea915bca476eeb61 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 15 Dec 2011 16:38:45 +0900 Subject: [PATCH] Fix #129: Memory leak when setOptions() fails while accepting a new connection --- .../io/netty/bootstrap/ClientBootstrap.java | 10 +++++++- .../bootstrap/ConnectionlessBootstrap.java | 23 ++++++++++++++++--- .../io/netty/bootstrap/ServerBootstrap.java | 6 ++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/netty/bootstrap/ClientBootstrap.java b/src/main/java/io/netty/bootstrap/ClientBootstrap.java index 1d90d9a1f9..91f1b05bd5 100644 --- a/src/main/java/io/netty/bootstrap/ClientBootstrap.java +++ b/src/main/java/io/netty/bootstrap/ClientBootstrap.java @@ -209,7 +209,15 @@ public class ClientBootstrap extends Bootstrap { // Set the options. Channel ch = getFactory().newChannel(pipeline); - ch.getConfig().setOptions(getOptions()); + boolean success = false; + try { + ch.getConfig().setOptions(getOptions()); + success = true; + } finally { + if (!success) { + ch.close(); + } + } // Bind. if (localAddress != null) { diff --git a/src/main/java/io/netty/bootstrap/ConnectionlessBootstrap.java b/src/main/java/io/netty/bootstrap/ConnectionlessBootstrap.java index cd790cf07d..cabd7081f8 100644 --- a/src/main/java/io/netty/bootstrap/ConnectionlessBootstrap.java +++ b/src/main/java/io/netty/bootstrap/ConnectionlessBootstrap.java @@ -183,8 +183,16 @@ public class ConnectionlessBootstrap extends Bootstrap { Channel ch = getFactory().newChannel(pipeline); // Apply options. - ch.getConfig().setPipelineFactory(getPipelineFactory()); - ch.getConfig().setOptions(getOptions()); + boolean success = false; + try { + ch.getConfig().setPipelineFactory(getPipelineFactory()); + ch.getConfig().setOptions(getOptions()); + success = true; + } finally { + if (!success) { + ch.close(); + } + } // Bind ChannelFuture future = ch.bind(localAddress); @@ -288,7 +296,16 @@ public class ConnectionlessBootstrap extends Bootstrap { // Set the options. Channel ch = getFactory().newChannel(pipeline); - ch.getConfig().setOptions(getOptions()); + boolean success = false; + try { + ch.getConfig().setPipelineFactory(getPipelineFactory()); + ch.getConfig().setOptions(getOptions()); + success = true; + } finally { + if (!success) { + ch.close(); + } + } // Bind. if (localAddress != null) { diff --git a/src/main/java/io/netty/bootstrap/ServerBootstrap.java b/src/main/java/io/netty/bootstrap/ServerBootstrap.java index 24705e9a50..c8b4c23d88 100644 --- a/src/main/java/io/netty/bootstrap/ServerBootstrap.java +++ b/src/main/java/io/netty/bootstrap/ServerBootstrap.java @@ -347,7 +347,11 @@ public class ServerBootstrap extends Bootstrap { ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception { // Apply child options. - e.getChildChannel().getConfig().setOptions(childOptions); + try { + e.getChildChannel().getConfig().setOptions(childOptions); + } catch (Throwable t) { + Channels.fireExceptionCaught(e.getChildChannel(), t); + } ctx.sendUpstream(e); }