Bug 256698

Summary: ServiceWorker downloads fail when chunks are sent via postMessage
Product: WebKit Reporter: mrskman
Component: Service WorkersAssignee: youenn fablet <youennf>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, cdumez, mark.lam, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar
Version: Safari 16   
Hardware: iPhone / iPad   
OS: iOS 16   

Description mrskman 2023-05-12 05:06:02 PDT
Steps to reproduce:
1) Go to https://stat-info.cz/safari-sw-download-tests.html
2) Start download using Test #3 (4 GiB at least)

It looks like the ServiceWorker thread is killed usually after downloading 1-2 GiB. This doesn't happen when chunks are downloaded inside the ServiceWorker thread (tests #1 and #2).

Reproduced on iOS 16.4.1 (iPhone XS).
Working fine on macOS 13.3.1 in Safari 16.4.
Comment 1 Alexey Proskuryakov 2023-05-12 08:55:48 PDT
This sounds like the ServiceWorker process running out of memory. I didn't analyze the test, but is it holding to the downloaded data permanently in RAM? If not, going be a garbage collection problem.
Comment 2 mrskman 2023-05-13 09:40:32 PDT
No, ServiceWorker isn't holding the downloaded data permanently. It holds one chunk at a time in Test #3.

The Test #3 works like this:
1) Web page downloads a chunk as a Blob.
2) Blob is passed to the ServiceWorker via postMessage.
3) ServiceWorker converts it to ArrayBuffer.
4) ServiceWorker waits until ReadableStream requests a new chunk via pull method.
5) ServiceWorker passes the ArrayBuffer to ReadableStream.
6) ServiceWorker sends message back to the page via MessageChannel.
7) Page receives the message and goes to step 1) until the download is completed.
Comment 3 Radar WebKit Bug Importer 2023-05-19 05:06:20 PDT
<rdar://problem/109561888>
Comment 4 youenn fablet 2023-05-22 01:45:11 PDT
I tried to reproduce now but I do not think I reproduced precisely the issue.

I did not see increase of memory usage.
Another possibility is that the service worker process is getting suspended, but not the web page somehow.

I'll continue trying to reproduce a bit. @mrksman, if you can reproduce easily, can you send me a sysdiagnose (youenn@apple.com) with the timestamp of the bug?
Comment 5 youenn fablet 2023-05-22 04:33:03 PDT
@mrskman sent me a sysdiagnose.
The networking process gets jetsamed, which triggers stopping the service workers and failing the download:
2023-05-22 11:34:42.517896+0200 0x327      Default   0x0                  0      kernel: (kernel) memorystatus: killing process 5169 [com.apple.WebKit.Networking] in high band FOREGROUND (100) - memorystatus_available_pages: 36200
Comment 6 youenn fablet 2023-05-22 08:25:42 PDT
It seems we have a memory leak with URLKeepingBlobAlive isolatedCopy().
Comment 7 youenn fablet 2023-05-22 09:58:41 PDT
Pull request: https://github.com/WebKit/WebKit/pull/14184
Comment 8 EWS 2023-05-23 02:29:45 PDT
Committed 264412@main (fb9630a9c9fc): <https://commits.webkit.org/264412@main>

Reviewed commits have been landed. Closing PR #14184 and removing active labels.
Comment 9 mrskman 2023-05-23 02:34:48 PDT
Thank you for patching this issue! How long does it usually take to get a patch to iOS?