Bug 266559 - Safari periodically erasing LocalStorage and IndexedDB for all websites
Summary: Safari periodically erasing LocalStorage and IndexedDB for all websites
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Website Storage (show other bugs)
Version: Safari 17
Hardware: Mac (Apple Silicon) macOS 13
: P2 Critical
Assignee: Charlie Wolfe
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2023-12-17 12:18 PST by Jeff Johnson
Modified: 2024-01-11 18:01 PST (History)
8 users (show)

See Also:


Attachments
Xcode project of Safari content blocker for third-party cookie (16.81 KB, application/zip)
2023-12-18 05:05 PST, Jeff Johnson
no flags Details
Safari preferences (13.53 KB, application/octet-stream)
2024-01-03 19:19 PST, Jeff Johnson
no flags Details
com.apple.WebKit.Networking Console log (24.86 KB, text/plain)
2024-01-09 13:38 PST, Jeff Johnson
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jeff Johnson 2023-12-17 12:18:39 PST
Safari 17.2 (18617.1.17.11.11, 18617) on macOS 13.6.3 (22G436)

For the past several days at least, Safari has been periodically, seemingly randomly erasing LocalStorage and IndexedDB for all websites simultaneously. Some visible consequences of this is that https://bsky.app is logged out, and the display settings on https://twitter.com are lost. This has been happening around once a day, sometimes twice a day. I have no idea what's causing it.

Via Time Machine backups, I discovered that it happened this morning between 9:23am and 10:47am. I actually did a full diff of my ~/Library folder backups (FileMerge with Full Disk Access is nice), but there weren't a lot of differences, and the only things that seemed interesting and relevant were in the ~/Library/Containers/com.apple.Safari/Data/Library/WebKit/WebsiteData/Default/ folder. The LocalStorage and IndexedDB folders were completely gone, though it looks like the ServiceWorkers folders survived.

Is there any good way for me to disagnose this issue?
Comment 1 Jeff Johnson 2023-12-17 18:24:47 PST
I think it may have something to do with the web inspector.

I've reproduced the issue many times today, but I still can't figure out precise steps.
Comment 2 Jeff Johnson 2023-12-18 05:05:45 PST
Created attachment 469107 [details]
Xcode project of Safari content blocker for third-party cookie
Comment 3 Jeff Johnson 2023-12-18 05:06:16 PST
I found 100% reliable steps to reproduce, but they're complicated. It turns out that the issue seems to involve both the web inspector and a Safari content blocker.

Some background: I disabled "Prevent cross-site tracking" because of data loss due to the 7-day cap on script-writeable storage. However, I still wanted third-party cookie blocking, so I wrote a little Safari content blocker with one rule:

        "action": {
            "type": "block-cookies"
        },
        "trigger": {
            "load-type": ["third-party"],
            "url-filter": ".*"
        }

Ironically, this workaround is also causing data loss. I find it very frustrating that I can't use Safari without it automatically erasing data in some way.

Steps to reproduce:
1) Download and unzip the attached sample Xcode project StopTheCookies
2) Add the file DEVELOPMENT_TEAM.xcconfig to the folder with the contents DEVELOPMENT_TEAM = [YourTeamID]
3) Build and run the Xcode project
4) Open Safari Settings
4) Enable StopTheCookies in Extensions Settings, and make sure to Allow in Private Browsing
5) Disable Prevent cross-site tracking in Privacy Settings
6) Set Safari opens with: A new private window in General Settings
7) Open http://www.sharonminsuk.com/code/storage-test.html in a non-private window
8) Enter "testing" and press the Store it button
9) Quit Safari
10) Open Safari
11) Open https://www.cnn.com in the private window
12) Open a new non-private window
13) Open http://www.sharonminsuk.com/code/storage-test.html in the non-private window
14) Close the non-private window
15) Open the web inspector in the CNN tab
16) Close the web inspector
17) Close the CNN private window
18) Open a new non-private window
19) Open http://www.sharonminsuk.com/code/storage-test.html in the non-private window

Expected results: Local storage: "testing"

Actual results: Local storage: empty
Comment 4 Jeff Johnson 2023-12-18 08:07:04 PST
Update:

