Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1956

BulkWrite freezes upon writing 0 records

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.4.3
    • None
    • None

    Description

      I'm finding that when I call collection_name.BulkWrite and pass in a List<WriteModel<>> that has 0 elements in it, BulkWrite will freeze and seems stuck in an infinite loop or something.

      Easy enough to fix but considering I was multi-threading this and it was rare for this to occur.. it was a pain to debug.

      To be far, it throws an exception I was catching and ignoring But why not just let it write 0 records? And simply don't actually do the write if the List passed in is empty?

      Here is a snippet of code that was causing the crash if it helps:

      public void StoreXmlDocumentWithCaching(String xmlDoc, int recordNum, bool Failed = false, string BatchDate = "", string queryLocation = "")
              {
                  DebuggingHelper.PrintMethodCheckpointMsg(0);
                  var filterRecord = Builders<Record>.Filter.Eq("RecordNum", recordNum);
                  var updateRecord = Builders<Record>.Update.Set("Document", xmlDoc).Set("BatchDate", BatchDate).Set("Failed", Failed).Set("Sent", false).Set("DeleteMe", false).Set("queryLocation", queryLocation).Set("SendMutex",(int?)null);
       
                  try
                  {
                      UpdateOneModel<Record> updateRecordModel = new UpdateOneModel<Record>(filterRecord, updateRecord) { IsUpsert = true };
       
                      cacheMutex.WaitOne();
                          _cacheRecords.Add(updateRecordModel);
                      cacheMutex.ReleaseMutex();
                  }
                  catch (MongoWriteException e)
                  {
                      Console.WriteLine(e);
                  }
                  
                  cacheMutex.WaitOne();
                  docsCached++;
       
                  if (docsCached >= numDocsToCache)
                  {
                      //Send all the cached updates at once.
                      _recordCollection.BulkWrite(_cacheRecords);
                      //And reset docsCached
                      _cacheRecords.Clear();
                      docsCached = 0;
                  }
                  cacheMutex.ReleaseMutex();
                  DebuggingHelper.PrintMethodCheckpointMsg(1);
              }
       
              public void FlushStoreXmlCache()
              {
                  DebuggingHelper.PrintMethodCheckpointMsg(0);
                  cacheMutex.WaitOne();
       
                  //write the remaining
                  _recordCollection.BulkWrite(_cacheRecords);   <- this line freezes the thread for many hours
       
                  //And reset cached docs
                  _cacheRecords.Clear();
                  docsCached = 0;
       
                  cacheMutex.ReleaseMutex();
                  DebuggingHelper.PrintMethodCheckpointMsg(1);
              }
      

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            mczarnek Matthew Czarnek
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: