<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>163845</bug_id>
          
          <creation_ts>2016-10-22 04:01:22 -0700</creation_ts>
          <short_desc>Promise - MutationObserver interaction spins the event loop</short_desc>
          <delta_ts>2019-11-22 22:33:58 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>Safari Technology Preview</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>OS X 10.11</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>WORKSFORME</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=204546</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Miklos Bertalan">miklos.bertalan</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>cdumez</cc>
    
    <cc>rniwa</cc>
    
    <cc>youennf</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1243357</commentid>
    <comment_count>0</comment_count>
    <who name="Miklos Bertalan">miklos.bertalan</who>
    <bug_when>2016-10-22 04:01:22 -0700</bug_when>
    <thetext>Queueing a Promise from a MutationObserver handler or triggering an observed change from a Promise callback spins the event loop. According to the spec, it should register a new microtask and not a new task.

Code to reproduce (it is also attached):

&lt;div class=&quot;tester&quot;&gt;&lt;/div&gt;
&lt;script&gt;
const tester = document.querySelector(&apos;.tester&apos;)
let counter = 0

setTimeout(() =&gt; console.log(&apos;timeout&apos;))

new MutationObserver(() =&gt; {
  console.log(&apos;mutation&apos;)
  if (counter &lt; 100) {
    Promise.resolve().then(() =&gt; {
      console.log(&apos;promise&apos;)
      tester.setAttribute(&apos;data-random&apos;, Math.random())
    })
  }
  counter++
}).observe(tester, {attributes: true})

tester.setAttribute(&apos;data-random&apos;, Math.random())
&lt;/script&gt;

Issue:

It the above code should print: mutation, promise, mutation, promise, ... timeout
Instead it prints: mutation, timeout, promise, mutation, promise, mutation, ...

Notes:

It seems to get it wrong consistently (not just some of the time). I think it is not caused by any kind of throttling.
It seems to queue a new task for both directions (Promise -&gt; Mutation, Mutation -&gt; Promise).
(Promise -&gt; Promise) and (Mutation -&gt; Mutation) are both handled correctly as new microtasks.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1243358</commentid>
    <comment_count>1</comment_count>
      <attachid>292494</attachid>
    <who name="Miklos Bertalan">miklos.bertalan</who>
    <bug_when>2016-10-22 04:02:26 -0700</bug_when>
    <thetext>Created attachment 292494
Script to recreate the bug</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1593181</commentid>
    <comment_count>2</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2019-11-22 22:33:51 -0800</bug_when>
    <thetext>This appears to be fixed now.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>292494</attachid>
            <date>2016-10-22 04:02:26 -0700</date>
            <delta_ts>2016-10-22 04:02:26 -0700</delta_ts>
            <desc>Script to recreate the bug</desc>
            <filename>PromiseMutObserverInteraction.html</filename>
            <type>text/html</type>
            <size>524</size>
            <attacher name="Miklos Bertalan">miklos.bertalan</attacher>
            
              <data encoding="base64">PGRpdiBjbGFzcz0idGVzdGVyIj48L2Rpdj4KPHNjcmlwdD4KY29uc3QgdGVzdGVyID0gZG9jdW1l
bnQucXVlcnlTZWxlY3RvcignLnRlc3RlcicpCmxldCBjb3VudGVyID0gMAoKc2V0VGltZW91dCgo
KSA9PiBjb25zb2xlLmxvZygndGltZW91dCcpKQpyZXF1ZXN0QW5pbWF0aW9uRnJhbWUoKCkgPT4g
Y29uc29sZS5sb2coJ2FuaW1hdGlvbkZyYW1lJykpCgpuZXcgTXV0YXRpb25PYnNlcnZlcigoKSA9
PiB7CiAgY29uc29sZS5sb2coJ211dGF0aW9uJykKICBpZiAoY291bnRlciA8IDEwMCkgewogICAg
UHJvbWlzZS5yZXNvbHZlKCkudGhlbigoKSA9PiB7CiAgICAgIGNvbnNvbGUubG9nKCdwcm9taXNl
JykKICAgICAgdGVzdGVyLnNldEF0dHJpYnV0ZSgnZGF0YS1yYW5kb20nLCBNYXRoLnJhbmRvbSgp
KQogICAgfSkKICB9CiAgY291bnRlcisrCn0pLm9ic2VydmUodGVzdGVyLCB7YXR0cmlidXRlczog
dHJ1ZX0pCgp0ZXN0ZXIuc2V0QXR0cmlidXRlKCdkYXRhLXJhbmRvbScsIE1hdGgucmFuZG9tKCkp
Cjwvc2NyaXB0Pgo=
</data>

          </attachment>
      

    </bug>

</bugzilla>