Title: [265283] trunk/Source/WebCore
Revision
265283
Author
[email protected]
Date
2020-08-05 04:03:08 -0700 (Wed, 05 Aug 2020)

Log Message

RegistrationDatabase::openSQLiteDatabase can spin
https://bugs.webkit.org/show_bug.cgi?id=215120
<rdar://problem/64850347>

Reviewed by Geoffrey Garen.

In case we fail opening the database when importing, we delete the database, the file and retry.
We do not need to retry in that case since there is nothing to import. Update the code to proceed with sending an import failure to SWServer.

In case of pushing changes to the database, it might be useful to retry but it is best to do this asynchronously.
For instance, there might be a script deleting the files/folders where is stored the database.
In that case, we check the result of pushing changes.
If they succeed, we call the completion handler and stop.
Otherwise, we will retry pushing the changes if changes were not pushed again in the meantime.
We introduce a push counter for that purpose.

* workers/service/server/RegistrationDatabase.cpp:
(WebCore::RegistrationDatabase::postTaskToWorkQueue):
(WebCore::RegistrationDatabase::openSQLiteDatabase):
(WebCore::RegistrationDatabase::importRecordsIfNecessary):
(WebCore::RegistrationDatabase::pushChanges):
(WebCore::RegistrationDatabase::schedulePushChanges):
(WebCore::RegistrationDatabase::doPushChanges):
* workers/service/server/RegistrationDatabase.h:
* workers/service/server/SWServer.cpp:
(WebCore::SWServer::registrationStoreDatabaseFailedToOpen):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (265282 => 265283)


--- trunk/Source/WebCore/ChangeLog	2020-08-05 08:33:35 UTC (rev 265282)
+++ trunk/Source/WebCore/ChangeLog	2020-08-05 11:03:08 UTC (rev 265283)
@@ -1,5 +1,34 @@
 2020-08-05  Youenn Fablet  <[email protected]>
 
+        RegistrationDatabase::openSQLiteDatabase can spin
+        https://bugs.webkit.org/show_bug.cgi?id=215120
+        <rdar://problem/64850347>
+
+        Reviewed by Geoffrey Garen.
+
+        In case we fail opening the database when importing, we delete the database, the file and retry.
+        We do not need to retry in that case since there is nothing to import. Update the code to proceed with sending an import failure to SWServer.
+
+        In case of pushing changes to the database, it might be useful to retry but it is best to do this asynchronously.
+        For instance, there might be a script deleting the files/folders where is stored the database.
+        In that case, we check the result of pushing changes.
+        If they succeed, we call the completion handler and stop.
+        Otherwise, we will retry pushing the changes if changes were not pushed again in the meantime.
+        We introduce a push counter for that purpose.
+
+        * workers/service/server/RegistrationDatabase.cpp:
+        (WebCore::RegistrationDatabase::postTaskToWorkQueue):
+        (WebCore::RegistrationDatabase::openSQLiteDatabase):
+        (WebCore::RegistrationDatabase::importRecordsIfNecessary):
+        (WebCore::RegistrationDatabase::pushChanges):
+        (WebCore::RegistrationDatabase::schedulePushChanges):
+        (WebCore::RegistrationDatabase::doPushChanges):
+        * workers/service/server/RegistrationDatabase.h:
+        * workers/service/server/SWServer.cpp:
+        (WebCore::SWServer::registrationStoreDatabaseFailedToOpen):
+
+2020-08-05  Youenn Fablet  <[email protected]>
+
         SWServerJobQueue::didResolveRegistrationPromise should not assume its registration key relates to an existing worker
         https://bugs.webkit.org/show_bug.cgi?id=215123
         <rdar://problem/65096786>

Modified: trunk/Source/WebCore/workers/service/server/RegistrationDatabase.cpp (265282 => 265283)


--- trunk/Source/WebCore/workers/service/server/RegistrationDatabase.cpp	2020-08-05 08:33:35 UTC (rev 265282)
+++ trunk/Source/WebCore/workers/service/server/RegistrationDatabase.cpp	2020-08-05 11:03:08 UTC (rev 265283)
@@ -133,12 +133,13 @@
 {
     ASSERT(isMainThread());
 
+    ++m_pushCounter;
     m_workQueue->dispatch([protectedThis = makeRef(*this), task = WTFMove(task)]() mutable {
         task();
     });
 }
 
