Bug 239432

Summary: Suspend storage operations when network process is in background and will be suspended
Product: WebKit Reporter: Sihui Liu <sihui_liu>
Component: New BugsAssignee: Sihui Liu <sihui_liu>
Status: RESOLVED DUPLICATE    
Severity: Normal CC: cdumez, ggaren, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
ews-feeder: commit-queue-
Patch
ews-feeder: commit-queue-
Patch
ews-feeder: commit-queue-
Patch
none
Patch
ews-feeder: commit-queue-
Patch
none
Patch
none
Patch
none
Patch
ews-feeder: commit-queue-
Patch none

Description Sihui Liu 2022-04-17 00:34:15 PDT
...
Comment 1 Sihui Liu 2022-04-17 00:49:37 PDT
Created attachment 457769 [details]
Patch
Comment 2 Sihui Liu 2022-04-17 17:56:24 PDT
Created attachment 457787 [details]
Patch
Comment 3 Sihui Liu 2022-04-17 22:26:15 PDT
Created attachment 457793 [details]
Patch
Comment 4 Sihui Liu 2022-04-18 16:18:07 PDT
Created attachment 457833 [details]
Patch
Comment 5 Sihui Liu 2022-04-18 16:52:35 PDT
Created attachment 457840 [details]
Patch
Comment 6 Sihui Liu 2022-04-18 18:25:11 PDT
Created attachment 457846 [details]
Patch
Comment 7 Sihui Liu 2022-04-19 09:13:56 PDT
Created attachment 457906 [details]
Patch
Comment 8 Chris Dumez 2022-04-19 09:22:56 PDT
Comment on attachment 457906 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=457906&action=review

> Source/WebKit/ChangeLog:9
> +        However, this is not a very reliable singal, because UI process may not send the message before process 

typo: singal

> Source/WebKit/ChangeLog:13
> +        background notification as signal of suspension. It's possible that application process holds special assertion

I can think of 2 issues here:
1. We are not supposed to use RunningBoard in child processes for security reasons AFAIK.
2. What if an app gets backgrounded, the timer starts (you suspend the db thread), then the app starts playing audible media and the RunningBoard timer stops?

Presumably then, your db thread says suspended and we'll end up with the same old hangs when the page's JS does DB operations? I say "same old" because this is the kind of bug I caused the last time I tried to rely on app visibility to make suspension decisions (as mentioned off-line).
Comment 9 Sihui Liu 2022-04-21 12:49:58 PDT
Created attachment 458084 [details]
Patch
Comment 10 Sihui Liu 2022-04-21 13:42:25 PDT
Created attachment 458089 [details]
Patch
Comment 11 Sihui Liu 2022-04-22 14:41:39 PDT
Created attachment 458175 [details]
Patch
Comment 12 Sihui Liu 2022-04-22 15:15:06 PDT
Created attachment 458176 [details]
Patch
Comment 13 Sihui Liu 2022-04-22 15:29:05 PDT
Created attachment 458177 [details]
Patch
Comment 14 Sihui Liu 2022-04-22 15:46:35 PDT
Created attachment 458181 [details]
Patch
Comment 15 Radar WebKit Bug Importer 2022-04-24 00:35:15 PDT
<rdar://problem/92228190>
Comment 16 Sihui Liu 2022-04-25 14:08:46 PDT
Created attachment 458297 [details]
Patch
Comment 17 Sihui Liu 2022-04-25 15:14:05 PDT
Created attachment 458302 [details]
Patch
Comment 18 Chris Dumez 2022-04-27 16:48:59 PDT
Comment on attachment 458302 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=458302&action=review

> Source/WebKit/ChangeLog:10
> +        However, this is not a very reliable singal, because UI process may not send the message before process 

typo: singal

> Source/WebKit/ChangeLog:20
> +        may send the suspend message earlier if ProcessThrottler says there is no activity left.

