Bug 185975

Summary: Ensure that the Web Content process doesn't sleep during initialization
Product: WebKit Reporter: Tim Horton <thorton>
Component: New BugsAssignee: Tim Horton <thorton>
Status: RESOLVED FIXED    
Severity: Normal CC: aestes, bdakin, cdumez, commit-queue, ggaren, webkit-bug-importer, wenson_hsieh
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch none

Description Tim Horton 2018-05-25 00:11:11 PDT
Ensure that the Web Content process doesn't sleep during initialization
Comment 1 Radar WebKit Bug Importer 2018-05-25 00:11:48 PDT
<rdar://problem/40548159>
Comment 2 Tim Horton 2018-05-25 00:13:31 PDT
Created attachment 341256 [details]
Patch
Comment 3 Tim Horton 2018-05-25 02:16:51 PDT
Comment on attachment 341256 [details]
Patch

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

> Source/WebKit/UIProcess/WebProcessProxy.cpp:1426
> +    m_backgroundActivityTokenForInitialization = nullptr;

Need to check if we have to clear this if the process crashes in the meantime.
Comment 4 Chris Dumez 2018-05-25 08:39:10 PDT
Comment on attachment 341256 [details]
Patch

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

>> Source/WebKit/UIProcess/WebProcessProxy.cpp:1426
>> +    m_backgroundActivityTokenForInitialization = nullptr;
> 
> Need to check if we have to clear this if the process crashes in the meantime.

Yes, I believe we do need to.
Comment 5 Andy Estes 2018-05-25 09:38:40 PDT
Comment on attachment 341256 [details]
Patch

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

> Source/WebKit/UIProcess/WebProcessPool.cpp:983
> +    // This must remain after the last message sent in this function,
> +    // otherwise the Web Content process could be suspended before
> +    // initialization is complete.
> +    process.didSendAllProcessInitializationMessages();

Could you use a ScopeExit for this to ensure this always runs last? I'm worried that maybe someone will add an early return in the future.
Comment 6 Geoffrey Garen 2018-05-25 09:53:48 PDT
Comment on attachment 341256 [details]
Patch

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

>> Source/WebKit/UIProcess/WebProcessPool.cpp:983
>> +    process.didSendAllProcessInitializationMessages();
> 
> Could you use a ScopeExit for this to ensure this always runs last? I'm worried that maybe someone will add an early return in the future.

Why not just take the assertion inside WebProcessProxy::didFinishLaunching or some similar function in WebProcessProxy?

That would get you a tighter abstraction.

The background assertion can be an internal implementation detail of WebProcessProxy startup, and then we don't have to enforce order relative to commands issued by WebProcessPool.

The contract WebProcessProxy should expose to clients is "If you launch me, I will always finish launching before sleeping. You don't have to do anything special to make this happen, and you also can't stop it from happening."

>>> Source/WebKit/UIProcess/WebProcessProxy.cpp:1426
>>> +    m_backgroundActivityTokenForInitialization = nullptr;
>> 
>> Need to check if we have to clear this if the process crashes in the meantime.
> 
> Yes, I believe we do need to.

You can just clear the token unconditionally in the crash handler. Probably doesn't make a big difference, since a live background assertion for a crashed process probably does nothing. But still good hygiene.
Comment 7 Tim Horton 2018-05-25 09:53:50 PDT
Comment on attachment 341256 [details]
Patch

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

>> Source/WebKit/UIProcess/WebProcessPool.cpp:983
>> +    process.didSendAllProcessInitializationMessages();
> 
> Could you use a ScopeExit for this to ensure this always runs last? I'm worried that maybe someone will add an early return in the future.

