Elffuss BraidHub

kikocis/java-driver public

Fix memory and resource leak on closed Sessions (JAVA-342, JAVA-390).
Changeset48eee9fbeb84b3e575381a919d05a5b8134ca9fadd5d142dd2b596aef90d1a32
Authorolim7t <omichallat+github@gmail.com> · 2014-07-10 17:15 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-07-17 13:31 +02:00
Parents2cf768d3be54

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -11,6 +11,8 @@
1111 - [bug] Avoid classloader leak in Tomcat (JAVA-343)
1212 - [bug] Avoid deadlock in onAdd/onUp (JAVA-387)
1313 - [bug] Make metadata parsing more lenient (JAVA-377, JAVA-391)
14+- [bug] Ensure defunct connections are completely closed (JAVA-394)
15+- [bug] Fix memory and resource leak on closed Sessions (JAVA-342, JAVA-390)
1416
1517
1618 2.0.3:
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +4 −0
@@ -1197,6 +1197,10 @@
11971197 return session;
11981198 }
11991199
1200+ boolean removeSession(Session session) {
1201+ return sessions.remove(session);
1202+ }
1203+
12001204 void reportLatency(Host host, long latencyNanos) {
12011205 for (LatencyTracker tracker : trackers) {
12021206 tracker.update(host, latencyNanos);
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +24 −2
@@ -51,6 +51,7 @@
5151 private final Striped<Lock> poolCreationLocks = Striped.lazyWeakLock(5);
5252
5353 private volatile boolean isInit;
54+ private volatile boolean isClosing;
5455
5556 // Package protected, only Cluster should construct that.
5657 SessionManager(Cluster cluster) {
@@ -100,6 +101,9 @@
100101 if (future != null)
101102 return future;
102103
104+ isClosing = true;
105+ cluster.manager.removeSession(this);
106+
103107 List<CloseFuture> futures = new ArrayList<CloseFuture>(pools.size());
104108 for (HostConnectionPool pool : pools.values())
105109 futures.add(pool.closeAsync());
@@ -192,13 +196,22 @@
192196 public Boolean call() {
193197 while (true) {
194198 try {
195- HostConnectionPool previous = pools.put(host, new HostConnectionPool(host, distance, SessionManager.this));
199+ if (isClosing)
200+ return true;
201+
202+ HostConnectionPool newPool = new HostConnectionPool(host, distance, SessionManager.this);
203+ HostConnectionPool previous = pools.put(host, newPool);
196204 if (previous == null) {
197205 logger.debug("Added connection pool for {}", host);
198206 } else {
199207 logger.debug("Renewed connection pool for {}", host);
200208 previous.closeAsync();
201209 }
210+
211+ // If we raced with a session shutdown, ensure that the pool will be closed.
212+ if (isClosing)
213+ newPool.closeAsync();
214+
202215 return true;
203216 } catch (Exception e) {
204217 logger.error("Error creating pool to " + host, e);
@@ -215,6 +228,9 @@
215228 // ConcurrentMap only for that since it's the duplicate HostConnectionPool creation we
216229 // want to avoid
217230 private boolean replacePool(Host host, HostDistance distance, HostConnectionPool condition) throws ConnectionException, UnsupportedProtocolVersionException {
231+ if (isClosing)
232+ return false;
233+
218234 Lock l = poolCreationLocks.get(host);
219235 l.lock();
220236 try {
@@ -222,7 +238,13 @@
222238 if (previous != condition)
223239 return false;
224240
225- pools.put(host, new HostConnectionPool(host, distance, this));
241+ HostConnectionPool newPool = new HostConnectionPool(host, distance, this);
242+ pools.put(host, newPool);
243+
244+ // If we raced with a session shutdown, ensure that the pool will be closed.
245+ if (isClosing)
246+ newPool.closeAsync();
247+
226248 return true;
227249 } finally {
228250 l.unlock();