-void RegistrationDatabase::openSQLiteDatabase(const String& fullFilename)
+bool RegistrationDatabase::openSQLiteDatabase(const String& fullFilename)
 {
     ASSERT(!isMainThread());
     ASSERT(!m_database);
@@ -154,10 +155,7 @@
     if (!m_database->open(fullFilename)) {
         RELEASE_LOG_ERROR(ServiceWorker, "Failed to open Service Worker registration database");
         m_database = nullptr;
-        callOnMainThread([this, protectedThis = makeRef(*this)] {
-            databaseFailedToOpen();
-        });
-        return;
+        return false;
     }
 
     // Disable threading checks. We always access the database from our serial WorkQueue. Such accesses
@@ -166,10 +164,9 @@
     m_database->disableThreadingChecks();
     
     auto doRecoveryAttempt = [&] {
-        // Delete the database and re-create it.
+        // Delete the database and files. We will try recreating it when flushing changes.
         m_database = nullptr;
         SQLiteFileSystem::deleteDatabaseFile(fullFilename);
-        openSQLiteDatabase(fullFilename);
     };
     
     String errorMessage = ensureValidRecordsTable();
@@ -176,7 +173,7 @@
     if (!errorMessage.isNull()) {
         RELEASE_LOG_ERROR(ServiceWorker, "ensureValidRecordsTable failed, reason: %" PUBLIC_LOG_STRING, errorMessage.utf8().data());
         doRecoveryAttempt();
-        return;
+        return false;
     }
     
     errorMessage = importRecords();
@@ -183,8 +180,9 @@
     if (!errorMessage.isNull()) {
         RELEASE_LOG_ERROR(ServiceWorker, "importRecords failed, reason: %" PUBLIC_LOG_STRING, errorMessage.utf8().data());
         doRecoveryAttempt();
-        return;
+        return false;
     }
+    return true;
 }
 
 void RegistrationDatabase::importRecordsIfNecessary()
@@ -191,11 +189,17 @@
 {
     ASSERT(!isMainThread());
 
-    if (FileSystem::fileExists(m_databaseFilePath))
-        openSQLiteDatabase(m_databaseFilePath);
+    if (FileSystem::fileExists(m_databaseFilePath)) {
+        if (!openSQLiteDatabase(m_databaseFilePath)) {
+            callOnMainThread([this, protectedThis = makeRef(*this)] {
+                databaseFailedToOpen();
+            });
+            return;
+        }
+    }
 
-    callOnMainThread([protectedThis = makeRef(*this)] {
-        protectedThis->databaseOpenedAndRecordsImported();
+    callOnMainThread([this, protectedThis = makeRef(*this)] {
+        databaseOpenedAndRecordsImported();
     });
 }
 
@@ -293,14 +297,26 @@
         else
             removedRegistrations.append(keyValue.key.isolatedCopy());
     }
+    schedulePushChanges(WTFMove(updatedRegistrations), WTFMove(removedRegistrations), WTFMove(completionHandler));
+}
 
-    postTaskToWorkQueue([this, updatedRegistrations = WTFMove(updatedRegistrations), removedRegistrations = WTFMove(removedRegistrations), completionHandler = WTFMove(completionHandler)]() mutable {
-        doPushChanges(updatedRegistrations, removedRegistrations);
-
-        if (!completionHandler)
-            return;
-
-        callOnMainThread(WTFMove(completionHandler));
+void RegistrationDatabase::schedulePushChanges(Vector<ServiceWorkerContextData>&& updatedRegistrations, Vector<ServiceWorkerRegistrationKey>&& removedRegistrations, CompletionHandler<void()>&& completionHandler)
+{
+    postTaskToWorkQueue([this, protectedThis = makeRef(*this), pushCounter = m_pushCounter, updatedRegistrations = WTFMove(updatedRegistrations), removedRegistrations = WTFMove(removedRegistrations), completionHandler = WTFMove(completionHandler)]() mutable {
+        bool success = doPushChanges(updatedRegistrations, removedRegistrations);
+        if (success) {
+            updatedRegistrations.clear();
+            removedRegistrations.clear();
+        }
+        callOnMainThread([this, protectedThis = WTFMove(protectedThis), success, pushCounter, updatedRegistrations = WTFMove(updatedRegistrations).isolatedCopy(), removedRegistrations = WTFMove(removedRegistrations).isolatedCopy(), completionHandler = WTFMove(completionHandler)]() mutable {
+            if (!success && (pushCounter + 1) == m_pushCounter) {
+                // We retry writing if no other change was pushed.
+                schedulePushChanges(WTFMove(updatedRegistrations), WTFMove(removedRegistrations), WTFMove(completionHandler));
+                return;
+            }
+            if (completionHandler)
+                completionHandler();
+        });
     });
 }
 
@@ -324,12 +340,12 @@
     });
 }
 