So, the odds are that the WebContent process will keep running for those 5 seconds where the network process db thread is suspended. As a result, wouldn't it cause a WebContent process hang in case of Webstorage usage (which does sync IPC to the network process).
If so, the WebProcess likely won't process the PrepareToSuspend IPC from the UIProcess. This means the WebProcess will not free its memory or make itself eligible for the freezer, making it way more likely to get jetsammed in the background.

I wouldn't want to trade network process kills on suspensions with increased WebProcess jetsams (which are more noticeable to the user IMO).

Or am I wrong and your patch deals with this in some way?

> Source/WebKit/NetworkProcess/NetworkProcess.cpp:1536
> +        WTFLogAlways("sihuil: [%p]NetworkProcess::deleteWebsiteData will removeAllPushSubscriptions session[%llu]", this, sessionID.toUInt64());

Oops.
Comment 19 Sihui Liu 2022-04-27 17:25:36 PDT
Comment on attachment 458302 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=458302&action=review

>> Source/WebKit/ChangeLog:10
>> +        However, this is not a very reliable singal, because UI process may not send the message before process 
> 
> typo: singal

Will change.

>> Source/WebKit/ChangeLog:20
>> +        may send the suspend message earlier if ProcessThrottler says there is no activity left.
> 
> So, the odds are that the WebContent process will keep running for those 5 seconds where the network process db thread is suspended. As a result, wouldn't it cause a WebContent process hang in case of Webstorage usage (which does sync IPC to the network process).
> If so, the WebProcess likely won't process the PrepareToSuspend IPC from the UIProcess. This means the WebProcess will not free its memory or make itself eligible for the freezer, making it way more likely to get jetsammed in the background.
> 
> I wouldn't want to trade network process kills on suspensions with increased WebProcess jetsams (which are more noticeable to the user IMO).
> 
> Or am I wrong and your patch deals with this in some way?

Yes, it's possible that this increases jetsam rate of web process, when:
1. web process needs to import StorageAreaMap syncly for executing localStorage or sessionStorage operations immediately (note we import it asyncly when an origin is loaded).
2. web process keeps running in background for over 25 seconds (with some background activities) and receives assertionWillInvalidate callback (sends prepareToSuspend) after ProcessStateMonitor's timer fires (note when process gets the assertion in less than 5s to the timeout, it will not receive invalidate message, thus not sending prepareToSuspend; leading to the process be jetsammed anyways).
3. web process will not be jetsammed if prepareToSuspend is called; and will be jetsammed if prepareToSuspend is not called.
Considering the probability of meeting all conditions, the chance of increasing jetsam rate is not very likely to be noticeable.
Also, if a web process uses much memory and runs for a long time in background, I think it's not surprising that it will get jetsammed at some point.

A complete fix would be using ProcessStateMonitor to send prepareToSuspend and processDidResume messages (merging prepareToSuspend with setStorageOperationsSuspended message), and maybe ensure UI process gets the reply of prepareToSuspend from web processes before sending it to network process; but this change might be even more risky. I'd like to view the patch as a step towards the complete fix; and let's see how ProcessStateMonitor works in production.

>> Source/WebKit/NetworkProcess/NetworkProcess.cpp:1536
>> +        WTFLogAlways("sihuil: [%p]NetworkProcess::deleteWebsiteData will removeAllPushSubscriptions session[%llu]", this, sessionID.toUInt64());
> 
> Oops.

:(
Comment 20 Sihui Liu 2022-04-27 17:29:47 PDT
Created attachment 458483 [details]
Patch
Comment 21 Sihui Liu 2022-04-29 10:56:15 PDT
Created attachment 458596 [details]
Patch
Comment 22 Sihui Liu 2022-04-29 11:02:07 PDT
Created attachment 458597 [details]
Patch
Comment 23 Sihui Liu 2022-04-29 11:38:18 PDT
Created attachment 458599 [details]
Patch
Comment 24 Sihui Liu 2022-05-17 14:33:07 PDT

*** This bug has been marked as a duplicate of bug 240359 ***