Bug 224299 - Make WebIDBServer not block main thread during initialization
Summary: Make WebIDBServer not block main thread during initialization
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Sihui Liu
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-04-07 12:21 PDT by Sihui Liu
Modified: 2021-04-13 09:55 PDT (History)
6 users (show)

See Also:


Attachments
Patch (36.49 KB, patch)
2021-04-07 13:52 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (36.65 KB, patch)
2021-04-07 17:55 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch for landing (37.29 KB, patch)
2021-04-13 09:26 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sihui Liu 2021-04-07 12:21:39 PDT
...
Comment 1 Sihui Liu 2021-04-07 13:52:22 PDT
Created attachment 425434 [details]
Patch
Comment 2 Geoffrey Garen 2021-04-07 16:24:17 PDT
Comment on attachment 425434 [details]
Patch

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

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:55
>  WebIDBServer::~WebIDBServer()

Does WebIDBServer do something to ensure that its WebCore::IDBServer::IDBServer thread has fully exited before we run this destructor? If not, WebCore::IDBServer::IDBServer may use-after-free m_serverLock.

I see code in close() that will post a task to destroy the WebCore::IDBServer::IDBServer. But I don't see us waiting on that task. And I don't know if we guarantee a close() before destruction. (If so, we should probably RELEASE_ASSERT that here.)

> Source/WebKitLegacy/Storage/InProcessIDBServer.h:135
> +    Lock m_serverLock;

This is maybe not correct, or at least it's a little scary -- because we passed a reference to m_serverLock to m_server, and this C++ declaration order means that m_serverLock's destructor will run before m_server's. So, for at least a time, m_server will hold a reference to a destructed m_serverLock.

Let's change the declaration order so that m_serverLock outlives m_server.
Comment 3 Sihui Liu 2021-04-07 17:22:37 PDT
Comment on attachment 425434 [details]
Patch

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

>> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:55
>>  WebIDBServer::~WebIDBServer()
> 
> Does WebIDBServer do something to ensure that its WebCore::IDBServer::IDBServer thread has fully exited before we run this destructor? If not, WebCore::IDBServer::IDBServer may use-after-free m_serverLock.
> 
> I see code in close() that will post a task to destroy the WebCore::IDBServer::IDBServer. But I don't see us waiting on that task. And I don't know if we guarantee a close() before destruction. (If so, we should probably RELEASE_ASSERT that here.)

Yes, in close() it sets the callback of CrossThreadHandler, and the callback is the last thing to run before thread exits. 
The callback dispatches a function that should hold the last reference of WebIDBServer to main runloop, so WebIDBServer goes away after thread.

(I think we can simplify close() a bit. Will do a followup.)

>> Source/WebKitLegacy/Storage/InProcessIDBServer.h:135
>> +    Lock m_serverLock;
> 
> This is maybe not correct, or at least it's a little scary -- because we passed a reference to m_serverLock to m_server, and this C++ declaration order means that m_serverLock's destructor will run before m_server's. So, for at least a time, m_server will hold a reference to a destructed m_serverLock.
> 
> Let's change the declaration order so that m_serverLock outlives m_server.

Sure.
Comment 4 Sihui Liu 2021-04-07 17:55:59 PDT
Created attachment 425466 [details]
Patch
Comment 5 Geoffrey Garen 2021-04-08 10:03:46 PDT
Comment on attachment 425466 [details]
Patch

r=me

Would be nice to RELEASE_ASSERT(!m_server) in the WebIDBServer destructor, to prove that we have indeed called close().

Similary, would be nice to move the call to setCompletionCallback() into the WebIDBServer server constructor. That way, the lifetime is more obvious, and we always get object lifetime right even if we forget to call close().
Comment 6 Sihui Liu 2021-04-13 09:26:31 PDT
Created attachment 425877 [details]
Patch for landing
Comment 7 EWS 2021-04-13 09:53:54 PDT
Committed r275891 (236456@main): <https://commits.webkit.org/236456@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 425877 [details].
Comment 8 Radar WebKit Bug Importer 2021-04-13 09:55:55 PDT
<rdar://problem/76596644>