title: Unobvious crash due to unbalanced call to dispatch group
body: <p>I received an crash report <code>BUG IN CLIENT OF LIBDISPATCH: Unbalanced call to dispatch_group_leave()</code>.</p>
  
  <p>The anonymized code which stacktrace points on, looks like:</p>
  
  <pre><code> func updateHealthKitData(from startDate: Date, to endDate: Date, completion: @escaping () -&gt; ()) {
       let group = DispatchGroup()
       group.enter()
       self.hkDataStore.getData1(startDate: startDate, endDate: endDate, completion: {(data1) in
           if let samplesToProcess = data1 {
               self.processData1(data: samplesToProcess, completion: {() in
                   group.leave()
               })
           } else {
               group.leave()
           }
       })
       group.enter()
       self.hkDataStore.getData2(startDate: startDate, completion: {(data2) in
           if let samplesToProcess = data2 {
               self.processData2(data: samplesToProcess, completion: {() in
                   group.leave()
               })
           } else {
               group.leave()
           }
       })
  
       group.notify(queue: .main) {
           completion()
       }
   }
  </code></pre>
  
  <p>The exception came from queue <code>com.apple.HealthKit.HKHealthStore.client</code>.
  The tasks <code>getData2</code> and <code>getData1</code> query health-kit in background queue.</p>
  
  <p>I read that dispatch group is thread-safe enough i.e. it can be used from other queues without <code>synchronize</code>.
  The Group dispatch enters twice before doing tasks and only after that it waits for balance to notify when tasks will be done.
  I double checked that any completions (and nested ones) aren't called twice which may lead to extra call to dispatch group.</p>
  
  <p>What can be wrong here ? Thanks in advance for any help and thoughts!</p>