Bug 224305 - Create WebIDBServer only when it is needed
Summary: Create WebIDBServer only when it is needed
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: 226547
Blocks: 225089
  Show dependency treegraph
 
Reported: 2021-04-07 14:42 PDT by Sihui Liu
Modified: 2021-06-14 20:14 PDT (History)
4 users (show)

See Also:


Attachments
Patch (16.74 KB, patch)
2021-04-07 15:02 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (17.16 KB, patch)
2021-04-07 20:53 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (16.94 KB, patch)
2021-04-08 10:58 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (17.37 KB, patch)
2021-04-08 14:21 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (17.40 KB, patch)
2021-04-08 16:01 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch for landing (17.57 KB, patch)
2021-04-09 14:07 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch for landing (17.66 KB, patch)
2021-04-09 14:21 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch for landing (17.63 KB, patch)
2021-04-09 14:22 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
[fast-cq] Patch (1.95 KB, patch)
2021-04-09 16:52 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
[fast-cq] Patch (1.84 KB, patch)
2021-04-10 01:30 PDT, Sihui Liu
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
[fast-cq] Patch (1.82 KB, patch)
2021-04-10 01:37 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch for landing (14.65 KB, patch)
2021-04-10 09:23 PDT, Sihui Liu
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch for landing (15.15 KB, patch)
2021-04-10 09:48 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (18.67 KB, patch)
2021-04-12 08:28 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 14:42:49 PDT
...
Comment 1 Sihui Liu 2021-04-07 14:43:19 PDT
rdar://71962196
Comment 2 Sihui Liu 2021-04-07 15:02:23 PDT
Created attachment 425445 [details]
Patch
Comment 3 Sihui Liu 2021-04-07 20:53:15 PDT
Created attachment 425475 [details]
Patch
Comment 4 Alex Christensen 2021-04-08 09:23:08 PDT
Comment on attachment 425475 [details]
Patch

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

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:70
> +        postTaskReply(CrossThreadTask([this, callback = WTFMove(callback), origins = crossThreadCopy(m_server->getOrigins())]() mutable {

Don't we need either a protected or a weak this here?

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:88
> +        postTaskReply(CrossThreadTask([this, callback = WTFMove(callback)]() mutable {

ditto

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:106
> +        postTaskReply(CrossThreadTask([this, callback = WTFMove(callback)]() mutable {

ditto
Comment 5 Sihui Liu 2021-04-08 10:58:05 PDT
Created attachment 425521 [details]
Patch
Comment 6 Sihui Liu 2021-04-08 11:01:40 PDT
(In reply to Alex Christensen from comment #4)
> Comment on attachment 425475 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=425475&action=review
> 
> > Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:70
> > +        postTaskReply(CrossThreadTask([this, callback = WTFMove(callback), origins = crossThreadCopy(m_server->getOrigins())]() mutable {
> 
> Don't we need either a protected or a weak this here?
> 

Updated the patch.

By the way do you think revealing sessionID in thread name is a bad idea?
Comment 7 Darin Adler 2021-04-08 11:07:05 PDT
Comment on attachment 425521 [details]
Patch

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

This looks good. I have some comments. I didn’t set review+ because I am not sure I’m enough of an expert on this area to spot mistakes, but I think it looks right.

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.h:97
> +    enum class DataTaskCountUpdateType : uint8_t { Decrease, Increase };
> +    void updateDataTaskCount(DataTaskCountUpdateType);

This is a peculiar interface. I suggest two separate named functions rather than a function with an enum class argument.

Also, to try to make this harder to get wrong, I suggest we make an object that increments on creation and decrements on destruction. We can capture that object in lambdas, instead of capturing "protectedThis" and writing an explicit decrement function.

> Source/WebKit/NetworkProcess/NetworkProcess.cpp:2675
> +    if (auto* webIDBServer = m_webIDBServers.get(sessionID))
> +        webIDBServer->removeConnection(connection);

In a tight context like this, I really like single word variable names that don’t try to disambiguate or be precise. Like:

    if (auto server = m_webIDBServers.get(sessionID))
        server->removeConnection(connection);

Sure, the local variable can be named webIDBServer or even webIDBServerForSession, but it doesn’t need to be. Single words work really well to make such code clear and readable.
Comment 8 Sihui Liu 2021-04-08 14:21:42 PDT
Created attachment 425542 [details]
Patch
Comment 9 Sihui Liu 2021-04-08 14:23:02 PDT
(In reply to Darin Adler from comment #7)
> Comment on attachment 425521 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=425521&action=review
> 
> This looks good. I have some comments. I didn’t set review+ because I am not
> sure I’m enough of an expert on this area to spot mistakes, but I think it
> looks right.
> 
> > Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.h:97
> > +    enum class DataTaskCountUpdateType : uint8_t { Decrease, Increase };
> > +    void updateDataTaskCount(DataTaskCountUpdateType);
> 
> This is a peculiar interface. I suggest two separate named functions rather
> than a function with an enum class argument.
> 
> Also, to try to make this harder to get wrong, I suggest we make an object
> that increments on creation and decrements on destruction. We can capture
> that object in lambdas, instead of capturing "protectedThis" and writing an
> explicit decrement function.
> 

Thanks for the comments. I've replaced it with a RefCounter.

> > Source/WebKit/NetworkProcess/NetworkProcess.cpp:2675
> > +    if (auto* webIDBServer = m_webIDBServers.get(sessionID))
> > +        webIDBServer->removeConnection(connection);
> 
> In a tight context like this, I really like single word variable names that
> don’t try to disambiguate or be precise. Like:
> 
>     if (auto server = m_webIDBServers.get(sessionID))
>         server->removeConnection(connection);
> 
> Sure, the local variable can be named webIDBServer or even
> webIDBServerForSession, but it doesn’t need to be. Single words work really
> well to make such code clear and readable.

Updated.
Comment 10 Darin Adler 2021-04-08 15:30:46 PDT
Comment on attachment 425542 [details]
Patch

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

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:44
> +    , m_dataTaskCounter([protectedThis = makeRef(*this)](RefCounterEvent event) { protectedThis->tryClose(); })

This creates a reference cycle since the WebIDBServer owns its m_dataTaskCounter, which in turn owns this lambda, which in turn references the WebIDBServer, so I think the WebIDBServer will leak.
Comment 11 Sihui Liu 2021-04-08 16:01:37 PDT
Created attachment 425555 [details]
Patch
Comment 12 Alex Christensen 2021-04-09 10:55:20 PDT
Comment on attachment 425555 [details]
Patch

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

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:435
> +    m_closeCallback();

It would probably be worth null checking m_closeCallback to make this more robust in case some tries to call tryClose twice in the future.

> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.h:107
> +    typedef RefCounter<DataTaskCounterType> DataTaskCounter;

"using DataTaskCounter = RefCounter<DataTaskCounterType>" is the cool new way to do this.  Same with DataTaskCounterToken

> Source/WebKit/NetworkProcess/NetworkProcess.cpp:2330
>          RefPtr<StorageQuotaManager> storageQuotaManager = weakThis ? this->storageQuotaManager(sessionID, origin) : nullptr;

If you used weakThis-> instead of this-> then you wouldn't need to capture this.  That would be better because it wouldn't allow you to accidentally use an unchecked this pointer (which is easy to do).
Comment 13 Alex Christensen 2021-04-09 10:56:11 PDT
Comment on attachment 425555 [details]
Patch

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

> Source/WebKit/WebProcess/Databases/IndexedDB/WebIDBConnectionToServer.cpp:62
> +    send(Messages::NetworkConnectionToWebProcess::AddIDBConnection());

Should there be a corresponding remove idb connection message in the destructor?
Comment 14 Darin Adler 2021-04-09 11:32:43 PDT
Comment on attachment 425555 [details]
Patch

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

>> Source/WebKit/NetworkProcess/NetworkProcess.cpp:2330
>>          RefPtr<StorageQuotaManager> storageQuotaManager = weakThis ? this->storageQuotaManager(sessionID, origin) : nullptr;
> 
> If you used weakThis-> instead of this-> then you wouldn't need to capture this.  That would be better because it wouldn't allow you to accidentally use an unchecked this pointer (which is easy to do).

This is excellent advice.

Also, when something is ThreadSafeRefCounted, we typically need to ref it before dereferencing it, not just dereference a weak pointer. There is likely a reason that’s not needed here, but *generally* the idiom would be to put the weak pointer into a RefPtr first.
Comment 15 Sihui Liu 2021-04-09 14:07:50 PDT
Created attachment 425650 [details]
Patch for landing
Comment 16 Sihui Liu 2021-04-09 14:21:01 PDT
Created attachment 425652 [details]
Patch for landing
Comment 17 Sihui Liu 2021-04-09 14:22:27 PDT
Created attachment 425653 [details]
Patch for landing
Comment 18 Sihui Liu 2021-04-09 14:29:19 PDT
Comment on attachment 425555 [details]
Patch

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

>> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.cpp:435
>> +    m_closeCallback();
> 
> It would probably be worth null checking m_closeCallback to make this more robust in case some tries to call tryClose twice in the future.

Added.

>> Source/WebKit/NetworkProcess/IndexedDB/WebIDBServer.h:107
>> +    typedef RefCounter<DataTaskCounterType> DataTaskCounter;
> 
> "using DataTaskCounter = RefCounter<DataTaskCounterType>" is the cool new way to do this.  Same with DataTaskCounterToken

Cool.

>>> Source/WebKit/NetworkProcess/NetworkProcess.cpp:2330
>>>          RefPtr<StorageQuotaManager> storageQuotaManager = weakThis ? this->storageQuotaManager(sessionID, origin) : nullptr;
>> 
>> If you used weakThis-> instead of this-> then you wouldn't need to capture this.  That would be better because it wouldn't allow you to accidentally use an unchecked this pointer (which is easy to do).
> 
> This is excellent advice.
> 
> Also, when something is ThreadSafeRefCounted, we typically need to ref it before dereferencing it, not just dereference a weak pointer. There is likely a reason that’s not needed here, but *generally* the idiom would be to put the weak pointer into a RefPtr first.

ah, I think we do need to ref it here. Updated in the patch.

>> Source/WebKit/WebProcess/Databases/IndexedDB/WebIDBConnectionToServer.cpp:62
>> +    send(Messages::NetworkConnectionToWebProcess::AddIDBConnection());
> 
> Should there be a corresponding remove idb connection message in the destructor?

NetworkProcess::connectionToWebProcessClosed should already cover this case.
Comment 19 EWS 2021-04-09 15:05:06 PDT
Committed r275779 (236352@main): <https://commits.webkit.org/236352@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 425653 [details].
Comment 20 Sihui Liu 2021-04-09 16:52:37 PDT Comment hidden (obsolete)
Comment 21 Sihui Liu 2021-04-09 16:52:38 PDT Comment hidden (obsolete)
Comment 22 EWS 2021-04-09 16:54:24 PDT Comment hidden (obsolete)
Comment 23 Sihui Liu 2021-04-10 01:30:01 PDT Comment hidden (obsolete)
Comment 24 Sihui Liu 2021-04-10 01:30:02 PDT Comment hidden (obsolete)
Comment 25 EWS 2021-04-10 01:33:21 PDT Comment hidden (obsolete)
Comment 26 Sihui Liu 2021-04-10 01:37:52 PDT Comment hidden (obsolete)
Comment 27 EWS 2021-04-10 01:39:20 PDT Comment hidden (obsolete)
Comment 28 Sihui Liu 2021-04-10 09:23:22 PDT Comment hidden (obsolete)
Comment 29 Sihui Liu 2021-04-10 09:23:24 PDT Comment hidden (obsolete)
Comment 30 Sihui Liu 2021-04-10 09:48:14 PDT Comment hidden (obsolete)
Comment 31 EWS 2021-04-10 10:20:24 PDT Comment hidden (obsolete)
Comment 32 EWS 2021-04-10 10:57:01 PDT
Committed r275799 (236370@main): <https://commits.webkit.org/236370@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 425686 [details].
Comment 33 Sihui Liu 2021-04-10 18:51:15 PDT
Reopened as the patch is reverted in r275799
Comment 34 Sihui Liu 2021-04-12 08:28:41 PDT
Created attachment 425748 [details]
Patch
Comment 35 EWS 2021-04-12 16:43:57 PDT
Committed r275846 (236411@main): <https://commits.webkit.org/236411@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 425748 [details].
Comment 36 Fujii Hironori 2021-04-26 23:07:34 PDT
Filed: Bug 225089 – HashTableConstIterator's consistency assertion fails while closing m_webIDBServers in NetworkProcess::didClose since r275846