Now there’s a good idea.
Comment 8 Tim Horton 2018-05-25 10:03:34 PDT
(In reply to Geoffrey Garen from comment #6)
> Comment on attachment 341256 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=341256&action=review
> 
> >> Source/WebKit/UIProcess/WebProcessPool.cpp:983
> >> +    process.didSendAllProcessInitializationMessages();
> > 
> > Could you use a ScopeExit for this to ensure this always runs last? I'm worried that maybe someone will add an early return in the future.
> 
> Why not just take the assertion inside WebProcessProxy::didFinishLaunching
> or some similar function in WebProcessProxy?
> 
> That would get you a tighter abstraction.
> 
> The background assertion can be an internal implementation detail of
> WebProcessProxy startup, and then we don't have to enforce order relative to
> commands issued by WebProcessPool.
> 
> The contract WebProcessProxy should expose to clients is "If you launch me,
> I will always finish launching before sleeping. You don't have to do
> anything special to make this happen, and you also can't stop it from
> happening."

Good point!

> >>> Source/WebKit/UIProcess/WebProcessProxy.cpp:1426
> >>> +    m_backgroundActivityTokenForInitialization = nullptr;
> >> 
> >> Need to check if we have to clear this if the process crashes in the meantime.
> > 
> > Yes, I believe we do need to.
> 
> You can just clear the token unconditionally in the crash handler. Probably
> doesn't make a big difference, since a live background assertion for a
> crashed process probably does nothing. But still good hygiene.
Comment 9 Tim Horton 2018-05-25 10:52:11 PDT
Comment on attachment 341256 [details]
Patch

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

>>>>> Source/WebKit/UIProcess/WebProcessPool.cpp:983
>>>>> +    process.didSendAllProcessInitializationMessages();
>>>> 
>>>> Could you use a ScopeExit for this to ensure this always runs last? I'm worried that maybe someone will add an early return in the future.
>>> 
>>> Why not just take the assertion inside WebProcessProxy::didFinishLaunching or some similar function in WebProcessProxy?
>>> 
>>> That would get you a tighter abstraction.
>>> 
>>> The background assertion can be an internal implementation detail of WebProcessProxy startup, and then we don't have to enforce order relative to commands issued by WebProcessPool.
>>> 
>>> The contract WebProcessProxy should expose to clients is "If you launch me, I will always finish launching before sleeping. You don't have to do anything special to make this happen, and you also can't stop it from happening."
>> 
>> Now there’s a good idea.
> 
> Good point!

Wait, no. I don’t think there *is* a place in WebProcessProxy that we know that other people (like WebProcessPool) are done sending initialization messages. This is all messy already (why does WebProcessPool::initializeNewWebProcess do so much work?!), but didFinishLaunching is waaaaay too early (as far as I can tell).
Comment 10 Geoffrey Garen 2018-05-25 11:31:48 PDT
> Wait, no. I don’t think there *is* a place in WebProcessProxy that we know
> that other people (like WebProcessPool) are done sending initialization
> messages.

I see.

OK, here's a potential alternative to put at the end of initializeNewWebProcess:

// Make sure the WebProcess finishes initialization before going to sleep.
auto token = throttler().backgroundActivityToken();
process.isResponsive([token] { }); // We don't really care what the answer is; this is just a convenient way to verify that our previous messages have been processed.
Comment 11 Geoffrey Garen 2018-05-25 11:39:00 PDT
> auto token = throttler().backgroundActivityToken();
> process.isResponsive([token] { }); // We don't really care what the answer
> is; this is just a convenient way to verify that our previous messages have
> been processed.

If we want to be extra pedantic, we can even take the assertion before the first message, to ensure that the web process starts receiving messages right away.
Comment 12 Tim Horton 2018-05-25 14:08:04 PDT
Created attachment 341325 [details]
Patch
Comment 13 Geoffrey Garen 2018-05-25 14:11:43 PDT
Comment on attachment 341325 [details]
Patch

r=me
Comment 14 WebKit Commit Bot 2018-05-25 14:53:27 PDT
Comment on attachment 341325 [details]
Patch

Clearing flags on attachment: 341325

Committed r232209: <https://trac.webkit.org/changeset/232209>
Comment 15 WebKit Commit Bot 2018-05-25 14:53:28 PDT
All reviewed patches have been landed.  Closing bug.