[CDRIVER-1969] Driver aborts after "mongoc_cluster_run_command_internal(): precondition failed: command_name" Created: 29/Dec/16  Updated: 17/Jan/17  Resolved: 17/Jan/17

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: None
Fix Version/s: 1.6.0

Type: Bug Priority: Major - P3
Reporter: Arseny Vakhrushev Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

To illustrate the case, it is sufficient to pass an empty BSON as a command to mongoc_client_command_simple():

src/mongoc/mongoc-cluster.c:183 mongoc_cluster_run_command_internal(): precondition failed: command_name
Aborted

When something is appended to the command, the issue is gone. See the example below:

#include <stdio.h>
#include <mongoc.h>
 
int main() {
	mongoc_client_t *client;
	bson_t command, reply;
	bson_error_t error;
	bool status;
 
	mongoc_init();
 
	client = mongoc_client_new("mongodb://127.0.0.1");
	BSON_ASSERT(client);
	bson_init(&command);
	
	// BSON_APPEND_BOOL(&command, "a", 1); // Issue is gone when uncommented
 
	status = mongoc_client_command_simple(client, "test", &command, 0, &reply, &error);
	if (!status) printf("Error: %s\n", error.message);
 
	bson_destroy(&command);
	bson_destroy(&reply);
	mongoc_client_destroy(client);
	mongoc_cleanup();
	return 0;
}



 Comments   
Comment by Githook User [ 16/Jan/17 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-1969 don't abort if command doc is empty
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/c4fb5bea9d675b179a94bbadf4342ec7d2dd544a

Comment by Hannes Magnusson [ 30/Dec/16 ]

Fair enough. I was just curious how this was discover to decide on how critical issue this is.

Comment by Arseny Vakhrushev [ 29/Dec/16 ]

By "every internal assertion" I don't mean the requirements of the API itself of course. In particular, there is no documented requirement for the command argument to contain certain fields except that it should be a valid bson_t. Hence, I don't believe I am breaking the contract.

Comment by Arseny Vakhrushev [ 29/Dec/16 ]

The actual use is out of question I assume. When you write a high-level binding and just expose the method to the user, trust me, there will be one who will run it with something that will cause a crash. And in this case, it's either you who controls the arguments (which I of course do for my part) or the authors of the underlying API. Obviously as the author of a binding, I do not want to be immersed into internals of the underlying API too much. Because otherwise, it would be pure hell to obey with every internal assertion.

Comment by Hannes Magnusson [ 29/Dec/16 ]

Just out of curiosity, Is there an actual usecase to run empty command?

This assertion is an internal check to make sure we ship something to the server that looks like a real command, I suppose the abort is a bit harsh and we should return an error instead. I don't think we should send the empty bson to the server.

Comment by Arseny Vakhrushev [ 29/Dec/16 ]

libmongoc 1.5.0

Generated at Wed Feb 07 21:13:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.