Modern IDB: Prevent the same transaction from being commited/aborted twice.
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 9 May 2016 23:59:55 +0000 (23:59 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 9 May 2016 23:59:55 +0000 (23:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=157494

Reviewed by Tim Horton.

When a page navigates or a worker terminates, in rare cases with specific unfortunate timing, the IDBServer
might receive a request to commit/abort a transaction that is already in the process of committing/aborting.

By moving transactions that are finishing into their own map we can at least detect this situation and
return an error. This seems like an improvement over some mysterious ASSERTs/timeouts.

No new tests:
While apparent that this is at least partially to blame for some existing timeouts/ASSERTs, I could not nail
down a reliable way to reproduce this with a dedicated test.

* Modules/indexeddb/server/UniqueIDBDatabase.cpp:
(WebCore::IDBServer::UniqueIDBDatabase::~UniqueIDBDatabase):
(WebCore::IDBServer::UniqueIDBDatabase::performCurrentDeleteOperation):
(WebCore::IDBServer::UniqueIDBDatabase::didDeleteBackingStore):
(WebCore::IDBServer::UniqueIDBDatabase::prepareToFinishTransaction):
(WebCore::IDBServer::UniqueIDBDatabase::commitTransaction):
(WebCore::IDBServer::UniqueIDBDatabase::didPerformCommitTransaction):
(WebCore::IDBServer::UniqueIDBDatabase::abortTransaction):
(WebCore::IDBServer::UniqueIDBDatabase::didPerformAbortTransaction):
(WebCore::IDBServer::UniqueIDBDatabase::hasUnfinishedTransactions):
(WebCore::IDBServer::UniqueIDBDatabase::operationAndTransactionTimerFired):
(WebCore::IDBServer::UniqueIDBDatabase::takeNextRunnableTransaction):
(WebCore::IDBServer::UniqueIDBDatabase::transactionCompleted): Renamed from inProgressTransactionCompleted.
(WebCore::IDBServer::UniqueIDBDatabase::inProgressTransactionCompleted): Deleted.
* Modules/indexeddb/server/UniqueIDBDatabase.h:

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

Source/WebCore/ChangeLog
Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp
Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.h

index 38f2095..8f464bc 100644 (file)
@@ -1,3 +1,36 @@
+2016-05-09  Brady Eidson  <beidson@apple.com>
+
+        Modern IDB: Prevent the same transaction from being commited/aborted twice.
+        https://bugs.webkit.org/show_bug.cgi?id=157494
+
+        Reviewed by Tim Horton.
+
+        When a page navigates or a worker terminates, in rare cases with specific unfortunate timing, the IDBServer
+        might receive a request to commit/abort a transaction that is already in the process of committing/aborting.
+        
+        By moving transactions that are finishing into their own map we can at least detect this situation and
+        return an error. This seems like an improvement over some mysterious ASSERTs/timeouts.
+
+        No new tests:
+        While apparent that this is at least partially to blame for some existing timeouts/ASSERTs, I could not nail 
+        down a reliable way to reproduce this with a dedicated test.
+        
+        * Modules/indexeddb/server/UniqueIDBDatabase.cpp:
+        (WebCore::IDBServer::UniqueIDBDatabase::~UniqueIDBDatabase):
+        (WebCore::IDBServer::UniqueIDBDatabase::performCurrentDeleteOperation):
+        (WebCore::IDBServer::UniqueIDBDatabase::didDeleteBackingStore):
+        (WebCore::IDBServer::UniqueIDBDatabase::prepareToFinishTransaction):
+        (WebCore::IDBServer::UniqueIDBDatabase::commitTransaction):
+        (WebCore::IDBServer::UniqueIDBDatabase::didPerformCommitTransaction):
+        (WebCore::IDBServer::UniqueIDBDatabase::abortTransaction):
+        (WebCore::IDBServer::UniqueIDBDatabase::didPerformAbortTransaction):
+        (WebCore::IDBServer::UniqueIDBDatabase::hasUnfinishedTransactions):
+        (WebCore::IDBServer::UniqueIDBDatabase::operationAndTransactionTimerFired):
+        (WebCore::IDBServer::UniqueIDBDatabase::takeNextRunnableTransaction):
+        (WebCore::IDBServer::UniqueIDBDatabase::transactionCompleted): Renamed from inProgressTransactionCompleted.
+        (WebCore::IDBServer::UniqueIDBDatabase::inProgressTransactionCompleted): Deleted.
+        * Modules/indexeddb/server/UniqueIDBDatabase.h:
+
 2016-05-09  Tim Horton  <timothy_horton@apple.com>
 
         Draw border and correct icon for placeholder attachments
index a03babb..fe48206 100644 (file)
@@ -60,7 +60,7 @@ UniqueIDBDatabase::~UniqueIDBDatabase()
 {
     LOG(IndexedDB, "UniqueIDBDatabase::~UniqueIDBDatabase() (%p) %s", this, m_identifier.debugString().utf8().data());
     ASSERT(!hasAnyPendingCallbacks());
-    ASSERT(m_inProgressTransactions.isEmpty());
+    ASSERT(!hasUnfinishedTransactions());
     ASSERT(m_pendingTransactions.isEmpty());
     ASSERT(m_openDatabaseConnections.isEmpty());
     ASSERT(m_closePendingDatabaseConnections.isEmpty());
@@ -194,7 +194,7 @@ void UniqueIDBDatabase::performCurrentDeleteOperation()
         return;
     }
 
-    if (!m_inProgressTransactions.isEmpty())
+    if (hasUnfinishedTransactions())
         return;
 
     ASSERT(!hasAnyPendingCallbacks());
@@ -250,7 +250,7 @@ void UniqueIDBDatabase::didDeleteBackingStore(uint64_t deletedVersion)
     ASSERT(m_currentOpenDBRequest);
     ASSERT(m_currentOpenDBRequest->isDeleteRequest());
     ASSERT(!hasAnyPendingCallbacks());
-    ASSERT(m_inProgressTransactions.isEmpty());
+    ASSERT(!hasUnfinishedTransactions());
     ASSERT(m_pendingTransactions.isEmpty());
     ASSERT(m_openDatabaseConnections.isEmpty());
 
@@ -983,6 +983,18 @@ void UniqueIDBDatabase::didPerformIterateCursor(uint64_t callbackIdentifier, con
     performGetResultCallback(callbackIdentifier, error, result);
 }
 
+bool UniqueIDBDatabase::prepareToFinishTransaction(UniqueIDBDatabaseTransaction& transaction)
+{
+    auto takenTransaction = m_inProgressTransactions.take(transaction.info().identifier());
+    if (!takenTransaction)
+        return false;
+
+    ASSERT(!m_finishingTransactions.contains(transaction.info().identifier()));
+    m_finishingTransactions.set(transaction.info().identifier(), WTFMove(takenTransaction));
+
+    return true;
+}
+
 void UniqueIDBDatabase::commitTransaction(UniqueIDBDatabaseTransaction& transaction, ErrorCallback callback)
 {
     ASSERT(isMainThread());
@@ -990,14 +1002,13 @@ void UniqueIDBDatabase::commitTransaction(UniqueIDBDatabaseTransaction& transact
 
     ASSERT(&transaction.databaseConnection().database() == this);
 
-    if (m_versionChangeTransaction == &transaction) {
-        ASSERT(!m_versionChangeDatabaseConnection || &m_versionChangeTransaction->databaseConnection() == m_versionChangeDatabaseConnection);
-        ASSERT(m_databaseInfo->version() == transaction.info().newVersion());
+    uint64_t callbackID = storeCallback(callback);
 
-        invokeOperationAndTransactionTimer();
+    if (!prepareToFinishTransaction(transaction)) {
+        performErrorCallback(callbackID, { IDBDatabaseException::UnknownError, ASCIILiteral("Attempt to commit transaction that is already finishing") });
+        return;
     }
 
-    uint64_t callbackID = storeCallback(callback);
     m_server.postDatabaseTask(createCrossThreadTask(*this, &UniqueIDBDatabase::performCommitTransaction, callbackID, transaction.info().identifier()));
 }
 
@@ -1017,7 +1028,7 @@ void UniqueIDBDatabase::didPerformCommitTransaction(uint64_t callbackIdentifier,
 
     performErrorCallback(callbackIdentifier, error);
 
-    inProgressTransactionCompleted(transactionIdentifier);
+    transactionCompleted(m_finishingTransactions.take(transactionIdentifier));
 }
 
 void UniqueIDBDatabase::abortTransaction(UniqueIDBDatabaseTransaction& transaction, ErrorCallback callback)
@@ -1028,6 +1039,12 @@ void UniqueIDBDatabase::abortTransaction(UniqueIDBDatabaseTransaction& transacti
     ASSERT(&transaction.databaseConnection().database() == this);
 
     uint64_t callbackID = storeCallback(callback);
+
+    if (!prepareToFinishTransaction(transaction)) {
+        performErrorCallback(callbackID, { IDBDatabaseException::UnknownError, ASCIILiteral("Attempt to abort transaction that is already finishing") });
+        return;
+    }
+
     m_server.postDatabaseTask(createCrossThreadTask(*this, &UniqueIDBDatabase::performAbortTransaction, callbackID, transaction.info().identifier()));
 }
 
@@ -1059,7 +1076,11 @@ void UniqueIDBDatabase::didPerformAbortTransaction(uint64_t callbackIdentifier,
     ASSERT(isMainThread());
     LOG(IndexedDB, "(main) UniqueIDBDatabase::didPerformAbortTransaction");
 
+    auto transaction = m_finishingTransactions.take(transactionIdentifier);
+    ASSERT(transaction);
+
     if (m_versionChangeTransaction && m_versionChangeTransaction->info().identifier() == transactionIdentifier) {
+        ASSERT(m_versionChangeTransaction == transaction);
         ASSERT(!m_versionChangeDatabaseConnection || &m_versionChangeTransaction->databaseConnection() == m_versionChangeDatabaseConnection);
         ASSERT(m_versionChangeTransaction->originalDatabaseInfo());
         m_databaseInfo = std::make_unique<IDBDatabaseInfo>(*m_versionChangeTransaction->originalDatabaseInfo());
@@ -1067,7 +1088,7 @@ void UniqueIDBDatabase::didPerformAbortTransaction(uint64_t callbackIdentifier,
 
     performErrorCallback(callbackIdentifier, error);
 
-    inProgressTransactionCompleted(transactionIdentifier);
+    transactionCompleted(WTFMove(transaction));
 }
 
 void UniqueIDBDatabase::transactionDestroyed(UniqueIDBDatabaseTransaction& transaction)
@@ -1127,6 +1148,11 @@ bool UniqueIDBDatabase::isCurrentlyInUse() const
     return !m_openDatabaseConnections.isEmpty() || !m_closePendingDatabaseConnections.isEmpty() || !m_pendingOpenDBRequests.isEmpty() || m_currentOpenDBRequest || m_versionChangeDatabaseConnection || m_versionChangeTransaction || m_isOpeningBackingStore || m_deleteBackingStoreInProgress;
 }
 
+bool UniqueIDBDatabase::hasUnfinishedTransactions() const
+{
+    return !m_inProgressTransactions.isEmpty() || !m_finishingTransactions.isEmpty();
+}
+
 void UniqueIDBDatabase::invokeOperationAndTransactionTimer()
 {
     LOG(IndexedDB, "UniqueIDBDatabase::invokeOperationAndTransactionTimer()");
@@ -1144,7 +1170,7 @@ void UniqueIDBDatabase::operationAndTransactionTimerFired()
     // Assuming it is not ephemeral, the server should now close it to free up resources.
     if (!m_backingStoreIsEphemeral && !isCurrentlyInUse()) {
         ASSERT(m_pendingTransactions.isEmpty());
-        ASSERT(m_inProgressTransactions.isEmpty());
+        ASSERT(!hasUnfinishedTransactions());
         m_server.closeUniqueIDBDatabase(*this);
         return;
     }
@@ -1223,7 +1249,7 @@ template<typename T> bool scopesOverlap(const T& aScopes, const Vector<uint64_t>
 RefPtr<UniqueIDBDatabaseTransaction> UniqueIDBDatabase::takeNextRunnableTransaction(bool& hadDeferredTransactions)
 {
     hadDeferredTransactions = false;
-    if (!m_backingStoreSupportsSimultaneousTransactions && !m_inProgressTransactions.isEmpty()) {
+    if (!m_backingStoreSupportsSimultaneousTransactions && hasUnfinishedTransactions()) {
         LOG(IndexedDB, "UniqueIDBDatabase::takeNextRunnableTransaction - Backing store only supports 1 transaction, and we already have 1");
         return nullptr;
     }
@@ -1279,10 +1305,11 @@ RefPtr<UniqueIDBDatabaseTransaction> UniqueIDBDatabase::takeNextRunnableTransact
     return currentTransaction;
 }
 
-void UniqueIDBDatabase::inProgressTransactionCompleted(const IDBResourceIdentifier& transactionIdentifier)
+void UniqueIDBDatabase::transactionCompleted(RefPtr<UniqueIDBDatabaseTransaction>&& transaction)
 {
-    auto transaction = m_inProgressTransactions.take(transactionIdentifier);
     ASSERT(transaction);
+    ASSERT(!m_inProgressTransactions.contains(transaction->info().identifier()));
+    ASSERT(!m_finishingTransactions.contains(transaction->info().identifier()));
 
     for (auto objectStore : transaction->objectStoreIdentifiers()) {
         if (!transaction->isReadOnly()) {
index 7fc63c8..af22728 100644 (file)
@@ -124,7 +124,7 @@ private:
     bool isVersionChangeInProgress();
 
     void activateTransactionInBackingStore(UniqueIDBDatabaseTransaction&);
-    void inProgressTransactionCompleted(const IDBResourceIdentifier&);
+    void transactionCompleted(RefPtr<UniqueIDBDatabaseTransaction>&&);
 
     // Database thread operations
     void deleteBackingStore(const IDBDatabaseIdentifier&);
@@ -176,11 +176,14 @@ private:
 
     bool hasAnyPendingCallbacks() const;
     bool isCurrentlyInUse() const;
+    bool hasUnfinishedTransactions() const;
 
     void invokeOperationAndTransactionTimer();
     void operationAndTransactionTimerFired();
     RefPtr<UniqueIDBDatabaseTransaction> takeNextRunnableTransaction(bool& hadDeferredTransactions);
 
+    bool prepareToFinishTransaction(UniqueIDBDatabaseTransaction&);
+
     IDBServer& m_server;
     IDBDatabaseIdentifier m_identifier;
     
@@ -211,6 +214,7 @@ private:
 
     Deque<RefPtr<UniqueIDBDatabaseTransaction>> m_pendingTransactions;
     HashMap<IDBResourceIdentifier, RefPtr<UniqueIDBDatabaseTransaction>> m_inProgressTransactions;
+    HashMap<IDBResourceIdentifier, RefPtr<UniqueIDBDatabaseTransaction>> m_finishingTransactions;
 
     // The keys into these sets are the object store ID.
     // These sets help to decide which transactions can be started and which must be deferred.