Title: [246685] trunk
Revision
246685
Author
[email protected]
Date
2019-06-21 11:01:13 -0700 (Fri, 21 Jun 2019)

Log Message

Safari crashes after ~2028 OfflineAudioContext objects are created (they never get garbage collected, consuming a thread each)
https://bugs.webkit.org/show_bug.cgi?id=198964
<rdar://problem/51891520>

Reviewed by Jer Noble.

Source/WebCore:

Move from setPendingActivity/unsetPendingActivity to an
m_pendingActivity member which is easier to manage.

Keep setting a pending activity for AudioContext at construction time
but do not do that for Offline contexts.
Instead, set the pending activity when startRendering is called.
Unset the pending activity when the rendering activity is finished.

Make m_audioDecoder a unique pointer so that it can lazily be initialized.
This removes the burden of creating an audio decoder thread for each context.

Test: webaudio/offlineaudiocontext-gc.html

* Modules/webaudio/AudioContext.cpp:
(WebCore::AudioContext::AudioContext):
(WebCore::AudioContext::constructCommon):
(WebCore::AudioContext::clear):
(WebCore::AudioContext::decodeAudioData):
(WebCore::AudioContext::startRendering):
(WebCore::AudioContext::finishedRendering):
(WebCore::AudioContext::dispatchEvent):
(WebCore::AudioContext::clearPendingActivity):
(WebCore::AudioContext::makePendingActivity):
To keep it consistent with setPendingActivity/unsetPendingActivity, we
explicitly ref/unref the AudioContext. We should try to remove this ref/unref.
* Modules/webaudio/AudioContext.h:
* Modules/webaudio/OfflineAudioDestinationNode.cpp:
(WebCore::OfflineAudioDestinationNode::startRendering):

LayoutTests:

* webaudio/offlineaudiocontext-gc-expected.txt: Added.
* webaudio/offlineaudiocontext-gc.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (246684 => 246685)


--- trunk/LayoutTests/ChangeLog	2019-06-21 17:53:33 UTC (rev 246684)
+++ trunk/LayoutTests/ChangeLog	2019-06-21 18:01:13 UTC (rev 246685)
@@ -1,3 +1,14 @@
+2019-06-21  Youenn Fablet  <[email protected]>
+
+        Safari crashes after ~2028 OfflineAudioContext objects are created (they never get garbage collected, consuming a thread each)
+        https://bugs.webkit.org/show_bug.cgi?id=198964
+        <rdar://problem/51891520>
+
+        Reviewed by Jer Noble.
+
+        * webaudio/offlineaudiocontext-gc-expected.txt: Added.
+        * webaudio/offlineaudiocontext-gc.html: Added.
+
 2019-06-21  Truitt Savell  <[email protected]>
 
         Unreviewed, rolling out r246655.

Added: trunk/LayoutTests/webaudio/offlineaudiocontext-gc-expected.txt (0 => 246685)


--- trunk/LayoutTests/webaudio/offlineaudiocontext-gc-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/webaudio/offlineaudiocontext-gc-expected.txt	2019-06-21 18:01:13 UTC (rev 246685)
@@ -0,0 +1,4 @@
+
+PASS GC a newly created offline audio context 
+PASS GC a rendering offline audio context 
+

Added: trunk/LayoutTests/webaudio/offlineaudiocontext-gc.html (0 => 246685)


--- trunk/LayoutTests/webaudio/offlineaudiocontext-gc.html	                        (rev 0)
+++ trunk/LayoutTests/webaudio/offlineaudiocontext-gc.html	2019-06-21 18:01:13 UTC (rev 246685)
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<script src=""
+<script src=""
+<script src=""
+<script>
+
+function createContext() {
+    let context = new webkitOfflineAudioContext(2, 1000, 44100);
+    if (window.internals)
+        return internals.observeGC(context);
+}
+
+function createRenderingContext(completeCallback) {
+    let context = new webkitOfflineAudioContext(2, 1000, 44100);
+    let node = context.createBufferSource();
+    node.connect(context.destination);
+    node.start();
+    context._oncomplete_ = completeCallback;
+    context.startRendering();
+    if (window.internals)
+        return internals.observeGC(context);
+}
+
+test(() => {
+    let observer = createContext();
+    gc();
+    if (window.internals)
+        assert_true(observer.wasCollected);
+}, "GC a newly created offline audio context");
+
+promise_test(async () => {
+    let complete = false;
+    let observer = createRenderingContext(() => complete = true);
+    while (!complete) {
+        gc();
+        if (window.internals)
+            assert_false(observer.wasCollected);
+        await new Promise(resolve => setTimeout(resolve, 50));
+    }
+    gc();
+    if (window.internals)
+        assert_true(observer.wasCollected);
+}, "GC a rendering offline audio context");
+</script>

Modified: trunk/Source/WebCore/ChangeLog (246684 => 246685)


