<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[286481] trunk/Source</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/286481">286481</a></dd>
<dt>Author</dt> <dd>simon.fraser@apple.com</dd>
<dt>Date</dt> <dd>2021-12-02 22:05:49 -0800 (Thu, 02 Dec 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>A Safari tab can rarely get stuck in a state where rendering updates stop happening
https://bugs.webkit.org/show_bug.cgi?id=233784
rdar://85445072

Reviewed by Chris Dumez.

Sometimes a Safari tab can get into a state where rendering updates cease to happen,
which manifests as partially broken scrolling, blank tiles revealed when scrolling,
and somewhat broken page updates. I was able to sometimes reproduce this by clicking
on links in eBay emails from Mail on a system with two displays.

>From the one time I reproduce with logging, the output indicated that DisplayRefreshMonitor::displayLinkFired()
would early return because isPreviousFrameDone() was false. The only way for that to occur,
barring memory corruption, is if DisplayRefreshMonitorMac::dispatchDisplayDidRefresh() returned early,
which it does if the callback comes twice in a single event loop; this may explain the rarity.

So fix DisplayRefreshMonitorMac::dispatchDisplayDidRefresh() call setIsPreviousFrameDone(true)
so the next callback can make progress

Also add some locking annotations and fix one missing lock, and some release logging.

Source/WebCore:

* platform/graphics/DisplayRefreshMonitor.cpp:
(WebCore::DisplayRefreshMonitor::stop):
(WebCore::DisplayRefreshMonitor::firedAndReachedMaxUnscheduledFireCount):
(WebCore::DisplayRefreshMonitor::displayLinkFired):
* platform/graphics/DisplayRefreshMonitor.h:
(WebCore::DisplayRefreshMonitor::WTF_REQUIRES_LOCK):
(WebCore::DisplayRefreshMonitor::WTF_GUARDED_BY_LOCK):
(WebCore::DisplayRefreshMonitor::setMaxUnscheduledFireCount): Deleted.
(WebCore::DisplayRefreshMonitor::isScheduled const): Deleted.
(WebCore::DisplayRefreshMonitor::setIsScheduled): Deleted.
(WebCore::DisplayRefreshMonitor::isPreviousFrameDone const): Deleted.
(WebCore::DisplayRefreshMonitor::setIsPreviousFrameDone): Deleted.

Source/WebKit:

* WebProcess/WebPage/mac/DisplayRefreshMonitorMac.cpp:
(WebKit::DisplayRefreshMonitorMac::dispatchDisplayDidRefresh):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsDisplayRefreshMonitorcpp">trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsDisplayRefreshMonitorh">trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.h</a></li>
<li><a href="#trunkSourceWebKitChangeLog">trunk/Source/WebKit/ChangeLog</a></li>
<li><a href="#trunkSourceWebKitWebProcessWebPageRemoteLayerTreeRemoteLayerTreeDisplayRefreshMonitormm">trunk/Source/WebKit/WebProcess/WebPage/RemoteLayerTree/RemoteLayerTreeDisplayRefreshMonitor.mm</a></li>
<li><a href="#trunkSourceWebKitWebProcessWebPagemacDisplayRefreshMonitorMaccpp">trunk/Source/WebKit/WebProcess/WebPage/mac/DisplayRefreshMonitorMac.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (286480 => 286481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-12-03 04:49:45 UTC (rev 286480)
+++ trunk/Source/WebCore/ChangeLog      2021-12-03 06:05:49 UTC (rev 286481)
</span><span class="lines">@@ -1,3 +1,39 @@
</span><ins>+2021-12-02  Simon Fraser  <simon.fraser@apple.com>
+
+        A Safari tab can rarely get stuck in a state where rendering updates stop happening
+        https://bugs.webkit.org/show_bug.cgi?id=233784
+        rdar://85445072
+
+        Reviewed by Chris Dumez.
+
+        Sometimes a Safari tab can get into a state where rendering updates cease to happen,
+        which manifests as partially broken scrolling, blank tiles revealed when scrolling,
+        and somewhat broken page updates. I was able to sometimes reproduce this by clicking
+        on links in eBay emails from Mail on a system with two displays.
+
+        From the one time I reproduce with logging, the output indicated that DisplayRefreshMonitor::displayLinkFired()
+        would early return because isPreviousFrameDone() was false. The only way for that to occur,
+        barring memory corruption, is if DisplayRefreshMonitorMac::dispatchDisplayDidRefresh() returned early,
+        which it does if the callback comes twice in a single event loop; this may explain the rarity.
+
+        So fix DisplayRefreshMonitorMac::dispatchDisplayDidRefresh() call setIsPreviousFrameDone(true)
+        so the next callback can make progress
+
+        Also add some locking annotations and fix one missing lock, and some release logging.
+
+        * platform/graphics/DisplayRefreshMonitor.cpp:
+        (WebCore::DisplayRefreshMonitor::stop):
+        (WebCore::DisplayRefreshMonitor::firedAndReachedMaxUnscheduledFireCount):
+        (WebCore::DisplayRefreshMonitor::displayLinkFired):
+        * platform/graphics/DisplayRefreshMonitor.h:
+        (WebCore::DisplayRefreshMonitor::WTF_REQUIRES_LOCK):
+        (WebCore::DisplayRefreshMonitor::WTF_GUARDED_BY_LOCK):
+        (WebCore::DisplayRefreshMonitor::setMaxUnscheduledFireCount): Deleted.
+        (WebCore::DisplayRefreshMonitor::isScheduled const): Deleted.
+        (WebCore::DisplayRefreshMonitor::setIsScheduled): Deleted.
+        (WebCore::DisplayRefreshMonitor::isPreviousFrameDone const): Deleted.
+        (WebCore::DisplayRefreshMonitor::setIsPreviousFrameDone): Deleted.
+
</ins><span class="cx"> 2021-12-02  Tyler Wilcock  <tyler_w@apple.com>
</span><span class="cx"> 
</span><span class="cx">         AX: In AccessibilityRenderObject::documentLinks, use existing image-map document links instead of always creating new ones
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsDisplayRefreshMonitorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.cpp (286480 => 286481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.cpp 2021-12-03 04:49:45 UTC (rev 286480)
+++ trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.cpp    2021-12-03 06:05:49 UTC (rev 286481)
</span><span class="lines">@@ -85,6 +85,8 @@
</span><span class="cx"> void DisplayRefreshMonitor::stop()
</span><span class="cx"> {
</span><span class="cx">     stopNotificationMechanism();
</span><ins>+
+    Locker locker { m_lock };
</ins><span class="cx">     setIsScheduled(false);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -152,8 +154,6 @@
</span><span class="cx"> 
</span><span class="cx"> bool DisplayRefreshMonitor::firedAndReachedMaxUnscheduledFireCount()
</span><span class="cx"> {
</span><del>-    ASSERT(m_lock.isLocked());
-
</del><span class="cx">     if (isScheduled()) {
</span><span class="cx">         m_unscheduledFireCount = 0;
</span><span class="cx">         return false;
</span><span class="lines">@@ -169,8 +169,10 @@
</span><span class="cx">         Locker locker { m_lock };
</span><span class="cx"> 
</span><span class="cx">         // This may be off the main thread.
</span><del>-        if (!isPreviousFrameDone())
</del><ins>+        if (!isPreviousFrameDone()) {
+            RELEASE_LOG(DisplayLink, "[Web] DisplayRefreshMonitor::displayLinkFired for display %u - previous frame is not complete", displayID());
</ins><span class="cx">             return;
</span><ins>+        }
</ins><span class="cx"> 
</span><span class="cx">         LOG_WITH_STREAM(DisplayLink, stream << "[Web] DisplayRefreshMonitor::displayLinkFired for display " << displayID() << " - scheduled " << isScheduled() << " unscheduledFireCount " << m_unscheduledFireCount << " of " << m_maxUnscheduledFireCount);
</span><span class="cx">         if (firedAndReachedMaxUnscheduledFireCount()) {
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsDisplayRefreshMonitorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.h (286480 => 286481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.h   2021-12-03 04:49:45 UTC (rev 286480)
+++ trunk/Source/WebCore/platform/graphics/DisplayRefreshMonitor.h      2021-12-03 06:05:49 UTC (rev 286481)
</span><span class="lines">@@ -72,23 +72,23 @@
</span><span class="cx"> 
</span><span class="cx">     WEBCORE_EXPORT virtual void dispatchDisplayDidRefresh(const DisplayUpdate&);
</span><span class="cx"> 
</span><del>-    Lock& lock() { return m_lock; }
-    void setMaxUnscheduledFireCount(unsigned count) { m_maxUnscheduledFireCount = count; }
</del><ins>+    Lock& lock() WTF_RETURNS_LOCK(m_lock) { return m_lock; }
+    void setMaxUnscheduledFireCount(unsigned count) WTF_REQUIRES_LOCK(m_lock) { m_maxUnscheduledFireCount = count; }
</ins><span class="cx"> 
</span><span class="cx">     // Returns true if the start was successful.
</span><span class="cx">     WEBCORE_EXPORT virtual bool startNotificationMechanism() = 0;
</span><span class="cx">     WEBCORE_EXPORT virtual void stopNotificationMechanism() = 0;
</span><span class="cx"> 
</span><del>-    bool isScheduled() const { return m_scheduled; }
-    void setIsScheduled(bool scheduled) { m_scheduled = scheduled; }
</del><ins>+    bool isScheduled() const WTF_REQUIRES_LOCK(m_lock) { return m_scheduled; }
+    void setIsScheduled(bool scheduled) WTF_REQUIRES_LOCK(m_lock) { m_scheduled = scheduled; }
</ins><span class="cx"> 
</span><del>-    bool isPreviousFrameDone() const { return m_previousFrameDone; }
-    void setIsPreviousFrameDone(bool done) { m_previousFrameDone = done; }
</del><ins>+    bool isPreviousFrameDone() const WTF_REQUIRES_LOCK(m_lock) { return m_previousFrameDone; }
+    void setIsPreviousFrameDone(bool done) WTF_REQUIRES_LOCK(m_lock) { m_previousFrameDone = done; }
</ins><span class="cx"> 
</span><span class="cx">     WEBCORE_EXPORT void displayDidRefresh(const DisplayUpdate&);
</span><span class="cx"> 
</span><span class="cx"> private:
</span><del>-    bool firedAndReachedMaxUnscheduledFireCount();
</del><ins>+    bool firedAndReachedMaxUnscheduledFireCount() WTF_REQUIRES_LOCK(m_lock);
</ins><span class="cx"> 
</span><span class="cx">     virtual void adjustPreferredFramesPerSecond(FramesPerSecond) { }
</span><span class="cx"> 
</span><span class="lines">@@ -102,11 +102,11 @@
</span><span class="cx">     std::optional<FramesPerSecond> m_maxClientPreferredFramesPerSecond;
</span><span class="cx"> 
</span><span class="cx">     Lock m_lock;
</span><del>-    bool m_scheduled { false };
-    bool m_previousFrameDone { true };
</del><ins>+    bool m_scheduled WTF_GUARDED_BY_LOCK(m_lock) { false };
+    bool m_previousFrameDone WTF_GUARDED_BY_LOCK(m_lock) { true };
</ins><span class="cx">     
</span><del>-    unsigned m_unscheduledFireCount { 0 };
-    unsigned m_maxUnscheduledFireCount { 0 };
</del><ins>+    unsigned m_unscheduledFireCount WTF_GUARDED_BY_LOCK(m_lock) { 0 };
+    unsigned m_maxUnscheduledFireCount WTF_GUARDED_BY_LOCK(m_lock) { 0 };
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/ChangeLog (286480 => 286481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/ChangeLog    2021-12-03 04:49:45 UTC (rev 286480)
+++ trunk/Source/WebKit/ChangeLog       2021-12-03 06:05:49 UTC (rev 286481)
</span><span class="lines">@@ -1,3 +1,29 @@
</span><ins>+2021-12-02  Simon Fraser  <simon.fraser@apple.com>
+
+        A Safari tab can rarely get stuck in a state where rendering updates stop happening
+        https://bugs.webkit.org/show_bug.cgi?id=233784
+        rdar://85445072
+
+        Reviewed by Chris Dumez.
+
+        Sometimes a Safari tab can get into a state where rendering updates cease to happen,
+        which manifests as partially broken scrolling, blank tiles revealed when scrolling,
+        and somewhat broken page updates. I was able to sometimes reproduce this by clicking
+        on links in eBay emails from Mail on a system with two displays.
+
+        From the one time I reproduce with logging, the output indicated that DisplayRefreshMonitor::displayLinkFired()
+        would early return because isPreviousFrameDone() was false. The only way for that to occur,
+        barring memory corruption, is if DisplayRefreshMonitorMac::dispatchDisplayDidRefresh() returned early,
+        which it does if the callback comes twice in a single event loop; this may explain the rarity.
+
+        So fix DisplayRefreshMonitorMac::dispatchDisplayDidRefresh() call setIsPreviousFrameDone(true)
+        so the next callback can make progress
+
+        Also add some locking annotations and fix one missing lock, and some release logging.
+
+        * WebProcess/WebPage/mac/DisplayRefreshMonitorMac.cpp:
+        (WebKit::DisplayRefreshMonitorMac::dispatchDisplayDidRefresh):
+
</ins><span class="cx"> 2021-12-02  Chris Dumez  <cdumez@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Regression(r283179) Google Drive freezes after downloading a folder
</span></span></pre></div>
<a id="trunkSourceWebKitWebProcessWebPageRemoteLayerTreeRemoteLayerTreeDisplayRefreshMonitormm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/WebProcess/WebPage/RemoteLayerTree/RemoteLayerTreeDisplayRefreshMonitor.mm (286480 => 286481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/WebProcess/WebPage/RemoteLayerTree/RemoteLayerTreeDisplayRefreshMonitor.mm   2021-12-03 04:49:45 UTC (rev 286480)
+++ trunk/Source/WebKit/WebProcess/WebPage/RemoteLayerTree/RemoteLayerTreeDisplayRefreshMonitor.mm      2021-12-03 06:05:49 UTC (rev 286481)
</span><span class="lines">@@ -67,11 +67,14 @@
</span><span class="cx">     if (!m_drawingArea)
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    if (!isScheduled()) {
-        LOG_WITH_STREAM(DisplayLink, stream << "RemoteLayerTreeDisplayRefreshMonitor::requestRefreshCallback - triggering update");
-        static_cast<DrawingArea&>(*m_drawingArea.get()).triggerRenderingUpdate();
-    }
</del><ins>+    Locker locker { lock() };
</ins><span class="cx"> 
</span><ins>+    if (isScheduled())
+        return true;
+
+    LOG_WITH_STREAM(DisplayLink, stream << "RemoteLayerTreeDisplayRefreshMonitor::requestRefreshCallback - triggering update");
+    static_cast<DrawingArea&>(*m_drawingArea.get()).triggerRenderingUpdate();
+
</ins><span class="cx">     setIsScheduled(true);
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="lines">@@ -78,12 +81,15 @@
</span><span class="cx"> 
</span><span class="cx"> void RemoteLayerTreeDisplayRefreshMonitor::didUpdateLayers()
</span><span class="cx"> {
</span><del>-    setIsScheduled(false);
</del><ins>+    {
+        Locker locker { lock() };
+        setIsScheduled(false);
</ins><span class="cx"> 
</span><del>-    if (!isPreviousFrameDone())
-        return;
</del><ins>+        if (!isPreviousFrameDone())
+            return;
</ins><span class="cx"> 
</span><del>-    setIsPreviousFrameDone(false);
</del><ins>+        setIsPreviousFrameDone(false);
+    }
</ins><span class="cx">     displayDidRefresh(m_currentUpdate);
</span><span class="cx">     m_currentUpdate = m_currentUpdate.nextUpdate();
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKitWebProcessWebPagemacDisplayRefreshMonitorMaccpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/WebProcess/WebPage/mac/DisplayRefreshMonitorMac.cpp (286480 => 286481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/WebProcess/WebPage/mac/DisplayRefreshMonitorMac.cpp  2021-12-03 04:49:45 UTC (rev 286480)
+++ trunk/Source/WebKit/WebProcess/WebPage/mac/DisplayRefreshMonitorMac.cpp     2021-12-03 06:05:49 UTC (rev 286481)
</span><span class="lines">@@ -60,8 +60,12 @@
</span><span class="cx"> void DisplayRefreshMonitorMac::dispatchDisplayDidRefresh(const DisplayUpdate& displayUpdate)
</span><span class="cx"> {
</span><span class="cx">     // FIXME: This will perturb displayUpdate.
</span><del>-    if (!m_firstCallbackInCurrentRunloop)
</del><ins>+    if (!m_firstCallbackInCurrentRunloop) {
+        RELEASE_LOG(DisplayLink, "[Web] DisplayRefreshMonitorMac::dispatchDisplayDidRefresh() for display %u - m_firstCallbackInCurrentRunloop is false", displayID());
+        Locker locker { lock() };
+        setIsPreviousFrameDone(true);
</ins><span class="cx">         return;
</span><ins>+    }
</ins><span class="cx"> 
</span><span class="cx">     DisplayRefreshMonitor::dispatchDisplayDidRefresh(displayUpdate);
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>