Original Description
The original description is unchanged below. See additional information below under Diagnosis.
We use the C# GridFS Package 2.2.3/2.2.4 against a Replica Set running MongoDB 3.2.6 and WiredTiger.
We're facing the following Problem:
- Open a Stream using OpenDownloadStreamByNameAsync with a CancellationToken results in hanging the Application
- Our GridFS do have about about 500K to xxxxK Chunks
After comparing various Installation of our Application on differnet Platforms (DEV, INT, STAGING, PROD),
we noticed, that the Index files_id_1_n_1 in .chunks collection and filename_1_uploadDate_1 in .files is missing.
We also reference to this Article: https://www.mongodb.com/blog/post/building-mongodb-applications-binary-files-using-gridfs-part-2
On the event on creating the "files_id_1_n_1 " index on the .chunks collection manually, the platform worked as expected.
After analysing the C# Driver code on GitHub, we understood that the indecies are created by the driver (and not the server).
Under which conditions, this index is created / or not created?
- could it be, that if the cancellation token is set to a small timeout (e.g 5 Seconds), the index is never created (because it takes longer to build - and get cancelled)?
- is the index needed by the driver - and blocks internally if not there?
- is it safe to create the index manually?
This issue is really urgent to us because it affects our caching-strategy and the recreation of those indexes on our production environment takes to much time. When we recreate that index the mongoDb takes all tickets available which makes our application unusable ! We can't even connect to the primary though the shell or mongochef.
Best regards,
Alessandro Catale
__________________________________________________________
ICT Application Operation Manager
Office +41 58 221 48 55
Mobile +41 79 271 04 12
alessandro.catale@swisscom.com
__________________________________________________________
Swisscom IT Services AG
Outsourcing Services
Pfingstweidstrasse 51
8005 Zürich
www.swisscom.ch/it-services
Swisscom IT Services
Outsourcing | Workplace | SAP | Finance
Diagnosis
There is a race condition in the EnsureIndexes method: https://github.com/mongodb/mongo-csharp-driver/blob/0ad339c4c889076680245c3786bddf3ddd2654e3/src/MongoDB.Driver.GridFS/GridFSBucket.cs#L768
If two threads attempt to upload a file at the same time it is possible that the first thread will acquire the __ensuredIndexes lock on line 770 and then see a non-empty collection on line 773 because the second thread might have uploaded a different file in the meantime.