--- trunk/Source/WebCore/ChangeLog	2019-06-21 17:53:33 UTC (rev 246684)
+++ trunk/Source/WebCore/ChangeLog	2019-06-21 18:01:13 UTC (rev 246685)
@@ -1,3 +1,40 @@
+2019-06-21  Youenn Fablet  <[email protected]>
+
+        Safari crashes after ~2028 OfflineAudioContext objects are created (they never get garbage collected, consuming a thread each)
+        https://bugs.webkit.org/show_bug.cgi?id=198964
+        <rdar://problem/51891520>
+
+        Reviewed by Jer Noble.
+
+        Move from setPendingActivity/unsetPendingActivity to an
+        m_pendingActivity member which is easier to manage.
+
+        Keep setting a pending activity for AudioContext at construction time
+        but do not do that for Offline contexts.
+        Instead, set the pending activity when startRendering is called.
+        Unset the pending activity when the rendering activity is finished.
+
+        Make m_audioDecoder a unique pointer so that it can lazily be initialized.
+        This removes the burden of creating an audio decoder thread for each context.
+
+        Test: webaudio/offlineaudiocontext-gc.html
+
+        * Modules/webaudio/AudioContext.cpp:
+        (WebCore::AudioContext::AudioContext):
+        (WebCore::AudioContext::constructCommon):
+        (WebCore::AudioContext::clear):
+        (WebCore::AudioContext::decodeAudioData):
+        (WebCore::AudioContext::startRendering):
+        (WebCore::AudioContext::finishedRendering):
+        (WebCore::AudioContext::dispatchEvent):
+        (WebCore::AudioContext::clearPendingActivity):
+        (WebCore::AudioContext::makePendingActivity):
+        To keep it consistent with setPendingActivity/unsetPendingActivity, we
+        explicitly ref/unref the AudioContext. We should try to remove this ref/unref.
+        * Modules/webaudio/AudioContext.h:
+        * Modules/webaudio/OfflineAudioDestinationNode.cpp:
+        (WebCore::OfflineAudioDestinationNode::startRendering):
+
 2019-06-21  Truitt Savell  <[email protected]>
 
         Unreviewed, rolling out r246655.

Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp (246684 => 246685)


--- trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp	2019-06-21 17:53:33 UTC (rev 246684)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp	2019-06-21 18:01:13 UTC (rev 246685)
@@ -99,6 +99,7 @@
 #include <wtf/MainThread.h>
 #include <wtf/Ref.h>
 #include <wtf/RefCounted.h>
+#include <wtf/Scope.h>
 #include <wtf/text/WTFString.h>
 
 const unsigned MaxPeriodicWaveLength = 4096;
@@ -141,6 +142,10 @@
     , m_mediaSession(PlatformMediaSession::create(*this))
     , m_eventQueue(std::make_unique<GenericEventQueue>(*this))
 {
+    // According to spec AudioContext must die only after page navigate.
+    // Lets mark it as ActiveDOMObject with pending activity and unmark it in clear method.
+    makePendingActivity();
+
     constructCommon();
 
     m_destinationNode = DefaultAudioDestinationNode::create(*this);
@@ -172,10 +177,6 @@
 
 void AudioContext::constructCommon()
 {
-    // According to spec AudioContext must die only after page navigate.
-    // Lets mark it as ActiveDOMObject with pending activity and unmark it in clear method.
-    setPendingActivity(*this);
-
     FFTFrame::initialize();
     
     m_listener = AudioListener::create();
@@ -242,6 +243,8 @@
 
 void AudioContext::clear()
 {
+    Ref<AudioContext> protectedThis(*this);
+
     // We have to release our reference to the destination node before the context will ever be deleted since the destination node holds a reference to the context.
     if (m_destinationNode)
         m_destinationNode = nullptr;
@@ -253,8 +256,7 @@
         m_nodesMarkedForDeletion.clear();
     } while (m_nodesToDelete.size());
 
-    // It was set in constructCommon.
-    unsetPendingActivity(*this);
+    clearPendingActivity();
 }
 
 void AudioContext::uninitialize()
@@ -429,7 +431,9 @@
 
 void AudioContext::decodeAudioData(Ref<ArrayBuffer>&& audioData, RefPtr<AudioBufferCallback>&& successCallback, RefPtr<AudioBufferCallback>&& errorCallback)
 {
-    m_audioDecoder.decodeAsync(WTFMove(audioData), sampleRate(), WTFMove(successCallback), WTFMove(errorCallback));
+    if (!m_audioDecoder)
+        m_audioDecoder = std::make_unique<AsyncAudioDecoder>();
+    m_audioDecoder->decodeAsync(WTFMove(audioData), sampleRate(), WTFMove(successCallback), WTFMove(errorCallback));
 }
 
 ExceptionOr<Ref<AudioBufferSourceNode>> AudioContext::createBufferSource()
@@ -1141,6 +1145,8 @@
     if (m_isStopScheduled || !willBeginPlayback())
         return;
 
+    makePendingActivity();
+
     destination()->startRendering();
     setState(State::Running);
 }
