[CDRIVER-3969] Destruction of cursor leads to failed bson assert of the cursor server_id Created: 29/Apr/21  Updated: 28/Oct/23  Resolved: 26/Jun/23

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: 1.16.1, 1.17.5
Fix Version/s: 1.24.2

Type: Bug Priority: Major - P3
Reporter: Johannes Schulte Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: rp-toSched, size-small
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10 with mongoc driver 1.75.5
Ubuntu 20.04.2 LTS with mongoc driver 1.16.1 (inside Windows Subsystem for Linux 2)


Attachments: Text File destroy_cursor_bug.c    
Quarter: FY23Q3, FY24Q2

 Description   

I encountered an issue where the mongoc_cursor_destroy function is not able to destroy a cursor. I attached a file with a small c program that demonstrates the issue. You need to replace the uri_string, db_name & collection_name so they are valid for your system. The issue only occurs when the collection has more than 100 elements.

The crucial part in the attached c program is the following:

mongoc_client_command_with_opts(client, db_name, command, NULL, NULL, &reply, &error);
cursor = mongoc_cursor_new_from_command_reply_with_opts(client, &reply, NULL);
mongoc_cursor_destroy(cursor);

 

The cursor returned from mongoc_cursor_new_from_command_reply_with_opts has a non zero cursor_id and a server_id which is zero.

This leads to the failure of BSON_ASSERT(server_id) during the destruction of the cursor inside mongoc_cluster_stream_for_server() in mongoc-cluster.c.

The log output is the following:

/build/mongo-c-driver-RRvvb_/mongo-c-driver-1.16.1/src/libmongoc/src/mongoc/mongoc-cluster.c:1913 mongoc_cluster_stream_for_server(): precondition failed: server_id
Aborted



 Comments   
Comment by Githook User [ 11/Jul/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3969 set serverId with mocked cursor reply (#1335)

serverId is expected in mongoc_cursor_new_from_command_reply_with_opts
when the reply includes a non-zero cursor.id
Branch: r1.24
https://github.com/mongodb/mongo-c-driver/commit/e167ae7d5f67b7cb2a8690890c7734df4463e491

Comment by Githook User [ 03/Jul/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3969 set serverId with mocked cursor reply (#1335)

serverId is expected in mongoc_cursor_new_from_command_reply_with_opts
when the reply includes a non-zero cursor.id
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/9e7bf882d1fb4e16f5701a152f5c664088ffcda3

Comment by Githook User [ 26/Jun/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3969 error when creating uncompleted cursor with no server ID (#1321)

  • fix type of BCON_INT32 value
  • require serverID for open cursor with `mongoc_cursor_new_from_command_reply_with_opts`
  • document expectations of server ID option
  • assert `found` after `mongoc_cursor_error`

If an error occurs, `found` is false. Assert on the error first to print the error.

Comment by Githook User [ 26/Jun/23 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-3969 error when creating uncompleted cursor with no server ID (#1321)

  • fix type of BCON_INT32 value
  • require serverID for open cursor with `mongoc_cursor_new_from_command_reply_with_opts`
  • document expectations of server ID option
  • assert `found` after `mongoc_cursor_error`

If an error occurs, `found` is false. Assert on the error first to print the error.

Comment by Kevin Albertson [ 29/Apr/21 ]

Hi johannes.schulte@inmation.com, thank you for the bug report and the reproducible program. I was able to reproduce this against a 4.4 MongoDB server, and agree this is a valid bug.

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