Bug 255458

Summary: `navigator.storage.getDirectory()` fails in nested workers with an `InvalidStateError`
Product: WebKit Reporter: Thomas Steiner <tomac>
Component: New BugsAssignee: Sihui Liu <sihui_liu>
Status: RESOLVED FIXED    
Severity: Normal CC: cdumez, karlcow, sihui_liu, webkit-bug-importer
Priority: P2 Keywords: BrowserCompat, InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 22723    
Attachments:
Description Flags
Safari
none
Chrome none

Description Thomas Steiner 2023-04-14 08:57:07 PDT
Created attachment 465914 [details]
Safari

1. Go to https://opfs-in-subworker.glitch.me/ in Chrome or Firefox.
2. Open the Console.
3. Note how calls to `navigator.storage.getDirectory()` succeed in the main thread, a worker, and a subworker.
4. Go to https://opfs-in-subworker.glitch.me/ in Safari.
5. Open the Console.
6. Note how calls to `navigator.storage.getDirectory()` succeed in the main thread, a worker, but fail in a subworker with an `InvalidStateError`.

## Console dump from Safari:

[Log] Succeeded in main thread (script.js, line 4)
FileSystemDirectoryHandle kind: "directory" name: "" FileSystemDirectoryHandle Prototype

[Log] Succeeded in worker.js (worker.js, line 4)
FileSystemDirectoryHandle kind: "directory" name: "" FileSystemDirectoryHandle Prototype

[Log] Message from subworker.js: (worker.js, line 12)
Object error: InvalidStateError: The object is in an invalid state. code: 11 message: "The object is in an invalid state." name: "InvalidStateError" Error Prototype message: "Failed in subworker.js InvalidStateError: The object is in an invalid state."
Object Prototype

I believe the problem lies somewhere here: https://github.com/WebKit/WebKit/blob/9da2b02e31a2ba79c73354b04dc59aea0d04bb88/Source/WebCore/Modules/storage/StorageManager.cpp#L138-L141.
Comment 1 Thomas Steiner 2023-04-14 08:59:19 PDT
Created attachment 465915 [details]
Chrome

For comparison, the Console output in Chrome.
Comment 2 Thomas Steiner 2023-04-14 09:01:55 PDT
(Note that due to https://bugs.webkit.org/show_bug.cgi?id=255402 I had to `postMessage()` any logs from subworker.js to worker.js to see anything.)
Comment 4 Radar WebKit Bug Importer 2023-04-14 15:52:26 PDT
<rdar://problem/108069396>
Comment 5 Sihui Liu 2023-04-17 16:50:56 PDT
Pull request: https://github.com/WebKit/WebKit/pull/12833
Comment 6 EWS 2023-04-18 08:52:15 PDT
Committed 263075@main (808b1c7f159a): <https://commits.webkit.org/263075@main>

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