Title: [219828] trunk/Source/WebKit
Revision
219828
Author
[email protected]
Date
2017-07-24 10:43:14 -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.

* UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:
(WebKit::ResourceLoadStatisticsPersistentStorage::ResourceLoadStatisticsPersistentStorage):
(WebKit::ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore):
* UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h:
(WebKit::ResourceLoadStatisticsPersistentStorage::createWeakPtr):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (219827 => 219828)


--- trunk/Source/WebKit/ChangeLog	2017-07-24 17:31:12 UTC (rev 219827)
+++ trunk/Source/WebKit/ChangeLog	2017-07-24 17:43:14 UTC (rev 219828)
@@ -1,3 +1,21 @@
+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.
+
+        * UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp:
+        (WebKit::ResourceLoadStatisticsPersistentStorage::ResourceLoadStatisticsPersistentStorage):
+        (WebKit::ResourceLoadStatisticsPersistentStorage::scheduleOrWriteMemoryStore):
+        * UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h:
+        (WebKit::ResourceLoadStatisticsPersistentStorage::createWeakPtr):
+
 2017-07-23  Carlos Garcia Campos  <[email protected]>
 
         Unreviewed. REGRESSION(r219713): [GTK][WPE] Fix default favicon database patch.

Modified: trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp (219827 => 219828)


--- trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp	2017-07-24 17:31:12 UTC (rev 219827)
+++ trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.cpp	2017-07-24 17:43:14 UTC (rev 219828)
@@ -83,7 +83,8 @@
 }
 
 ResourceLoadStatisticsPersistentStorage::ResourceLoadStatisticsPersistentStorage(WebResourceLoadStatisticsStore& store, const String& storageDirectoryPath)
-    : m_memoryStore(store)
+    : m_weakPtrFactory(this)
+    , m_memoryStore(store)
     , m_storageDirectoryPath(storageDirectoryPath)
 {
 }
@@ -280,8 +281,10 @@
         if (!m_hasPendingWrite) {
             m_hasPendingWrite = true;
             Seconds delay = minimumWriteInterval - timeSinceLastWrite + 1_s;
-            m_memoryStore.statisticsQueue().dispatchAfter(delay, [this] () mutable {
-                writeMemoryStoreToDisk();
+            // WorkQueue::dispatchAfter() keeps the WorkQueue alive so the dispatched lambda may get executed after the store has been destroyed.
+            m_memoryStore.statisticsQueue().dispatchAfter(delay, [weakThis = createWeakPtr()] () mutable {
+                if (weakThis)
+                    weakThis->writeMemoryStoreToDisk();
             });
         }
         return;

Modified: trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h (219827 => 219828)


--- trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h	2017-07-24 17:31:12 UTC (rev 219827)
+++ trunk/Source/WebKit/UIProcess/Storage/ResourceLoadStatisticsPersistentStorage.h	2017-07-24 17:43:14 UTC (rev 219828)
@@ -28,6 +28,7 @@
 #include <wtf/Forward.h>
 #include <wtf/MonotonicTime.h>
 #include <wtf/WallTime.h>
+#include <wtf/WeakPtr.h>
 #include <wtf/text/WTFString.h>
 
 namespace WebCore {
@@ -62,6 +63,9 @@
     void excludeFromBackup() const;
     void refreshMemoryStoreFromDisk();
 
+    WeakPtr<ResourceLoadStatisticsPersistentStorage> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
+
+    WeakPtrFactory<ResourceLoadStatisticsPersistentStorage> m_weakPtrFactory;
     WebResourceLoadStatisticsStore& m_memoryStore;
     const String m_storageDirectoryPath;
     std::unique_ptr<WebCore::FileMonitor> m_fileMonitor;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to