{"id":38319,"date":"2023-01-30T15:16:54","date_gmt":"2023-01-30T20:16:54","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=38319"},"modified":"2023-01-31T08:04:27","modified_gmt":"2023-01-31T13:04:27","slug":"nsurlsession-connection-leak","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/01\/30\/nsurlsession-connection-leak\/","title":{"rendered":"NSURLSession Connection Leak"},"content":{"rendered":"<p><a href=\"https:\/\/lapcatsoftware.com\/articles\/NSURLSession.html\">Jeff Johnson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/lapcatsoftware.com\/articles\/NSURLSession.html\"><p>What it doesn&rsquo;t tell you is that if you don&rsquo;t invalidate the session (via <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsurlsession\/1407428-finishtasksandinvalidate?language=objc\">finishTasksAndInvalidate<\/a> or <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsurlsession\/1411538-invalidateandcancel?language=objc\">invalidateAndCancel<\/a>), then the internet connection created by the session remains open until the app terminates, even after the delegate method <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsurlsessiontaskdelegate\/1411610-urlsession?language=objc\">URLSession:task:didCompleteWithError:<\/a> has been called, and even after the app&rsquo;s code no longer has a strong reference to the session. It&rsquo;s more than just a potential memory leak.<\/p><p>[&#8230;]<\/p><p>The <code>NSURLSession<\/code> API seems peculiar, because you would expect <code>URLSession:task:didCompleteWithError:<\/code> to be, you know, the end. Shouldn&rsquo;t you be able to freely (pun intended) dispose of the connection at that point? The reality, however, is that you need to invalidate <em>every<\/em> used session.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jeff Johnson: What it doesn&rsquo;t tell you is that if you don&rsquo;t invalidate the session (via finishTasksAndInvalidate or invalidateAndCancel), then the internet connection created by the session remains open until the app terminates, even after the delegate method URLSession:task:didCompleteWithError: has been called, and even after the app&rsquo;s code no longer has a strong reference to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-01-30T20:16:58Z","apple_news_api_id":"da8c77a7-664a-48ce-a4ef-79d87d32404c","apple_news_api_modified_at":"2023-01-31T13:04:30Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/A2ox3p2ZKSM6k73nYfTJATA","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":[69,30,2223,571,476,71],"class_list":["post-38319","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-mac","tag-macos-13-ventura","tag-memory-management","tag-networking","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38319","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=38319"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38319\/revisions"}],"predecessor-version":[{"id":38331,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38319\/revisions\/38331"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=38319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=38319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=38319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}