Skip to content

Commit ccb7fb3

Browse files
committed
fix potential leaks caused by server shutting down connection early
1 parent e37e74d commit ccb7fb3

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/com/danga/MemCached/SockIOPool.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public class SockIOPool {
140140
private static MessageDigest MD5 = null;
141141

142142
// Constants
143+
private static final Integer ZERO = new Integer( 0 );
143144
public static final int NATIVE_HASH = 0; // native String.hashCode();
144145
public static final int OLD_COMPAT_HASH = 1; // original compatibility hashing algorithm (works with other clients)
145146
public static final int NEW_COMPAT_HASH = 2; // new CRC32 based compatibility hashing algorithm (works with other clients)
@@ -777,14 +778,8 @@ protected SockIO createSocket( String host ) {
777778

778779
if ( !socket.isConnected() ) {
779780
log.error( "++++ failed to get SockIO obj for: " + host + " -- new socket is not connected" );
780-
try {
781-
socket.trueClose();
782-
}
783-
catch ( Exception ex ) {
784-
log.error( "++++ failed to close SockIO obj for server: " + host );
785-
log.error( ex.getMessage(), ex );
786-
socket = null;
787-
}
781+
deadPool.put( socket, ZERO );
782+
socket = null;
788783
}
789784
}
790785
catch ( Exception ex ) {
@@ -903,7 +898,10 @@ public SockIO getSock( String key, Integer hashCode ) {
903898
}
904899
}
905900
else {
906-
sock = null;
901+
if ( sock != null ) {
902+
deadPool.put( sock, ZERO );
903+
sock = null;
904+
}
907905
}
908906

909907
return sock;
@@ -943,7 +941,10 @@ public SockIO getSock( String key, Integer hashCode ) {
943941
}
944942
}
945943
else {
946-
sock = null;
944+
if ( sock != null ) {
945+
deadPool.put( sock, ZERO );
946+
sock = null;
947+
}
947948
}
948949

949950
// if we do not want to failover, then bail here
@@ -1025,7 +1026,7 @@ public SockIO getConnection( String host ) {
10251026
}
10261027
else {
10271028
// add to deadpool for later reaping
1028-
deadPool.put( socket, new Integer(0) );
1029+
deadPool.put( socket, ZERO );
10291030

10301031
// remove from avail pool
10311032
i.remove();
@@ -1142,10 +1143,16 @@ public void checkIn( SockIO socket, boolean addToAvail ) {
11421143
log.debug( "++++ removing socket (" + socket.toString() + ") from busy pool for host: " + host );
11431144
removeSocketFromPool( busyPool, host, socket );
11441145

1145-
// add to avail pool
1146-
if ( addToAvail && socket.isConnected() ) {
1147-
log.debug( "++++ returning socket (" + socket.toString() + " to avail pool for host: " + host );
1148-
addSocketToPool( availPool, host, socket );
1146+
if ( socket.isConnected() ) {
1147+
// add to avail pool
1148+
if ( addToAvail ) {
1149+
log.debug( "++++ returning socket (" + socket.toString() + " to avail pool for host: " + host );
1150+
addSocketToPool( availPool, host, socket );
1151+
}
1152+
}
1153+
else {
1154+
deadPool.put( socket, ZERO );
1155+
socket = null;
11491156
}
11501157
}
11511158
}
@@ -1347,7 +1354,7 @@ protected void selfMaint() {
13471354
}
13481355
else {
13491356
// add to deadPool for later reaping
1350-
deadPool.put( socket, new Integer( 0 ) );
1357+
deadPool.put( socket, ZERO );
13511358
}
13521359

13531360
// remove from the availPool
@@ -1386,7 +1393,7 @@ protected void selfMaint() {
13861393
}
13871394
else {
13881395
// add to deadPool for later reaping
1389-
deadPool.put( socket, new Integer( 0 ) );
1396+
deadPool.put( socket, ZERO );
13901397
}
13911398

13921399
// remove from the busy pool
@@ -1410,8 +1417,9 @@ protected void selfMaint() {
14101417
catch ( Exception ex ) {
14111418
log.error( "++++ failed to close SockIO obj from deadPool" );
14121419
log.error( ex.getMessage(), ex );
1413-
socket = null;
14141420
}
1421+
1422+
socket = null;
14151423
}
14161424

14171425
log.debug( "+++ ending self maintenance." );

0 commit comments

Comments
 (0)