BulkWrite freezes upon writing 0 records

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Done
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.4.3
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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);
              }
      

            Assignee:
            Robert Stam (Inactive)
            Reporter:
            Matthew Czarnek
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: