<div>Jordan,</div><div><br></div><div>Thank you very much for your response.</div><div><br></div><div>Your explanation of what the optimizer is free to do in the face of @inline(never) was enlightening.  It&#39;s clear that, as Ole was suggesting, I need to make extendLifetime call through to the standard library&#39;s withExtendedLifetime to prevent the optimizer from peeking:</div><div><br></div><div>func extendLifetime&lt;T : AnyObject&gt;(_ t: T) {</div><div>    withExtendedLifetime(handle) {}<br></div><div>}</div><div><br></div><div>And thank you (very much) for calling out that race.  I went ahead with the change that you suggested, making AsyncHandle&#39;s Handle an IUO var which gets nil&#39;d prior to the call to async.  AsyncHandle now looks like</div><div><br></div><div>class AsyncHandle {</div><div><br></div><div>    var handle: Handle!</div><div><br></div><div>    deinit {</div><div>        let handle = self.handle</div><div>        self.handle = nil</div><div>        q.async {</div><div>            extendLifetime(handle)</div><div>        }</div><div>    }</div><div><br></div><div>}</div><div><br></div><div>Thanks again,</div><div>Nate Chandler</div><div><br></div><div><br><div class="gmail_quote"><div>On Mon, Feb 27, 2017 at 3:01 PM Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Feb 27, 2017, at 10:53, Ole Begemann via swift-users &lt;<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_-2304155703197011463Apple-interchange-newline gmail_msg"><div class="gmail_msg"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class="gmail_msg">On 27/02/2017 19:34, Nate Chandler via swift-users wrote:</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg"><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg">Hi Ole,<br class="gmail_msg"><br class="gmail_msg">A quick follow-up--are you suggesting calling withExtendedLifetime<br class="gmail_msg">inside the closure passed to async, as below?<br class="gmail_msg"><br class="gmail_msg">class AsyncHandle {<br class="gmail_msg"><br class="gmail_msg">   let handle: Handle<br class="gmail_msg"><br class="gmail_msg">   deinit {<br class="gmail_msg">       let handle: Handle = self.handle<br class="gmail_msg">       q.async {<br class="gmail_msg">           withExtendedLifetime(handle) {}<br class="gmail_msg">       }<br class="gmail_msg">   }<br class="gmail_msg">}<br class="gmail_msg"></blockquote><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class="gmail_msg">Yes, that&#39;s what I&#39;m suggesting. Sorry I didn&#39;t make that clear before. Since you mentioned that you couldn&#39;t use the stdlib&#39;s _fixLifetime function, I just wanted to point out that there is a public function in the stdlib for this purpose.</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg"><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg">If so, it seems that that may be a final answer to (3).  It does raise<br class="gmail_msg">an additional question, however:<br class="gmail_msg"><br class="gmail_msg">(6) Would we expect calling withExtendedLifetime to have different<br class="gmail_msg">behavior from calling an @inline(never) function from a closure enqueued<br class="gmail_msg">async from deinit?<br class="gmail_msg"></blockquote><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class="gmail_msg">Yeah, I don&#39;t know the answer, sorry.</span></div></blockquote><br class="gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">I&#39;m not an optimizer person, but expecting &#39;@inline(never)&#39; to mean anything other than &quot;don&#39;t inline this&quot; is probably set for failure. The optimizer is still allowed to look at the body of the function. withExtendedLifetime is implemented more carefully to prevent eliding the capture.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">That said, if you&#39;re <i class="gmail_msg">not</i> on when the outer deinit gets called &#39;q&#39;, you still have a race—it&#39;s possible that &#39;q&#39; will execute the block you give it before the deinitializer exits! I don&#39;t think there&#39;s a good way to avoid this other than to make &#39;handle&#39; optional (or ImplicitlyUnwrappedOptional) and explicitly set it to nil before enqueuing the &quot;deallocation block&quot;.</div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Jordan</div><br class="gmail_msg"></div></blockquote></div></div>