{"id":20423,"date":"2018-02-05T15:43:34","date_gmt":"2018-02-05T20:43:34","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20423"},"modified":"2024-10-09T15:38:36","modified_gmt":"2024-10-09T19:38:36","slug":"sandbox-limitation-on-number-of-files-that-can-be-opened","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/02\/05\/sandbox-limitation-on-number-of-files-that-can-be-opened\/","title":{"rendered":"Sandbox Limitation on Number of Files That Can Be Opened"},"content":{"rendered":"<p><a href=\"http:\/\/www.openradar.me\/13006144\">Matteo Rattotti<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.openradar.me\/13006144\"><p>After loading a seemingly magic random number (around 3000\/3200) of images the Sandbox will stop loading any more images. Any other operation that tries to load files from outside the container will fail.<\/p><p>The <code>NSOpenPanel<\/code> behave in a different way, it just won&rsquo;t return more than the &ldquo;magic random number&rdquo; of images, and after that any attempt to use it will return zero files.<\/p><p>If the files are loaded from inside the container, they will all load as expected, but after reaching the &ldquo;magic random number&rdquo; files from outside the container can&rsquo;t be loaded anymore.<\/p><\/blockquote>\n\n<p>I don&rsquo;t think I&rsquo;ve blogged about this before, but I&rsquo;ve heard many reports of it, and as far as I&rsquo;m aware it&rsquo;s a longstanding issue that dates to the introduction of the <tt>com.apple.security.files.user-selected.read-only<\/tt> entitlement in macOS 10.7.3. Note that this is not about the number of files that can be open simultaneously. The undocumented limit applies even if you close your file descriptors.<\/p>\n\n<p>A related issue is that I&rsquo;ve been using OmniOutliner a lot more recently, and after a while it will complain that it doesn&rsquo;t have permission to save my document. Indeed, it doesn&rsquo;t think the file even exists. I can neither save nor close the document without force quitting. The Console log makes it look like this is related to security-scoped bookmarks, which are used to access files that are saved in the <tt>Documents<\/tt> folder rather than in the application&rsquo;s container. The problem dates to at least <a href=\"https:\/\/discourse.omnigroup.com\/t\/save-error-the-document-name-could-not-be-saved-as-name-the-file-doesn-t-exist-fixed-by-repairing-permissions\/9113\">2014<\/a> and also affects OmniPlan and <a href=\"https:\/\/discussions.apple.com\/thread\/7821969\">Numbers<\/a>, adding to the likelihood that the bug is in the OS rather than apps. None of the workarounds described in the preceding forum links worked for me.<\/p>\n\n<p>Update (2018-02-05): <a href=\"https:\/\/twitter.com\/steipete\/status\/960602244638035968\">Peter Steinberger<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/steipete\/status\/960602244638035968\">\n<p>Security scoped URLs have many gotchas.<\/p>\n<\/blockquote>\n\n<p>Update (2018-02-13): I also ran into a problem where Downcast couldn&rsquo;t access any of the files <em>in its sandbox container<\/em> because of problems with security-scoped bookmarks. I had to delete everything and reset it.<\/p>\n\n<p id=\"sandbox-limitation-on-number-of-files-that-can-be-opened-update-2021-08-04\">Update (2021-08-04): <a href=\"https:\/\/twitter.com\/sindresorhus\/status\/1422631677000617985\">Sindre Sorhus<\/a> (via <a href=\"https:\/\/twitter.com\/ctietze\/status\/1422801923481247744\">Christian Tietze<\/a>):<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/sindresorhus\/status\/1422631677000617985\">\n<p>It&rsquo;s just ridiculous how they could leave this broken for so many years. I have gotten many 1 star reviews because of this issue. I even have an alert in the app to inform the user about it. (Half the number of files, since it&rsquo;s both open and save)<\/p>\n<\/blockquote>\n\n<p id=\"sandbox-limitation-on-number-of-files-that-can-be-opened-update-2021-08-10\">Update (2021-08-10): <a href=\"https:\/\/buckleyisms.com\/blog\/anecdotes-about-the-macos-sandbox-file-limit\/\">Michael Buckley<\/a> (<a href=\"https:\/\/news.ycombinator.com\/item?id=28105814\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"https:\/\/buckleyisms.com\/blog\/anecdotes-about-the-macos-sandbox-file-limit\/\">\n<p>There has recently been some <a href=\"https:\/\/twitter.com\/sindresorhus\/status\/1422631677000617985\">renewed discussion on Twitter<\/a> about the limitation that the macOS sandbox places on the number of open files an app can access at once. Apps are still running into problems because of this limitation, and there is not a lot of technical detail available on it outside of Apple, so I&rsquo;d like to share my understanding of it.<\/p>\n<p>macOS only allows sandboxed apps to access a limited number of files at a time, but there is no way for an app to query how many files it can open, or if it&rsquo;s close to the limit. In fact, this limit is dependent on the amount of RAM installed in the computer and the number of files open by other apps.<\/p>\n<\/blockquote>\n<p>I&rsquo;ve heard others say that it&rsquo;s not an &ldquo;at a time&rdquo; limit, but rather a total limit for the lifetime of the process, despite what the documentation for <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsurl\/1413736-stopaccessingsecurityscopedresou\">NSURL.stopAccessingSecurityScopedResource()<\/a> says.<\/p>\n<blockquote cite=\"https:\/\/buckleyisms.com\/blog\/anecdotes-about-the-macos-sandbox-file-limit\/\">\n<p>The engineer stayed late to help me file radars on the issue for both the security team and the fonts team, but cautioned that the underlying problem would probably not get fixed any time soon. It would be up to app developers and other teams within Apple to work around the problems the limitation created. It was foundational to how the macOS app sandbox was designed, and could only be fixed if they found a safe and performant way to store this mapping in userspace, which was unlikely. Apple was not going to throw out the sandbox or compromise its security to remove this limitation. Considering that it remains a problem to this day, he was right.<\/p>\n<p>[&#8230;]<\/p>\n<p>Considering that this limitation <a href=\"https:\/\/twitter.com\/Schwieb\/status\/1238538962710323200\">still affects high-profile apps like Microsoft Office<\/a>, I don&rsquo;t think Apple is unmotivated to fix this, but considering how long it has remained a problem, until Apple announces otherwise, we can&rsquo;t assume it&rsquo;s going away any time soon.<\/p><p>[&#8230;]<\/p>\n<p>There are two ways to deal with the issue. The first is to relinquish security-scoped bookmarks as quickly as possible, and the second is to prompt users to open folders, not files.<\/p><\/blockquote>\n<p>If you go the folder route:<\/p>\n<blockquote cite=\"https:\/\/buckleyisms.com\/blog\/anecdotes-about-the-macos-sandbox-file-limit\/\">\n<p>However, you may need to limit yourself to APIs that access those files using string paths, rather than <code>NSURLs<\/code>.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/shapeof.com\/archives\/2021\/8\/anecdotes_about_the_macos_sandbox_file_limit.html\">Gus Mueller<\/a>:<\/p>\n<blockquote cite=\"https:\/\/shapeof.com\/archives\/2021\/8\/anecdotes_about_the_macos_sandbox_file_limit.html\"><p>This is the primary reason Retrobatch isn&rsquo;t sandboxed (and thus, not on the App Store). I&rsquo;ve considered just dealing with the likely support emails (and suggesting folks give it full disk access, or only add folder), but I just can&rsquo;t bring myself to do it. What a pain this bug has been for years.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Matteo Rattotti: After loading a seemingly magic random number (around 3000\/3200) of images the Sandbox will stop loading any more images. Any other operation that tries to load files from outside the container will fail.The NSOpenPanel behave in a different way, it just won&rsquo;t return more than the &ldquo;magic random number&rdquo; of images, and after [&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":"2021-08-04T19:41:58Z","apple_news_api_id":"2fa59028-34ba-4958-a078-5879bbefef6b","apple_news_api_modified_at":"2023-12-06T19:23:36Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AL6WQKDS6SVigeFh5u-_vaw","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":[2],"tags":[131,113,2518,30,17,1529,241,580,53,2669],"class_list":["post-20423","post","type-post","status-publish","format-standard","hentry","category-technology","tag-bug","tag-downcast","tag-entitlements","tag-mac","tag-mountainlion","tag-macos-10-13","tag-numbers","tag-omnioutliner","tag-sandboxing","tag-security-scoped-bookmarks"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20423","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=20423"}],"version-history":[{"count":6,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20423\/revisions"}],"predecessor-version":[{"id":33366,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20423\/revisions\/33366"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}