title: dispatch_group_leave crash in swift
body: <p>This happens very rarely. Here is the last line of the stack trace:</p>
  
  <pre><code>0  libdispatch.dylib              0x0000000197a85a9c dispatch_group_leave + 48
  </code></pre>
  
  <p>dispatch_group_leave is called in a complete closure which is invoked like this:</p>
  
  <pre><code>  dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                  { () -&gt; Void in
                      let query = HKStatisticsCollectionQuery(quantityType: quantityType,
                          quantitySamplePredicate: nil,
                          options: statisticOptions,
                          anchorDate: anchorDate,
                          intervalComponents: interval)
                      query.initialResultsHandler = {
  
                          complete()
  </code></pre>
  
  <p>So we dispatch to a background thread, run a HKStatisticsCollectionQuery, and then call a function parameter closure called complete. Inside complete is where the dispatch_group_leave is called and the crash happens. </p>
  
  <p>Any ideas are most appreciated!
  Thanks!</p>