{"id":103973,"date":"2020-07-10T07:00:00","date_gmt":"2020-07-10T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103973"},"modified":"2020-07-10T06:55:34","modified_gmt":"2020-07-10T13:55:34","slug":"20200710-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200710-00\/?p=103973","title":{"rendered":"Cancelling a Windows Runtime asynchronous operation, part 8: C++\/WinRT, revised"},"content":{"rendered":"<p>Some time ago, we observed that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200707-00\/?p=103960\"> C++\/WinRT relies on the ABI result from the <code>Get\u00adResults()<\/code> method to report cancellation<\/a>. This is different from how task cancellation is projected in C# and C++\/CX. Furthermore, it&#8217;s a leaky abstraction: The awaiting code needs to know how the underlying operation is implemented in order to know what exception will be raised upon cancellation.<\/p>\n<p>Fortunately, that has been fixed in <a href=\"https:\/\/github.com\/microsoft\/cppwinrt\/pull\/643\">PR 643<\/a> (released as <a href=\"https:\/\/github.com\/microsoft\/cppwinrt\/releases\/tag\/2.0.200601.2\"> version 2.0.200601.2<\/a>) so that C++\/WinRT aligns with the other projections in how it handles cancellation: It now detects cancellation by checking the status of the operation.<\/p>\n<p>Conceptually, it&#8217;s a one-line fix:<\/p>\n<pre>template &lt;typename Async&gt;\r\nstruct await_adapter\r\n{\r\n    ...\r\n\r\n    auto await_resume() const\r\n    {\r\n        <span style=\"color: blue;\">if (async.Status() == AsyncStatus::Canceled) throw hresult_canceled();<\/span>\r\n        return async.GetResults();\r\n    }\r\n};\r\n<\/pre>\n<p>However, this adds the virtual method call <code>async.Status()<\/code> to a hot code path. Even worse, if this is a remote operation, the virtual method call has to cross a process boundary, which is even more expensive. (If you enable <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190430-00\/?p=102460\"> Async-Async<\/a>, then this becomes a local query, but it&#8217;s still virtual.)<\/p>\n<p>The fix is to cache the status reported by the <code>Completed<\/code> callback:<\/p>\n<pre><span style=\"color: blue;\">inline void check_status_canceled(AsyncStatus status)\r\n{\r\n    if (status == AsyncStatus::Canceled) throw hresult_canceled();\r\n}<\/span>\r\n\r\ntemplate &lt;typename Async&gt;\r\nstruct await_adapter\r\n{\r\n    <span style=\"color: blue;\">AsyncStatus status = AsyncStatus::Started;<\/span>\r\n    ...\r\n\r\n    void await_suspend(std::experimental::coroutine_handle&lt;&gt; handle)\r\n    {\r\n        async.Completed([<span style=\"color: blue;\">this<\/span>, handler = disconnect_aware_handler{ handle }]\r\n                        (auto&amp;&amp;, auto&amp;&amp; operation_status)\r\n        {\r\n            <span style=\"color: blue;\">status = operation_status;<\/span>\r\n            handler();\r\n        });\r\n    }\r\n\r\n    auto await_resume() const\r\n    {\r\n        <span style=\"color: blue;\">check_status_canceled(status);<\/span>\r\n        return async.GetResults();\r\n    }\r\n};\r\n<\/pre>\n<p>The code to convert a <code>Canceled<\/code> status into an exception was factored out, because similar fixes need to be made to other methods that react to the result of an operation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the completion result.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Using the completion result.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103973\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}