<?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>222746</bug_id>
          
          <creation_ts>2021-03-04 09:49:27 -0800</creation_ts>
          <short_desc>IDBTransaction is closed prematurely if a microtask is awaited after opening the transaction under some circumstances</short_desc>
          <delta_ts>2021-03-11 10:30:09 -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>WebCore Misc.</component>
          <version>Safari Technology Preview</version>
          <rep_platform>Mac (Intel)</rep_platform>
          <op_sys>macOS 10.15</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>216769</dup_id>
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=216769</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Bruno Windels">brunow</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>beidson</cc>
    
    <cc>cdumez</cc>
    
    <cc>ggaren</cc>
    
    <cc>keith_miller</cc>
    
    <cc>rniwa</cc>
    
    <cc>sihui_liu</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1735879</commentid>
    <comment_count>0</comment_count>
      <attachid>422245</attachid>
    <who name="Bruno Windels">brunow</who>
    <bug_when>2021-03-04 09:49:27 -0800</bug_when>
    <thetext>Created attachment 422245
a small test case reproducing the problem

I&apos;m encountering an issue with IndexedDB which I can reproduce on several webkit based browsers (Safari on iOS 13.4, Safari TP 121 on macOS 10.15, and WebKitGTK 2.30.5 on Linux).
The issue is that under some circumstances, an IDBTransaction can get closed if you wait a single microtask (e.g. await Promise.resolve()) to queue your first IDBRequest (as opposed to a macrotask, which is expected).

In the web app where I encountered the issue, it happens easily if I have optional async code path after opening a transaction (async because they use indexeddb, no other macro tasks).

I&apos;ve attached a test case that reproduces the problem, where scheduling a macro task in between two transactions seems to trigger the bug.
I have reduced it as much as possible, so every step is essential to reproduce the problem AFAIK.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1737146</commentid>
    <comment_count>1</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2021-03-08 10:09:11 -0800</bug_when>
    <thetext>The IDBTransaction constructor uses this:
 vm.whenIdle([protectedThis = makeRef(*this)]() {
            protectedThis-&gt;deactivate();
        });

It seems our lambda is getting called prematurely here because the JS is using await on a setTimeout.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1737158</commentid>
    <comment_count>2</comment_count>
    <who name="Sihui Liu">sihui_liu</who>
    <bug_when>2021-03-08 10:35:30 -0800</bug_when>
    <thetext>Seem the same as https://bugs.webkit.org/show_bug.cgi?id=216769</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1737169</commentid>
    <comment_count>3</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2021-03-08 11:00:35 -0800</bug_when>
    <thetext>Interesting part of the repro case:
```
            try {
                const db = await openDatabase(dbName, db =&gt; {
                    db.createObjectStore(&quot;test&quot;, {keyPath: &quot;key&quot;});
                }, 1);
                const readTxn = db.transaction([&quot;test&quot;], &quot;readonly&quot;);
                await reqAsPromise(readTxn.objectStore(&quot;test&quot;).get(&quot;somekey&quot;));
                // schedule a macro task in between the two txns
                await new Promise(r =&gt; setTimeout(r, 0));
                const writeTxn = db.transaction([&quot;test&quot;], &quot;readwrite&quot;);
                await Promise.resolve();
                writeTxn.objectStore(&quot;test&quot;).add({key: &quot;somekey&quot;, value: &quot;foo&quot;});
                await txnAsPromise(writeTxn);
            } catch (err) {
                if (err.name === &quot;TransactionInactiveError&quot;) {
                    return true; // BUG
                }
```

So it looks like whenIdle called our lambda before the call to:
writeTxn.objectStore(&quot;test&quot;).add()

Calling our lambda deactivated the writeTxn, which is why we reject the promise with a TransactionInactiveError.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1738493</commentid>
    <comment_count>4</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2021-03-11 09:50:15 -0800</bug_when>
    <thetext>&lt;rdar://problem/75320436&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1738516</commentid>
    <comment_count>5</comment_count>
    <who name="Sihui Liu">sihui_liu</who>
    <bug_when>2021-03-11 10:30:09 -0800</bug_when>
    <thetext>

*** This bug has been marked as a duplicate of bug 216769 ***</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>422245</attachid>
            <date>2021-03-04 09:49:27 -0800</date>
            <delta_ts>2021-03-04 09:49:27 -0800</delta_ts>
            <desc>a small test case reproducing the problem</desc>
            <filename>idb-test-safari-close-txn.html</filename>
            <type>text/html</type>
            <size>2553</size>
            <attacher name="Bruno Windels">brunow</attacher>
            
              <data encoding="base64">PGh0bWw+CjxoZWFkPjxtZXRhIGNoYXJzZXQ9InV0Zi04Ij48L2hlYWQ+Cjxib2R5PgoJPHNjcmlw
