# Dispatch precondition false positive?

**URL:** <https://forums.swift.org/t/dispatch-precondition-false-positive/28498>\
**Category:** Using Swift\
**Created:** [September 4, 2019, 8:52am UTC](https://forums.swift.org/t/dispatch-precondition-false-positive/28498 "2019-09-04T08:52:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [September 4, 2019, 8:52am UTC](https://forums.swift.org/t/dispatch-precondition-false-positive/28498/1 "2019-09-04T08:52:40Z")

</div>

I'm not sure if this belongs in the Apple developer forums, rather than the Swift forums, but I think Dispatch is part of Swift, so it might fit. Please let me know if this is better asked elsewhere.

I have written a small promise type for a project I'm working on, and it allows us to specify which queue to run a completion handler on. This had a bug, so in order to test the semantics of this, I wrote a (supposedly failing) test using:

```swift
promise.onCompletion(on: .queue(expectedQueue)) { _ in
    dispatchPrecondition(condition: .onQueue(expectedQueue))
    exp.fulfill()
}

```

However, it produces a false positive, as far as I can tell. That is, the condition passes, even when the queue is in fact not the correct queue. If I instead change the test to

```swift
promise.onCompletion(on: .queue(expectedQueue)) { _ in
    let currentLabel = String(validatingUTF8: __dispatch_queue_get_label(nil))!
    XCTAssertEqual(expectedQueue.label, currentLabel)
    exp.fulfill()
}

```

… the test fails as expected.

What am I getting wrong? Is this a bug in `dispatchPrecondition` or am I misunderstanding the semantics of how it is supposed to work?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 4, 2019, 9:15am UTC](https://forums.swift.org/t/dispatch-precondition-false-positive/28498/2 "2019-09-04T09:15:17Z")

</div>

Do you use target queues? Do you dispatch synchronously or not? I mean, could you not be on your expected queue, after all?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [September 4, 2019, 10:22am UTC](https://forums.swift.org/t/dispatch-precondition-false-positive/28498/3 "2019-09-04T10:22:23Z")

</div>

Queues are not necessarily unique.

> [@What is the default target queue for a serial queue?](https://forums.swift.org/t/what-is-the-default-target-queue-for-a-serial-queue/18094):
>
> I just recently noticed that dispatch queues always target another queue by default even if not explicitly specified. For concurrent queues it will target the global queue with QoS .default. However the precondition in the following code will fail if I try to use it on serial queue. Does a serial queue has a default target queue at all? What behavior should I expect? let serial = DispatchQueue(label: "serial") let concurrent = DispatchQueue(label: "concurrent", attributes: .concurrent) …

Check if anything related applies in your setup.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [September 4, 2019, 10:48am UTC](https://forums.swift.org/t/dispatch-precondition-false-positive/28498/4 "2019-09-04T10:48:23Z")

</div>

> [@gwendal.roue](#):
>
> Do you use target queues? Do you dispatch synchronously or not? I mean, could you not be on your expected queue, after all?

My `Promise` type has a private (serial) `mutationQueue`. All read and write access to `result` and `completionHandlers` are performed synchronously on this queue. This is because `onCompletion(on:execute:)` and `resolve(with:)` can be called in any order on any queue, and I want to eliminate race conditions, if a completion block is added while the promise is being resolved.

In addition my `onCompletion` has an `ExecutionContext` type that wraps a closure. It only exposes a single function `execute` which combines the concept of `DispatchQueue.async { ... }`, `DispatchQueue.sync { ... }`, `DispatchQueue.asyncAfter(deadline: ...) { ... } ` and simply `{ ... }()`, so that I can specify how I want my code to run.

The bug in my code was that when I specified `.immediate` as the execution context, and the promise was resolved, it got executed without dispatch (as it should), but it got executed from within the `mutationQueue`, and not from queue that either `onCompletion` or `resolve` was called from.

I fixed the bug, but I was confused as to why my tests didn't fail. I explicitly had a test like this:

```swift
func testOnCompletion_onDefaultContext_whenResolvedOnAQueue_shouldExecuteOnSameQueue() {
    let queue = DispatchQueue(label: "expected")
    let exp = expectation(description: "should resolve on same queue")
    Promise<Void, Error>
        // (1) this returns an unresolved promise, and causes resolve() to 
        // be called on 'queue' after 100 milliseconds
        .delayed(.milliseconds(100), on: queue, with: .success)
        // (2) this adds a completion handler that should be executed
        // as soon as the promise is resolved, either on the queue 
        // that 'onCompletion' is called from (if already resolved), or
        // the queue that 'resolve' is called from (if not yet resolved) (ie. 'queue')
        .onCompletion(on: .immediate) { result in
            dispatchPrecondition(condition: .onQueue(queue))
            exp.fulfill()
        }
    waitForExpectations(timeout: 1.0)
}

```

The `dispatchPrecondition` passed. But when I changed the test to compare queue labels instead, it failed with:

```swift
XCTAssertEqual failed: ("expected") is not equal to ("promise.mutation_sync")

```
