[CDRIVER-2836] Issue with threading and incorrect data Created: 25/Sep/18 Updated: 27/Oct/23 Resolved: 25/Sep/18 |
|
| Status: | Closed |
| Project: | C Driver |
| Component/s: | libbson, libmongoc |
| Affects Version/s: | 1.13.0 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Phillip Carruthers | Assignee: | Unassigned |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 10 |
||
| Description |
|
Our server is multi-threaded and from what I can glime from the documentation it states that both mongodb c driver and bson is not thread safe. However I would have thought that if using the client pool I could get a client object for the thread that needs it so that the client object is thread safe and then getting a collection object from the client using mongoc_client_get_collection() and then a cursor object from the collection object using mongoc_collection_find_with_opts(), it should all be thread safe. All seems fine when performing requests across a single thread however if I start up a two or more threads I eventually get an issue where I am not seeing the correct data from the collection for that thread, I have created an assert that verifies that the data retrieved should be for that collection on a thread. Do I have to put a mutex lock/unlock combination around every bson and mongoc call? If that is the case the performance is going to be really bad.
|
| Comments |
| Comment by A. Jesse Jiryu Davis [ 25/Sep/18 ] |
|
Oh, I see the confusion. A bson_t or bson_iter_t is not thread-safe, so a single bson_t can't be used from multiple threads concurrently, nor can a single bson_iter_t be used from multiple threads concurrently. As long as you're not sharing a bson_t, bson_iter_t, or any other bson or mongoc struct from multiple threads concurrently you're fine. |
| Comment by Phillip Carruthers [ 25/Sep/18 ] |
|
Hi The bson doc says it is not thread safe so using the calls to marshal data I am actually putting a mutex lock/unlock around the bson calls. However when I increase the number of active threads I hit an issue where Regards This email and any attachments are intended only for the person or entity If this email has been transmitted outside the ordinary course of its On 25/09/2018, 20:21, "A. Jesse Jiryu Davis (Jira)" <jira@mongodb.org> |
| Comment by A. Jesse Jiryu Davis [ 25/Sep/18 ] |
|
Hi, can you please tell me what information would make the mongoc_client_pool_t tutorial more useful to you? Here's an answer to this question specifically: > However I would have thought that if using the client pool I could get a client object for the thread that needs it so that the client object is thread safe and then getting a collection object from the client using mongoc_client_get_collection() and then a cursor object from the collection object using mongoc_collection_find_with_opts(), it should all be thread safe. I think you're saying the right thing: only one thread at a time can use mongoc_client_t or any other C Driver object. Only mongoc_client_pool_t can be accessed from multiple threads concurrently. So, each thread checks out a client from the pool, uses that client, collection, and cursor in a single thread, and checks the client back into the pool. Other threads can run concurrently using different clients popped from the same pool. The tutorial provides an example of this pattern. |
| Comment by Phillip Carruthers [ 25/Sep/18 ] |
|
Hi I would like a more informed response. Regards On 25/09/2018, 16:25, "A. Jesse Jiryu Davis (Jira)" <jira@mongodb.org> |
| Comment by A. Jesse Jiryu Davis [ 25/Sep/18 ] |
|
Hi! The answers are in the threading section of the tutorial:
|