dCB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiPgoKICAgICAgICBjb25zdCBsb2cgPSAoLi4ucGFyYW1z
KSA9PiB7CiAgICAgICAgICAgIGRvY3VtZW50LndyaXRlKHBhcmFtcy5qb2luKCIgIikrIjxicj4i
KTsKICAgICAgICB9OwoKICAgICAgICBmdW5jdGlvbiByZXFBc1Byb21pc2UocmVxKSB7CiAgICAg
ICAgICAgIHJldHVybiBuZXcgUHJvbWlzZSgocmVzb2x2ZSwgcmVqZWN0KSA9PiB7CiAgICAgICAg
ICAgICAgICByZXEub25zdWNjZXNzID0gKCkgPT4gcmVzb2x2ZShyZXEucmVzdWx0KTsKICAgICAg
ICAgICAgICAgIHJlcS5vbmVycm9yID0gKGVycikgPT4gcmVqZWN0KGVycik7CiAgICAgICAgICAg
IH0pOwogICAgICAgIH0KCiAgICAgICAgZnVuY3Rpb24gdHhuQXNQcm9taXNlKHR4bikgewogICAg
ICAgICAgICByZXR1cm4gbmV3IFByb21pc2UoKHJlc29sdmUsIHJlamVjdCkgPT4gewogICAgICAg
ICAgICAgICAgdHhuLmFkZEV2ZW50TGlzdGVuZXIoImNvbXBsZXRlIiwgcmVzb2x2ZSk7CiAgICAg
ICAgICAgICAgICB0eG4uYWRkRXZlbnRMaXN0ZW5lcigiYWJvcnQiLCByZWplY3QpOwogICAgICAg
ICAgICB9KTsKICAgICAgICB9CgogICAgICAgIGZ1bmN0aW9uIG9wZW5EYXRhYmFzZShuYW1lLCBj
cmVhdGVPYmplY3RTdG9yZSwgdmVyc2lvbikgewogICAgICAgICAgICBjb25zdCByZXEgPSBpbmRl
eGVkREIub3BlbihuYW1lLCB2ZXJzaW9uKTsKICAgICAgICAgICAgcmVxLm9udXBncmFkZW5lZWRl
ZCA9IChldikgPT4gewogICAgICAgICAgICAgICAgY29uc3QgZGIgPSBldi50YXJnZXQucmVzdWx0
OwogICAgICAgICAgICAgICAgY29uc3QgdHhuID0gZXYudGFyZ2V0LnRyYW5zYWN0aW9uOwogICAg
ICAgICAgICAgICAgY29uc3Qgb2xkVmVyc2lvbiA9IGV2Lm9sZFZlcnNpb247CiAgICAgICAgICAg
ICAgICBjcmVhdGVPYmplY3RTdG9yZShkYiwgdHhuLCBvbGRWZXJzaW9uLCB2ZXJzaW9uKTsKICAg
ICAgICAgICAgfTsgCiAgICAgICAgICAgIHJldHVybiByZXFBc1Byb21pc2UocmVxKTsKICAgICAg
ICB9CgogICAgICAgIGFz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ik7CiAgICAgICAgICAg
IH0gY2F0Y2ggKGVycikgewogICAgICAgICAgICAgICAgaWYgKGVyci5uYW1lID09PSAiVHJhbnNh
Y3Rpb25JbmFjdGl2ZUVycm9yIikgewogICAgICAgICAgICAgICAgICAgIHJldHVybiB0cnVlOwog
ICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IGZpbmFsbHkgewogICAgICAgICAgICAgICAg
dHJ5IHsKICAgICAgICAgICAgICAgICAgICBpbmRleGVkREIuZGVsZXRlRGF0YWJhc2UoZGJOYW1l
KTsKICAgICAgICAgICAgICAgIH0gY2F0Y2ggKGVycikge30KICAgICAgICAgICAgfQogICAgICAg
ICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQoKICAgICAgICAoYXN5bmMgKCkgPT4gewogICAg
ICAgICAgICBpZiAoYXdhaXQgZGV0ZWN0V2Via2l0RWFybHlDbG9zZVR4bkJ1ZygpKSB7CiAgICAg
ICAgICAgICAgICBsb2coInRoZSB0ZXN0IGZhaWxlZCwgeW91ciBicm93c2VyIHNlZW1zIHRvIGhh
dmUgdGhlIGJ1ZyIpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgbG9nKCJ0
aGUgdGVzdCBzdWNjZWVkZWQsIHlvdXIgYnJvd3NlciBzZWVtcyBmaW5lIik7CiAgICAgICAgICAg
IH0KICAgICAgICB9KSgpOwoKCTwvc2NyaXB0Pgo8L2JvZHk+CjwvaHRtbD4K
</data>

          </attachment>
      

    </bug>

</bugzilla>