Title: [219845] trunk/Source/WebKit
Revision
219845
Author
[email protected]
Date
2017-07-24 15:50:32 -0700 (Mon, 24 Jul 2017)

Log Message

Fix lifetime management issue in ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore()
https://bugs.webkit.org/show_bug.cgi?id=174790

Reviewed by Brady Eidson.

Fix lifetime management issue in ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore().
WorkQueue::dispatchAfter() keeps the WorkQueue alive because its implementation keeps a strong ref to
|this|. As a result, the lambda passed to dispatchAfter(), which calls writeMemoryStoreToDisk(), can
get executed after the store is gone.

To address the issue, we now use a RunLoop::Timer to schedule the write, instead of a
WorkQueue::dispatchAfter() call. This way, we are guaranteed that the callback will not get called
after the store has been destroyed.

* UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:
(WebKit::ResourceLoadStatisticsPersistentStorage::ResourceLoadStatisticsPersistentStorage):
(WebKit::ResourceLoadStatisticsPersistentStorage::asyncWriteTimerFired):
(WebKit::ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore):
(WebKit::ResourceLoadStatisticsPersistentStorage::finishAllPendingWorkSynchronously):
(WebKit::ResourceLoadStatisticsPersistentStorage::ref):
(WebKit::ResourceLoadStatisticsPersistentStorage::deref):
* UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h:

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (219844 => 219845)


--- trunk/Source/WebKit/ChangeLog	2017-07-24 22:42:00 UTC (rev 219844)
+++ trunk/Source/WebKit/ChangeLog	2017-07-24 22:50:32 UTC (rev 219845)
@@ -1,5 +1,30 @@
 2017-07-24  Chris Dumez  <[email protected]>
 
+        Fix lifetime management issue in ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore()
+        https://bugs.webkit.org/show_bug.cgi?id=174790
+
+        Reviewed by Brady Eidson.
+
+        Fix lifetime management issue in ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore().
+        WorkQueue::dispatchAfter() keeps the WorkQueue alive because its implementation keeps a strong ref to
+        |this|. As a result, the lambda passed to dispatchAfter(), which calls writeMemoryStoreToDisk(), can
+        get executed after the store is gone.
+
+        To address the issue, we now use a RunLoop::Timer to schedule the write, instead of a
+        WorkQueue::dispatchAfter() call. This way, we are guaranteed that the callback will not get called
+        after the store has been destroyed.
+
+        * UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:
+        (WebKit::ResourceLoadStatisticsPersistentStorage::ResourceLoadStatisticsPersistentStorage):
+        (WebKit::ResourceLoadStatisticsPersistentStorage::asyncWriteTimerFired):
+        (WebKit::ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore):
+        (WebKit::ResourceLoadStatisticsPersistentStorage::finishAllPendingWorkSynchronously):
+        (WebKit::ResourceLoadStatisticsPersistentStorage::ref):
+        (WebKit::ResourceLoadStatisticsPersistentStorage::deref):
+        * UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h:
+
+2017-07-24  Chris Dumez  <[email protected]>
+
         Unreviewed, rolling out r219828.
 
         Causes debug assertions to be hit on iOS

Modified: trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp (219844 => 219845)


--- trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp	2017-07-24 22:42:00 UTC (rev 219844)
+++ trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp	2017-07-24 22:50:32 UTC (rev 219845)
@@ -85,6 +85,7 @@
 ResourceLoadStatisticsPersistentStorage::ResourceLoadStatisticsPersistentStorage(WebResourceLoadStatisticsStore& store, const String& storageDirectoryPath)
     : m_memoryStore(store)
     , m_storageDirectoryPath(storageDirectoryPath)
+    , m_asyncWriteTimer(RunLoop::main(), this, &ResourceLoadStatisticsPersistentStorage::asyncWriteTimerFired)
 {
 }
 
@@ -237,6 +238,14 @@
         m_memoryStore.grandfatherExistingWebsiteData();
 }
 
+void ResourceLoadStatisticsPersistentStorage::asyncWriteTimerFired()
+{
+    ASSERT(RunLoop::isMain());
+    m_memoryStore.statisticsQueue().dispatch([this] () mutable {
+        writeMemoryStoreToDisk();
+    });
+}
+
 void ResourceLoadStatisticsPersistentStorage::writeMemoryStoreToDisk()
 {
     ASSERT(!RunLoop::isMain());
@@ -280,8 +289,8 @@
         if (!m_hasPendingWrite) {
             m_hasPendingWrite = true;
             Seconds delay = minimumWriteInterval - timeSinceLastWrite + 1_s;
-            m_memoryStore.statisticsQueue().dispatchAfter(delay, [this] () mutable {
-                writeMemoryStoreToDisk();
+            RunLoop::main().dispatch([this, protectedThis = makeRef(*this), delay] {
+                m_asyncWriteTimer.startOneShot(delay);
             });
         }
         return;
@@ -305,6 +314,8 @@
 
 void ResourceLoadStatisticsPersistentStorage::finishAllPendingWorkSynchronously()
 {
+    m_asyncWriteTimer.stop();
+
     BinarySemaphore semaphore;
     // Make sure any pending work in our queue is finished before we terminate.
     m_memoryStore.statisticsQueue().dispatch([&semaphore, this] {
@@ -316,6 +327,16 @@
     semaphore.wait(WallTime::infinity());
 }
 
+void ResourceLoadStatisticsPersistentStorage::ref()
+{
+    m_memoryStore.ref();
+}
+
+void ResourceLoadStatisticsPersistentStorage::deref()
+{
+    m_memoryStore.deref();
+}
+
 #if !PLATFORM(IOS)
 void ResourceLoadStatisticsPersistentStorage::excludeFromBackup() const
 {

Modified: trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h (219844 => 219845)


--- trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h	2017-07-24 22:42:00 UTC (rev 219844)
+++ trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h	2017-07-24 22:50:32 UTC (rev 219845)
@@ -27,6 +27,7 @@
 
 #include <wtf/Forward.h>
 #include <wtf/MonotonicTime.h>
+#include <wtf/RunLoop.h>
 #include <wtf/WallTime.h>
 #include <wtf/text/WTFString.h>
 
@@ -49,6 +50,9 @@
 
     void finishAllPendingWorkSynchronously();
 
+    void ref();
+    void deref();
+
 private:
     String storageDirectoryPath() const;
     String resourceLogFilePath() const;
@@ -61,9 +65,11 @@
     void populateMemoryStoreFromDisk();
     void excludeFromBackup() const;
     void refreshMemoryStoreFromDisk();
+    void asyncWriteTimerFired();
 
     WebResourceLoadStatisticsStore& m_memoryStore;
     const String m_storageDirectoryPath;
+    RunLoop::Timer<ResourceLoadStatisticsPersistentStorage> m_asyncWriteTimer;
     std::unique_ptr<WebCore::FileMonitor> m_fileMonitor;
     WallTime m_lastStatisticsFileSyncTime;
     MonotonicTime m_lastStatisticsWriteTime;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to