title: Is this crash in a Swift script caused by a race condition?
body: <p>Running the code below usually prints:</p>
  <pre><code>hello
  </code></pre>
  <p>Occasionally, it prints:</p>
  <pre><code>hello BlockExperiment world
  </code></pre>
  <p>But sometimes I get the following crash:</p>
  <pre><code>hello BlockExperiment/usr/bin/swift[0x51f95a4]
  /usr/bin/swift[0x51f719e]
  /usr/bin/swift[0x51f987c]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x12980)[0x7fc7c0650980]
  /usr/lib/swift/linux/libswiftCore.so(+0x40d2c4)[0x7fc7bc9642c4]
  /usr/lib/swift/linux/libswiftCore.so(+0x3fecc6)[0x7fc7bc955cc6]
  /usr/lib/swift/linux/libswiftCore.so(+0x3fe20b)[0x7fc7bc95520b]
  /usr/lib/swift/linux/libswiftCore.so(+0x3e456d)[0x7fc7bc93b56d]
  </code></pre>
  <p>This doesn't seem to break any of the <code>weak</code>/<code>strong</code>/<code>unowned</code> practices that I know of.
  Is the problem a basic race condition, where <code>self</code> is being accessed in the print statement after it has already been deallocated?</p>
  <p>I can't be sure because in that case, I would expect the usual error message saying the access was to a deallocated instance. If it isn't this, how can you tell and in general debug this type of issue?</p>
  <p>And if it is a race condition, why does the print statement get cut off sometimes after &quot;hello&quot;?</p>
  <p>Code:</p>
  <pre><code>import Foundation
  
  final class BlockRunner {
    var block: () -&gt; ()
    init(_ b: @escaping () -&gt; ()) { block = b }
    func run() { block() }
    deinit { print(&quot;Deinit: \(self)&quot;) }
  }
  
  final class BlockExperiment {
    func f() { 
      let queue = DispatchQueue(label: &quot;Queue&quot;)
      let runner = BlockRunner { print(&quot;hello&quot;, self, &quot;world&quot;) }
      queue.asyncAfter(deadline: .now() + 1) { runner.run() }
    }
    deinit { print(&quot;Deinit: \(self)&quot;) }
  }
  
  do {
    let e = BlockExperiment()
    e.f()
    RunLoop.current.run(until: Date() + 1 + 0.000000000000001)
  }
  </code></pre>