Regression(r191673): Crash in RunLoopTimer::schedule()
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Nov 2015 00:14:36 +0000 (00:14 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Nov 2015 00:14:36 +0000 (00:14 +0000)
https://bugs.webkit.org/show_bug.cgi?id=150816
<rdar://problem/23335285>

Reviewed by Anders Carlsson.

The crash was happening when the RunLoopTimer would fire during the
call to RunLoopTimer::schedule(), which can happen because we are
calling schedule() from a background thread. In such case, the
timerFired() callback execution would cause |this| to get destroyed.

To avoid this issue, DecodingResultDispatcher is now ref-counted. The
object is ref'd while calling startTimer() so that the object cannot go
away during the execution of this method. Also, we explicitly ref the
object when starting the timer to keep the object alive until the
RunLoopTimer has fired, at which point we explicitely de-ref.

This should handle correctly the cases where the RunLoopTimer fires
during AND after the execution of startTimer().

* platform/network/DataURLDecoder.cpp:
(WebCore::DataURLDecoder::DecodingResultDispatcher::dispatch):
(WebCore::DataURLDecoder::DecodingResultDispatcher::startTimer):
(WebCore::DataURLDecoder::DecodingResultDispatcher::timerFired):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@191921 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/network/DataURLDecoder.cpp

index f918bfa4e9a97e978ae35a23d7b727254c6d05e1..ea59b9e611be68197281fffae36c452b3aa0f3f4 100644 (file)
@@ -1,3 +1,30 @@
+2015-11-02  Chris Dumez  <cdumez@apple.com>
+
+        Regression(r191673): Crash in RunLoopTimer::schedule()
+        https://bugs.webkit.org/show_bug.cgi?id=150816
+        <rdar://problem/23335285>
+
+        Reviewed by Anders Carlsson.
+
+        The crash was happening when the RunLoopTimer would fire during the
+        call to RunLoopTimer::schedule(), which can happen because we are
+        calling schedule() from a background thread. In such case, the
+        timerFired() callback execution would cause |this| to get destroyed.
+
+        To avoid this issue, DecodingResultDispatcher is now ref-counted. The
+        object is ref'd while calling startTimer() so that the object cannot go
+        away during the execution of this method. Also, we explicitly ref the
+        object when starting the timer to keep the object alive until the
+        RunLoopTimer has fired, at which point we explicitely de-ref.
+
+        This should handle correctly the cases where the RunLoopTimer fires
+        during AND after the execution of startTimer().
+
+        * platform/network/DataURLDecoder.cpp:
+        (WebCore::DataURLDecoder::DecodingResultDispatcher::dispatch):
+        (WebCore::DataURLDecoder::DecodingResultDispatcher::startTimer):
+        (WebCore::DataURLDecoder::DecodingResultDispatcher::timerFired):
+
 2015-11-02  Andy Estes  <aestes@apple.com>
 
         [Cocoa] Add tvOS and watchOS to SUPPORTED_PLATFORMS
index 1542efc5b6540d551f7b6cea2393a768d66746bf..bbef9e2624a8a915c0dce96743b8288799df60ae 100644 (file)
@@ -56,12 +56,12 @@ struct DecodeTask {
 
 #if HAVE(RUNLOOP_TIMER)
 
-class DecodingResultDispatcher {
+class DecodingResultDispatcher : public ThreadSafeRefCounted<DecodingResultDispatcher> {
 public:
     static void dispatch(std::unique_ptr<DecodeTask> decodeTask)
     {
-        auto& dispatcher = *new DecodingResultDispatcher(WTF::move(decodeTask));
-        dispatcher.startTimer();
+        Ref<DecodingResultDispatcher> dispatcher = adoptRef(*new DecodingResultDispatcher(WTF::move(decodeTask)));
+        dispatcher->startTimer();
     }
 
 private:
@@ -73,6 +73,8 @@ private:
 
     void startTimer()
     {
+        // Keep alive until the timer has fired.
+        ref();
         m_timer.startOneShot(0);
         m_timer.schedule(m_decodeTask->scheduleContext.scheduledPairs);
     }
@@ -84,7 +86,7 @@ private:
         else
             m_decodeTask->completionHandler({ });
 
-        delete this;
+        deref();
     }
 
     RunLoopTimer<DecodingResultDispatcher> m_timer;