After my last comment, I disabled my Safari content blocker and re-enabled "Prevent cross-site tracking". But now the issue has occurred again, so I don't think these two mentioned factors are essential to the bug.

However, I continue to think that the web inspector is involved, because I did in fact use the web inspector afterward.
Comment 5 Alexey Proskuryakov 2023-12-18 09:22:23 PST
This seems like an issue that could benefit form having a sysdiagnose collected right after reproducing, so that we could correlate logging with a timestamp (not necessary if we can reproduce, but sounds like it's not a given that we will).

I wouldn't upload a sysdiagnose here, but filing a duplicate via Feedback Assistant would be one way to make it available.
Comment 6 Radar WebKit Bug Importer 2023-12-18 09:22:32 PST
<rdar://problem/119818267>
Comment 7 Jeff Johnson 2023-12-18 12:09:47 PST
(In reply to Alexey Proskuryakov from comment #5)
> This seems like an issue that could benefit form having a sysdiagnose
> collected right after reproducing, so that we could correlate logging with a
> timestamp (not necessary if we can reproduce, but sounds like it's not a
> given that we will).
> 
> I wouldn't upload a sysdiagnose here, but filing a duplicate via Feedback
> Assistant would be one way to make it available.

I don't really have a timestamp. Except for with my steps to reproduce above, it's unclear exactly when the issue occurs, and I only discover it sometime after the fact.
Comment 8 Jeff Johnson 2023-12-24 10:09:15 PST
I've discovered semi-reliable steps to reproduce. The bug doesn't happen every time, but it happens a lot of the time with these steps. Here's a screen recording:

https://youtu.be/VMQUZwJB4x8

I'm using http://www.sharonminsuk.com/code/storage-test.html and https://javascript.info/localstorage for testing local storage.

No content blockers or extensions are enabled. Prevent cross-site tracking is enabled.

Safari is set to open with a new private window, but on further testing, this doesn't appear to be essential.

I now believe this is a web inspector bug. I've never been able to reproduce without using the web inspector. Indeed, I believe this is why the bug has affected me so much, because I'm a very frequent user of the web inspector.
Comment 9 John Wilander 2023-12-24 18:19:28 PST
(In reply to Jeff Johnson from comment #8)
> I've discovered semi-reliable steps to reproduce. The bug doesn't happen
> every time, but it happens a lot of the time with these steps. Here's a
> screen recording:
> 
> https://youtu.be/VMQUZwJB4x8
> 
> I'm using http://www.sharonminsuk.com/code/storage-test.html and
> https://javascript.info/localstorage for testing local storage.
> 
> No content blockers or extensions are enabled. Prevent cross-site tracking
> is enabled.
> 
> Safari is set to open with a new private window, but on further testing,
> this doesn't appear to be essential.
> 
> I now believe this is a web inspector bug. I've never been able to reproduce
> without using the web inspector. Indeed, I believe this is why the bug has
> affected me so much, because I'm a very frequent user of the web inspector.

Thank you for narrowing this down. We have not heard other reports of this issue which would typically be the case if it were affecting all users.

Private Browsing Mode is ephemeral so storage will only exist in-memory and go away at browser restart or if the network process were to crash. Are you using Private Browsing Mode (I see you comment on new private window)? Does it reproduce in regular browsing mode?
Comment 10 Jeff Johnson 2023-12-24 18:36:03 PST
(In reply to John Wilander from comment #9)
> (In reply to Jeff Johnson from comment #8)
> > I've discovered semi-reliable steps to reproduce. The bug doesn't happen
> > every time, but it happens a lot of the time with these steps. Here's a
> > screen recording:
> Private Browsing Mode is ephemeral so storage will only exist in-memory and
> go away at browser restart or if the network process were to crash. Are you
> using Private Browsing Mode (I see you comment on new private window)? Does
> it reproduce in regular browsing mode?

It's all in the screen recording. ;-)

The bug reproduces in regular browsing mode. I have Safari set to open with a new private window, but as I said, that's nonessential. The steps to reproduce, in text:

1) Open http://www.sharonminsuk.com/code/storage-test.html in a non-private window
2) Store "testing" in Local storage
3) Open https://javascript.info/localstorage in a non-private window
4) Press the localStorage.setItem('test', 1) button
5) Quit Safari
6) Launch Safari
7) Open https://www.cnn.com (in a private or non-private window, doesn't matter)
8) Open a new non-private window
9) Open https://javascript.info/localstorage in the new window
10) Run the localStorage.getItem('test') and confirm that the value is 1
11) Close the https://javascript.info/localstorage window
12) Open the web inspector console in the cnn.com window
13) Scroll the page slowly down to the bottom and back up to the top again
14) Close the window
15) Open a new non-private window
16) Open http://www.sharonminsuk.com/code/storage-test.html

