Skip to content

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

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void endOperation() {
void close() {
boolean doClose = withLock(lock, () -> {
State localState = state;
if (localState == State.OPERATION_IN_PROGRESS) {
if (localState.inProgress()) {
Copy link
Member Author

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 while AsyncCommandBatchCursor.next is running (not necessarily the next method itself, but the asynchronous logic of this method), then the following state transitions were possible:

  1. One of the close calls observes State.OPERATION_IN_PROGRESS, and changes it to State.CLOSE_PENDING. So far so good.
  2. The other close observes State.CLOSE_PENDING and changes it to State.CLOSED, which breaks one of the invariant that are supposed to be maintained by CursorResourceManager: while an operation is in progress, state.inProgress() must be true, i.e., state must be either OPERATION_IN_PROGRESS or CLOSE_PENDING (implies OPERATION_IN_PROGRESS).

state = State.CLOSE_PENDING;
} else if (localState != State.CLOSED) {
state = State.CLOSED;
Expand Down
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(() -> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix in CursorResourceManager, this assertion fails.

cursorResourceManager.close();
cursorResourceManager.close();
Copy link
Member Author

@stIncMale stIncMale Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good example of close being executed concurrently with an operation being in progress, without any parallelism involved. Examples like this emphasize how concurrency and parallelism are different.

cursorResourceManager.setServerCursor(null);
});
} finally {
cursorResourceManager.endOperation();
}
}
}