Skip to content

Commit 143dce3

Browse files
committed
[JAVA-315]: several exception cases do not put back the dbport in pool, resulting in forever hanging pool to that server
[JAVA-316]: in DBTCPConnector.say(), possible race condition that affects getLastError() result
1 parent e96cebc commit 143dce3

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,9 @@ void _checkClosed(){
117117
}
118118

119119
WriteResult _checkWriteError( DB db , MyPort mp , DBPort port , WriteConcern concern )
120-
throws MongoException {
120+
throws MongoException, IOException {
121121
CommandResult e = null;
122-
try {
123-
e = port.runCommand( db , concern.getCommand() );
124-
} catch ( IOException ioe ){
125-
throw new MongoException.Network( ioe.getMessage() , ioe );
126-
}
127-
mp.done( port );
122+
e = port.runCommand( db , concern.getCommand() );
128123

129124
Object foo = e.get( "err" );
130125
if ( foo == null )
@@ -162,7 +157,6 @@ public WriteResult say( DB db , OutMessage m , WriteConcern concern , ServerAddr
162157
return _checkWriteError( db , mp , port , concern );
163158
}
164159
else {
165-
mp.done( port );
166160
return new WriteResult( db , port , concern );
167161
}
168162
}
@@ -186,6 +180,7 @@ public WriteResult say( DB db , OutMessage m , WriteConcern concern , ServerAddr
186180
throw re;
187181
}
188182
finally {
183+
mp.done( port );
189184
m.doneWithMessage();
190185
}
191186
}
@@ -210,26 +205,30 @@ public Response call( DB db , DBCollection coll , OutMessage m , ServerAddress h
210205
final DBPort port = mp.get( false , slaveOk, hostNeeded );
211206

212207
Response res = null;
208+
boolean retry = false;
213209
try {
214210
port.checkAuth( db );
215211
res = port.call( m , coll );
216-
mp.done( port );
217212
if ( res._responseTo != m.getId() )
218213
throw new MongoException( "ids don't match" );
219214
}
220215
catch ( IOException ioe ){
221-
boolean shouldRetry = _error( ioe, slaveOk ) && ! coll._name.equals( "$cmd" ) && retries > 0;
222216
mp.error( port , ioe );
223-
if ( shouldRetry ){
224-
return call( db , coll , m , hostNeeded , retries - 1 );
217+
retry = _error( ioe, slaveOk ) && ! coll._name.equals( "$cmd" ) && retries > 0;
218+
if ( !retry ){
219+
throw new MongoException.Network( "can't call something" , ioe );
225220
}
226-
throw new MongoException.Network( "can't call something" , ioe );
227221
}
228222
catch ( RuntimeException re ){
229223
mp.error( port , re );
230224
throw re;
225+
} finally {
226+
mp.done( port );
231227
}
232-
228+
229+
if (retry)
230+
return call( db , coll , m , hostNeeded , retries - 1 );
231+
233232
ServerError err = res.getError();
234233

235234
if ( err != null && err.isNotMasterError() ){
@@ -356,8 +355,6 @@ void done( DBPort p ){
356355
*/
357356
void error( DBPort p , Exception e ){
358357
p.close();
359-
p.getPool().done( p );
360-
361358
_requestPort = null;
362359
_logger.log( Level.SEVERE , "MyPort.error called" , e );
363360
}

0 commit comments

Comments
 (0)