Expected results: "testing"

Actual results: nothing currently stored. Also, https://javascript.info/localstorage now returns null.
Comment 11 Charlie Wolfe 2024-01-03 18:52:49 PST
(In reply to Jeff Johnson from comment #10)
> (In reply to John Wilander from comment #9)
> > (In reply to Jeff Johnson from comment #8)
> > > I've discovered semi-reliable steps to reproduce. The bug doesn't happen
> > > every time, but it happens a lot of the time with these steps. Here's a
> > > screen recording:
> > Private Browsing Mode is ephemeral so storage will only exist in-memory and
> > go away at browser restart or if the network process were to crash. Are you
> > using Private Browsing Mode (I see you comment on new private window)? Does
> > it reproduce in regular browsing mode?
> 
> It's all in the screen recording. ;-)
> 
> The bug reproduces in regular browsing mode. I have Safari set to open with
> a new private window, but as I said, that's nonessential. The steps to
> reproduce, in text:
> 
> 1) Open http://www.sharonminsuk.com/code/storage-test.html in a non-private
> window
> 2) Store "testing" in Local storage
> 3) Open https://javascript.info/localstorage in a non-private window
> 4) Press the localStorage.setItem('test', 1) button
> 5) Quit Safari
> 6) Launch Safari
> 7) Open https://www.cnn.com (in a private or non-private window, doesn't
> matter)
> 8) Open a new non-private window
> 9) Open https://javascript.info/localstorage in the new window
> 10) Run the localStorage.getItem('test') and confirm that the value is 1
> 11) Close the https://javascript.info/localstorage window
> 12) Open the web inspector console in the cnn.com window
> 13) Scroll the page slowly down to the bottom and back up to the top again
> 14) Close the window
> 15) Open a new non-private window
> 16) Open http://www.sharonminsuk.com/code/storage-test.html
> 
> Expected results: "testing"
> 
> Actual results: nothing currently stored. Also,
> https://javascript.info/localstorage now returns null.

I have not been able to reproduce this issue after trying these steps several times. It would be helpful if you provided a sysdiagnose taken after you have reproduced the issue, even if you cannot give an exact timestamp.
Comment 12 Jeff Johnson 2024-01-03 19:19:41 PST
Created attachment 469287 [details]
Safari preferences

I just reproduced on another machine, a Mac mini running macOS 14.2.1 and Safari 17.2.1.

I literally never upload sysdiagnose for anything, because I feel that they are too intrusive, but if it helps, I've attached the Safari preferences from the Mac mini.
Comment 13 Alexey Proskuryakov 2024-01-03 19:36:01 PST
Would you be comfortable with providing `log show` output that covers the period of time when this happens? That's the part of sysdiagnose that's most likely to be helpful here.

(probably via feedback assistant or direct email, not a Bugzilla attachment)
Comment 14 Jeff Johnson 2024-01-03 20:12:46 PST
I suspect it may depend on what gets displayed on the page at the time, perhaps the ads, because I go through periods of being able to reproduce the issue and periods of not being able to reproduce.
Comment 15 Jeff Johnson 2024-01-04 04:22:52 PST
I reproduced the bug while watching a subfolder of ~/Library/Containers/com.apple.Safari/Data/Library/WebKit/WebsiteData/Default/ in an open Finder window to see when the LocalStorage folder disappears, and it actually happens immediately when I open the web inspector in step 12, so steps 13-16 are unnecessary.

Watching the Console log at the same time, I see these messages:

GeneralStorageDirectory for session 1 is already in use by session 2

NetworkStorageManager::NetworkStorageManager path for session 1 is already in use by session 2