@@ -1176,14 +1182,23 @@
     });
 }
 
-void AudioContext::fireCompletionEvent()
+void AudioContext::finishedRendering(bool didRendering)
 {
+    ASSERT(isOfflineContext());
     ASSERT(isMainThread());
     if (!isMainThread())
         return;
 
+    auto clearPendingActivityIfExitEarly = WTF::makeScopeExit([this] {
+        clearPendingActivity();
+    });
+
+
     ALWAYS_LOG(LOGIDENTIFIER);
-    
+
+    if (!didRendering)
+        return;
+
     AudioBuffer* renderedBuffer = m_renderTarget.get();
     setState(State::Closed);
 
@@ -1192,12 +1207,20 @@
         return;
 
     // Avoid firing the event if the document has already gone away.
-    if (!m_isStopScheduled) {
-        // Call the offline rendering completion event listener.
-        m_eventQueue->enqueueEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
-    }
+    if (m_isStopScheduled)
+        return;
+
+    clearPendingActivityIfExitEarly.release();
+    m_eventQueue->enqueueEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
 }
 
+void AudioContext::dispatchEvent(Event& event)
+{
+    EventTarget::dispatchEvent(event);
+    if (event.eventInterface() == OfflineAudioCompletionEventInterfaceType)
+        clearPendingActivity();
+}
+
 void AudioContext::incrementActiveSourceCount()
 {
     ++m_activeSourceCount;
@@ -1347,6 +1370,23 @@
         m_scriptExecutionContext->addConsoleMessage(source, level, message);
 }
 
+void AudioContext::clearPendingActivity()
+{
+    if (!m_pendingActivity)
+        return;
+    m_pendingActivity = nullptr;
+    // FIXME: Remove this specific deref() and ref() call in makePendingActivity().
+    deref();
+}
+
+void AudioContext::makePendingActivity()
+{
+    if (m_pendingActivity)
+        return;
+    m_pendingActivity = ActiveDOMObject::makePendingActivity(*this);
+    ref();
+}
+
 #if !RELEASE_LOG_DISABLED
 WTFLogChannel& AudioContext::logChannel() const
 {

Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.h (246684 => 246685)


--- trunk/Source/WebCore/Modules/webaudio/AudioContext.h	2019-06-21 17:53:33 UTC (rev 246684)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.h	2019-06-21 18:01:13 UTC (rev 246685)
@@ -259,8 +259,8 @@
     using ThreadSafeRefCounted::deref;
 
     void startRendering();
-    void fireCompletionEvent();
-    
+    void finishedRendering(bool didRendering);
+
     static unsigned s_hardwareContextCount;
 
     // Restrictions to change default behaviors.
@@ -297,7 +297,9 @@
     AudioContext(Document&, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate);
     
     static bool isSampleRateRangeGood(float sampleRate);
-    
+    void clearPendingActivity();
+    void makePendingActivity();
+
 private:
     void constructCommon();
 
@@ -320,6 +322,7 @@
 
     // EventTarget
     ScriptExecutionContext* scriptExecutionContext() const final;
+    void dispatchEvent(Event&) final;
 
     // MediaProducer
     MediaProducer::MediaStateFlags mediaState() const override;
@@ -429,7 +432,7 @@
     Thread* volatile m_audioThread { nullptr };
     Thread* volatile m_graphOwnerThread { nullptr }; // if the lock is held then this is the thread which owns it, otherwise == nullptr.
 
-    AsyncAudioDecoder m_audioDecoder;
+    std::unique_ptr<AsyncAudioDecoder> m_audioDecoder;
 
     // This is considering 32 is large enough for multiple channels audio. 
     // It is somewhat arbitrary and could be increased if necessary.
@@ -441,6 +444,7 @@
     BehaviorRestrictions m_restrictions { NoRestrictions };
 
     State m_state { State::Suspended };
+    RefPtr<PendingActivity<AudioContext>> m_pendingActivity;
 };
 
 // FIXME: Find out why these ==/!= functions are needed and remove them if possible.

Modified: trunk/Source/WebCore/Modules/webaudio/OfflineAudioDestinationNode.cpp (246684 => 246685)


--- trunk/Source/WebCore/Modules/webaudio/OfflineAudioDestinationNode.cpp	2019-06-21 17:53:33 UTC (rev 246684)
+++ trunk/Source/WebCore/Modules/webaudio/OfflineAudioDestinationNode.cpp	2019-06-21 18:01:13 UTC (rev 246685)
@@ -94,8 +94,7 @@
     m_renderThread = Thread::create("offline renderer", [this] {
         bool didRender = offlineRender();
         callOnMainThread([this, didRender] {
-            if (didRender)
-                context().fireCompletionEvent();
+            context().finishedRendering(didRender);
             deref();
         });
     });
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to