{"id":49621,"date":"2025-10-14T14:56:52","date_gmt":"2025-10-14T18:56:52","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=49621"},"modified":"2025-10-21T15:38:45","modified_gmt":"2025-10-21T19:38:45","slug":"cultivated-task-cancellation","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/10\/14\/cultivated-task-cancellation\/","title":{"rendered":"Cultivated Task Cancellation"},"content":{"rendered":"<p><a href=\"https:\/\/macguru.dev\/cultivated-task-cancellation\/\">Max Seelemann<\/a> (on his <a href=\"https:\/\/macguru.dev\/and-now-i-blog\/\">new blog<\/a>):<\/p>\n<blockquote cite=\"https:\/\/macguru.dev\/cultivated-task-cancellation\/\"><p>So how do you tell if a task supports cancellation? That&rsquo;s tricky to answer per-se because cancellation is <em>voluntary behavior<\/em> and needs to be <em>represented<\/em> in the task&rsquo;s value or error type. If you&rsquo;re lucky, cancellation support (or lack thereof) is documented.<\/p><p>[&#8230;]<\/p><p>The designated way to check for cancellation is to use static properties like <code>Task.isCancelled<\/code>. Using static properties may seem odd at first, but it&rsquo;s clever: APIs <em>called inside the task<\/em> can check for cancellation without knowing where they&rsquo;re running or taking task handles as arguments. In fact, such APIs are probably the best way to handle cancellation, as we&rsquo;ll see shortly.<\/p><p>[&#8230;]<\/p><p>You might be thinking, &ldquo;okay, nice and all. But why go to all this effort to explain something that&rsquo;s baked into the system anyway?&rdquo; Well, I fear here comes an inconvenient truth. Not many things in the Standard Library and Foundation have cancellation support built in. In fact, as far as I know, it&rsquo;s just <strong>three<\/strong>.<\/p><\/blockquote>\n\n<p id=\"cultivated-task-cancellation-update-2025-10-15\">Update (<a href=\"#cultivated-task-cancellation-update-2025-10-15\">2025-10-15<\/a>): <a href=\"https:\/\/macguru.dev\/the-task-cancellation-trap\/\">Max Seelemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/macguru.dev\/the-task-cancellation-trap\/\">\n<ul><li>The <code>Task<\/code> initializer is declared as <code>@discardableResult<\/code>, which means you can create a task and let go of its handle without a warning, like <code>Task {&#8230;}<\/code>. This makes it too easy to unintentionally set up a task that&rsquo;s forgotten and runs forever.<\/li><li>It&rsquo;s too easy to forget to call the cancel method. Especially when the task is stored in an array or dictionary rather than a single instance variable. Every task handle would need to be cancelled individually when removed or replaced.<\/li><\/ul>\n<p>[&#8230;]<\/p>\n<p>To solve number 2, we wrote a little wrapper called <a href=\"https:\/\/gist.github.com\/macguru\/d39e78a9a7f3c5a9ba742a6759d700c5\"><code>ScopedTask<\/code><\/a>, which handles cancellation automatically.<\/p>\n<p>[&#8230;]<\/p>\n<p>I published the <a href=\"https:\/\/gist.github.com\/macguru\/d39e78a9a7f3c5a9ba742a6759d700c5\">source for <code>ScopedTask<\/code> on GitHub<\/a>&mdash;feel free to use or adapt it to your needs.<\/p>\n<\/blockquote>\n\n<p id=\"cultivated-task-cancellation-update-2025-10-21\">Update (<a href=\"#cultivated-task-cancellation-update-2025-10-21\">2025-10-21<\/a>): <a href=\"https:\/\/macguru.dev\/task-cancellation-handlers\/\">Max Seelemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/macguru.dev\/task-cancellation-handlers\/\">\n<p>The signature of the cancellation handler is <code>@Sendable () -&gt; Void<\/code>&mdash;a synchronous sendable non-throwing non-isolated closure with no arguments and no return value. Which means:<\/p><ul><li>You cannot &ldquo;return&rdquo; or &ldquo;throw&rdquo; out of the main operation from the handler; you can only influence its course of execution.<\/li><li>To achieve that, you must use only sendable (thread-safe) constructs.<\/li><li>And these constructs must be synchronously accessible.<\/li><\/ul><p><\/p>\n<p>[&#8230;]<\/p>\n<p>The only standard library construct that fulfills all these requirements out of the box is <code>Task<\/code> itself. We&rsquo;ll see an example shortly. But for anything else, you have no choice but to go to lower-level mechanisms&#8230;like <em>locks<\/em>.<\/p>\n<p>[&#8230;]<\/p>\n<p>So that is a simple function that suspends until cancelled. &#x1F605; Only that, in my opinion, it&rsquo;s not simple at all&mdash;it&rsquo;s really complex with numerous pitfalls small and large. Should this keep you from implementing cancellable tasks? It shouldn&rsquo;t, but you should sharpen your senses.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Max Seelemann (on his new blog): So how do you tell if a task supports cancellation? That&rsquo;s tricky to answer per-se because cancellation is voluntary behavior and needs to be represented in the task&rsquo;s value or error type. If you&rsquo;re lucky, cancellation support (or lack thereof) is documented.[&#8230;]The designated way to check for cancellation is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2025-10-14T18:56:56Z","apple_news_api_id":"038d0cdf-9fb1-40af-b312-3d64c851056a","apple_news_api_modified_at":"2025-10-21T19:38:47Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AA40M35-xQK-zEj1kyFEFag","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[31,2741,30,2742,71,2200,901],"class_list":["post-49621","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-26","tag-mac","tag-macos-tahoe-26","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=49621"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49621\/revisions"}],"predecessor-version":[{"id":49711,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49621\/revisions\/49711"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=49621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=49621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=49621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}