-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix CursorResourceManager.close
#1440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb.internal.operation; | ||
|
||
import com.mongodb.MongoNamespace; | ||
import com.mongodb.ServerCursor; | ||
import com.mongodb.internal.binding.AsyncConnectionSource; | ||
import com.mongodb.internal.binding.ReferenceCounted; | ||
import com.mongodb.internal.connection.Connection; | ||
import com.mongodb.internal.mockito.MongoMockito; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.mockito.Mockito.when; | ||
|
||
final class CursorResourceManagerTest { | ||
@Test | ||
void doubleCloseExecutedConcurrentlyWithOperationBeingInProgressShouldNotFail() { | ||
CursorResourceManager<?, ?> cursorResourceManager = new CursorResourceManager<ReferenceCounted, ReferenceCounted>( | ||
new MongoNamespace("db", "coll"), | ||
MongoMockito.mock(AsyncConnectionSource.class, mock -> { | ||
when(mock.retain()).thenReturn(mock); | ||
when(mock.release()).thenReturn(1); | ||
}), | ||
null, | ||
MongoMockito.mock(ServerCursor.class)) { | ||
@Override | ||
void markAsPinned(final ReferenceCounted connectionToPin, final Connection.PinningMode pinningMode) { | ||
} | ||
|
||
@Override | ||
void doClose() { | ||
} | ||
}; | ||
cursorResourceManager.tryStartOperation(); | ||
try { | ||
assertDoesNotThrow(() -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the fix in |
||
cursorResourceManager.close(); | ||
cursorResourceManager.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good example of |
||
cursorResourceManager.setServerCursor(null); | ||
}); | ||
} finally { | ||
cursorResourceManager.endOperation(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with the original code:
If
AsyncCommandBatchCursor.close
is called concurrently more than once whileAsyncCommandBatchCursor.next
is running (not necessarily thenext
method itself, but the asynchronous logic of this method), then the following state transitions were possible:close
calls observesState.OPERATION_IN_PROGRESS
, and changes it toState.CLOSE_PENDING
. So far so good.close
observesState.CLOSE_PENDING
and changes it toState.CLOSED
, which breaks one of the invariant that are supposed to be maintained byCursorResourceManager
: while an operation is in progress,state.inProgress()
must betrue
, i.e.,state
must be eitherOPERATION_IN_PROGRESS
orCLOSE_PENDING
(impliesOPERATION_IN_PROGRESS
).