The message is apparently from https://bugs.webkit.org/show_bug.cgi?id=236844 Add assertion that no two sessions share the same general storage directory.
Comment 16 Ahmad Saleem 2024-01-04 20:53:48 PST
NOTE - I am not sure this is exact issue or not but on 'https://www.reddit.com/r/Safari/', I have seen similar articles / posts, where people said that they were logged out of websites and local storage not working etc.

e.g., https://www.reddit.com/r/Safari/comments/18f24f3/safari_frequently_dumping_all_cookies/

and 

https://www.reddit.com/r/Safari/comments/185cra6/safari_nuking_cookies_and_local_storage_randomly/

In above, I can see Jeff's response with UnderpassAppCompany account as well.

So it might be bit wide spread but never got reported on 'bugs.webkit.org'.
Comment 17 John Wilander 2024-01-08 19:53:24 PST
Jeff, since we have not been able to reproduce, I'm wondering if you have some setup that might matter.
- SIP disabled?
- Something that has been granted access to Safari's files?
- Some kind of anti malware software that might be cleaning things?
- Some web extensions granted access to all websites?
- Running local builds of WebKit?
Comment 18 Jeff Johnson 2024-01-08 20:00:59 PST
(In reply to John Wilander from comment #17)
> Jeff, since we have not been able to reproduce, I'm wondering if you have
> some setup that might matter.
> - SIP disabled?

No.

> - Something that has been granted access to Safari's files?

No.

> - Some kind of anti malware software that might be cleaning things?

No.

> - Some web extensions granted access to all websites?

It happens even with extensions disabled.

> - Running local builds of WebKit?

No.

Moreover, I reproduced it on two different Macs that are independent of each other. The Mac mini is a mostly stock machine that I use exclusively for testing.
Comment 19 Jeff Johnson 2024-01-09 05:25:35 PST
It may help to enable "Preserve Log" in the web inspector console. This seems to increase the frequency of the issue.

Please continue testing, because I go through periods of not being able to reproduce, and then periods of being able to reproduce. Like I said before, I can see the files disappear in Finder literally simultaneously with opening the web inspector, so it's not another unrelated app deleting the files coincidentally. It's definitely Safari.
Comment 20 Jeff Johnson 2024-01-09 11:01:16 PST
I've also been able to reproduce the bug several times with the MiniBrowser app in the latest WebKit Build Archive: https://webkit.org/build-archives/#mac-sonoma-x86_64%20arm64

There are many failures to reproduce, but it does happen after a number of tries.

For testing, I used about:blank to Set Default URL to Current URL in MiniBrowser Settings.
Comment 21 Jeff Johnson 2024-01-09 13:38:12 PST
Created attachment 469351 [details]
com.apple.WebKit.Networking Console log

I think I found the bug in the Console log from MiniBrowser. It says, "BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use". That's when the files get deleted.

Attached are the Console log messages from the com.apple.WebKit.Networking process.
Comment 22 Charlie Wolfe 2024-01-10 15:09:04 PST
(In reply to Jeff Johnson from comment #21)
> Created attachment 469351 [details]
> com.apple.WebKit.Networking Console log
> 
> I think I found the bug in the Console log from MiniBrowser. It says, "BUG
> IN CLIENT OF libsqlite3.dylib: database integrity compromised by API
> violation: vnode unlinked while in use". That's when the files get deleted.
> 
> Attached are the Console log messages from the com.apple.WebKit.Networking
> process.

Thank you! Those logs certainly appear to be relevant.

I believe I was also just able to reproduce this issue, though it is very flaky for me. It seems that when web inspector is opened, there's a possibility that the network process will delete storage from several origins, which is clearly incorrect.

When the bug reproduces, I see that `OriginStorageManager::deleteData()` is being invoked on origins where it shouldn't be. I will continue to investigate this.
Comment 23 Charlie Wolfe 2024-01-10 17:26:28 PST
Pull request: https://github.com/WebKit/WebKit/pull/22635
Comment 24 Alexey Proskuryakov 2024-01-11 08:20:30 PST
> It says, "BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use".

We should really look into this too. Having any code paths where we unlink open database files is risky indeed.
Comment 25 EWS 2024-01-11 18:01:30 PST
Committed 272951@main (cc8a261932c7): <https://commits.webkit.org/272951@main>

Reviewed commits have been landed. Closing PR #22635 and removing active labels.