[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
Ok so from what you are saying I should be able to use the client thread
pool in a multi-threaded environment and all the objects obtained from the
client object should be thread safe in that thread, which is what I would
expect.

The bson doc says it is not thread safe so using the calls to marshal data
in and out of mongo document has to be protected by mutexes I believe, is
that the case?

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
the data I have in the document is not for the collection on that thread.

Regards
Phillip Carruthers
dbSpaces Limited

This email and any attachments are intended only for the person or entity
for which it is addressed and may contain confidential material. If you
are not the addressee or have received this email in error, please inform
the sender immediately and delete it from your computer. In addition, if
you are not the addressee or have received this email in error, any
disclosure, copying, distribution or any action taken or omitted to be
taken in reliance
upon it is prohibited and may be unlawful.

If this email has been transmitted outside the ordinary course of its
business, the company for which the sender works accepts no liability for
any loss or damage suffered by any person arising from any use of or
reliance on information contained in this email, and any opinion expressed
in this email is personal to the sender and may not reflect the opinion of
such company. Although the network operator makes every reasonable effort
to
keep its network free from viruses, neither the network operator nor the
sender or the company for which the sender works accepts any
responsibility for computer viruses transmitted through this email or any
attachments; it is your responsibility to virus scan this email and any
attachments. Any email reply to this address may be subject to
interception or monitoring for operational reasons or for lawful business
practices.

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
That tutorial is of no use to anyone and I am surprised you closed my
question with that.
Have you actually looked at that tutorial?

I would like a more informed response.

Regards
Phillip Carruthers
dbSpaces Limited

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:

 

http://mongoc.org/libmongoc/current/tutorial.html#threading

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