Currently, both CommandBatchCursor and ChangeStreamBatchCursor call TimeoutContext.resetTimeoutIfPresent when are being closed. Additionally, CommandBatchCursor calls TimeoutContext.resetToDefaultMaxTime. In a situation when a cursor is used as part of an operation, this behavior resets the timeout of the whole operation, which is not correct.
I think it could be done this way. But there is an “if” statement before killServerCursor call which has to be taken care of.
private void killServerCursor(final MongoNamespace namespace, final ServerCursor localServerCursor, final Connection localConnection) { OperationContext operationContext = assertNotNull(getConnectionSource()).getOperationContext(); TimeoutContext timeoutContext = operationContext.getTimeoutContext(); timeoutContext.resetToDefaultMaxTime(); TimeoutContext contextForCloseOperation = timeoutContext.copyTimeoutContext(); contextForCloseOperation.resetTimeoutIfPresent(); OperationContext operationContextForCloseOperation = operationContext.withTimeoutContext(contextForCloseOperation); localConnection.command(namespace.getDatabaseName(), getKillCursorsCommand(namespace, localServerCursor), NoOpFieldNameValidator.INSTANCE, ReadPreference.primary(), new BsonDocumentCodec(), operationContextForCloseOperation); }