-void RegistrationDatabase::doPushChanges(const Vector<ServiceWorkerContextData>& updatedRegistrations, const Vector<ServiceWorkerRegistrationKey>& removedRegistrations)
+bool RegistrationDatabase::doPushChanges(const Vector<ServiceWorkerContextData>& updatedRegistrations, const Vector<ServiceWorkerRegistrationKey>& removedRegistrations)
 {
     if (!m_database) {
         openSQLiteDatabase(m_databaseFilePath);
         if (!m_database)
-            return;
+            return false;
     }
 
     SQLiteTransaction transaction(*m_database);
@@ -338,7 +354,7 @@
     SQLiteStatement sql(*m_database, "INSERT INTO Records VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"_s);
     if (sql.prepare() != SQLITE_OK) {
         RELEASE_LOG_ERROR(ServiceWorker, "Failed to prepare statement to store registration data into records table (%i) - %s", m_database->lastError(), m_database->lastErrorMsg());
-        return;
+        return false;
     }
 
     for (auto& registration : removedRegistrations) {
@@ -347,7 +363,7 @@
             || sql.bindText(1, registration.toDatabaseKey()) != SQLITE_OK
             || sql.step() != SQLITE_DONE) {
             RELEASE_LOG_ERROR(ServiceWorker, "Failed to remove registration data from records table (%i) - %s", m_database->lastError(), m_database->lastErrorMsg());
-            return;
+            return false;
         }
     }
 
@@ -376,7 +392,7 @@
             || sql.bindBlob(13, certificateInfoEncoder.buffer(), certificateInfoEncoder.bufferSize()) != SQLITE_OK
             || sql.step() != SQLITE_DONE) {
             RELEASE_LOG_ERROR(ServiceWorker, "Failed to store registration data into records table (%i) - %s", m_database->lastError(), m_database->lastErrorMsg());
-            return;
+            return false;
         }
     }
 
@@ -383,6 +399,7 @@
     transaction.commit();
 
     LOG(ServiceWorker, "Updated ServiceWorker registration database (%zu added/updated registrations and %zu removed registrations", updatedRegistrations.size(), removedRegistrations.size());
+    return true;
 }
 
 String RegistrationDatabase::importRecords()

Modified: trunk/Source/WebCore/workers/service/server/RegistrationDatabase.h (265282 => 265283)


--- trunk/Source/WebCore/workers/service/server/RegistrationDatabase.h	2020-08-05 08:33:35 UTC (rev 265282)
+++ trunk/Source/WebCore/workers/service/server/RegistrationDatabase.h	2020-08-05 11:03:08 UTC (rev 265283)
@@ -61,14 +61,15 @@
     
     String databaseDirectoryIsolatedCopy() const { return m_databaseDirectory.isolatedCopy(); }
 
+    void schedulePushChanges(Vector<ServiceWorkerContextData>&&, Vector<ServiceWorkerRegistrationKey>&&, CompletionHandler<void()>&&);
     void postTaskToWorkQueue(Function<void()>&&);
 
     // Methods to be run on the work queue.
-    void openSQLiteDatabase(const String& fullFilename);
+    bool openSQLiteDatabase(const String& fullFilename);
     String ensureValidRecordsTable();
     String importRecords();
     void importRecordsIfNecessary();
-    void doPushChanges(const Vector<ServiceWorkerContextData>&, const Vector<ServiceWorkerRegistrationKey>&);
+    bool doPushChanges(const Vector<ServiceWorkerContextData>&, const Vector<ServiceWorkerRegistrationKey>&);
     void doClearOrigin(const SecurityOrigin&);
 
     // Replies to the main thread.
@@ -81,6 +82,7 @@
     String m_databaseDirectory;
     String m_databaseFilePath;
     std::unique_ptr<SQLiteDatabase> m_database;
+    uint64_t m_pushCounter { 0 };
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/workers/service/server/SWServer.cpp (265282 => 265283)


--- trunk/Source/WebCore/workers/service/server/SWServer.cpp	2020-08-05 08:33:35 UTC (rev 265282)
+++ trunk/Source/WebCore/workers/service/server/SWServer.cpp	2020-08-05 11:03:08 UTC (rev 265283)
@@ -149,6 +149,7 @@
 
 void SWServer::registrationStoreDatabaseFailedToOpen()
 {
+    ASSERT(!m_importCompleted);
     if (!m_importCompleted)
         registrationStoreImportComplete();
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to