Bug 174521 - Monitor directory for new statistics files after a delete operation
Summary: Monitor directory for new statistics files after a delete operation
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Brent Fulgham
URL:
Keywords: InRadar
: 174466 (view as bug list)
Depends on:
Blocks:
 
Reported: 2017-07-14 12:22 PDT by Brent Fulgham
Modified: 2017-07-14 15:53 PDT (History)
3 users (show)

See Also:


Attachments
Patch (3.54 KB, patch)
2017-07-14 13:07 PDT, Brent Fulgham
no flags Details | Formatted Diff | Diff
Patch (5.46 KB, patch)
2017-07-14 15:07 PDT, Brent Fulgham
cdumez: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brent Fulgham 2017-07-14 12:22:42 PDT
If the ResourceLoadStatistics data file is deleted on disk, we should monitor the data directory to see if a new ones is created by a separate process. If so, we should sync with that data and begin watching the new file for changes.

This replaces the buggy logic I originally wrote that would attempt to sync when we wanted to write to disk.
Comment 1 Radar WebKit Bug Importer 2017-07-14 12:23:50 PDT
<rdar://problem/33322189>
Comment 2 Brent Fulgham 2017-07-14 13:07:27 PDT
Created attachment 315478 [details]
Patch
Comment 3 Chris Dumez 2017-07-14 13:24:54 PDT
Comment on attachment 315478 [details]
Patch

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

> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:137
> +            monitorDirectoryForNewStatistics();

This is insufficient. You have the same issue when populateMemoryStoreFromDisk() is called and the file does not exist yet.

> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:145
> +    String storagePath = this->storageDirectoryPath();

Don't need the the this->

> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:150
> +        if (m_fileMonitor || type == FileMonitor::FileChangeType::Removal) {

m_fileMonitor being non-null should not be possible?

What does removal mean here. Is this called when the directory gets removed. What if a file was removed in the folder?

> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:163
> +        refreshMemoryStoreFromDisk();

Do we want refreshMemoryStoreFromDisk() or populateMemoryStoreFromDisk() here?

> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h:68
> +    std::unique_ptr<WebCore::FileMonitor> m_directoryMonitor;

Do we really need a second FileMonitor member, it seems to be m_fileMonitor and m_directoryMonitor are mutually exclusive and we could thus reuse the same member.
Comment 4 Chris Dumez 2017-07-14 13:29:41 PDT
Comment on attachment 315478 [details]
Patch

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

> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:148
> +    m_directoryMonitor = std::make_unique<FileMonitor>(storagePath, m_memoryStore.statisticsQueue(), [this] (FileMonitor::FileChangeType type) {

What if the folder does not exist yet. You only call makeAllDirectories() when actually writing data to disk at the moment. I think we should probably call makeAllDirectories() here before we start monitoring as well.

>> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:150
>> +        if (m_fileMonitor || type == FileMonitor::FileChangeType::Removal) {
> 
> m_fileMonitor being non-null should not be possible?
> 
> What does removal mean here. Is this called when the directory gets removed. What if a file was removed in the folder?

I looked it up and it looks like we get a DISPATCH_VNODE_WRITE for file additions and removals from the folder. DISPATCH_VNODE_DELETE must mean that the folder has been removed then, in which case I case it is ok to stop monitoring.
Comment 5 Brent Fulgham 2017-07-14 14:36:46 PDT
Comment on attachment 315478 [details]
Patch

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

>> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:137
>> +            monitorDirectoryForNewStatistics();
> 
> This is insufficient. You have the same issue when populateMemoryStoreFromDisk() is called and the file does not exist yet.

Oh, good point! I'll add the call there, too.

>> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:145
>> +    String storagePath = this->storageDirectoryPath();
> 
> Don't need the the this->

Right -- I'll remove.

>> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:148
>> +    m_directoryMonitor = std::make_unique<FileMonitor>(storagePath, m_memoryStore.statisticsQueue(), [this] (FileMonitor::FileChangeType type) {
> 
> What if the folder does not exist yet. You only call makeAllDirectories() when actually writing data to disk at the moment. I think we should probably call makeAllDirectories() here before we start monitoring as well.

Good idea. I'll fix that.

>>> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:150
>>> +        if (m_fileMonitor || type == FileMonitor::FileChangeType::Removal) {
>> 
>> m_fileMonitor being non-null should not be possible?
>> 
>> What does removal mean here. Is this called when the directory gets removed. What if a file was removed in the folder?
> 
> I looked it up and it looks like we get a DISPATCH_VNODE_WRITE for file additions and removals from the folder. DISPATCH_VNODE_DELETE must mean that the folder has been removed then, in which case I case it is ok to stop monitoring.

Yes -- that's right.

I was worried about a case where we are monitoring for an external process, and our own process decides to write a file. Since VNODE events are not filtered by process, we might see our own file create and believe we should take some action. So, if the monitor sees that we are already monitoring a file, it should just exit.

>> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:163
>> +        refreshMemoryStoreFromDisk();
> 
> Do we want refreshMemoryStoreFromDisk() or populateMemoryStoreFromDisk() here?

I think we want to refresh, since the user might have done some browsing since the file was deleted, but before the current process writes its own content. So we don't want to lose observations made in the running process just because an external process wrote some data.

>> Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h:68
>> +    std::unique_ptr<WebCore::FileMonitor> m_directoryMonitor;
> 
> Do we really need a second FileMonitor member, it seems to be m_fileMonitor and m_directoryMonitor are mutually exclusive and we could thus reuse the same member.

Yes, I was thinking that might be the case as well. I'll try that approach.
Comment 6 Brent Fulgham 2017-07-14 15:07:43 PDT
Created attachment 315495 [details]
Patch
Comment 7 Chris Dumez 2017-07-14 15:14:01 PDT
Comment on attachment 315495 [details]
Patch

r=me if the bots are happy.
Comment 8 Brent Fulgham 2017-07-14 15:52:54 PDT
Committed r219530: <http://trac.webkit.org/changeset/219530>
Comment 9 Brent Fulgham 2017-07-14 15:53:56 PDT
*** Bug 174466 has been marked as a duplicate of this bug. ***