aboutsummaryrefslogtreecommitdiff
path: root/www/waterfox/files/patch-bug1408631
blob: ff58eaad4fbcac5a05b8d3030e0b82ec5a965fa8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
commit 48a63f5601f6
Author: dimi <dlee@mozilla.com>
Date:   Fri Oct 20 10:18:59 2017 +0800

    Bug 1408631 - Release SafeBrowsing lookupcache in worker thread while shutdown. r=francois
    
    MozReview-Commit-ID: HuPUyIDFLPX
    
    --HG--
    extra : rebase_source : d6e4f5bbcf96c97541792e23447f0810150c5ac9
---
 toolkit/components/url-classifier/Classifier.cpp   | 13 +++++---
 .../url-classifier/nsUrlClassifierDBService.cpp    | 37 ++++++++++++++++++++++
 .../url-classifier/nsUrlClassifierDBService.h      |  5 +++
 .../url-classifier/nsUrlClassifierProxies.cpp      | 10 ++++++
 .../url-classifier/nsUrlClassifierProxies.h        |  1 +
 5 files changed, 62 insertions(+), 4 deletions(-)

diff --git toolkit/components/url-classifier/Classifier.cpp toolkit/components/url-classifier/Classifier.cpp
index 404e31e2421e..9946469268fa 100644
--- toolkit/components/url-classifier/Classifier.cpp
+++ toolkit/components/url-classifier/Classifier.cpp
@@ -267,6 +267,8 @@ Classifier::Open(nsIFile& aCacheDirectory)
 void
 Classifier::Close()
 {
+  // Close will be called by PreShutdown, so it is important to note that
+  // things put here should not affect an ongoing update thread.
   DropStores();
 }
 
@@ -1428,10 +1430,8 @@ Classifier::UpdateCache(TableUpdate* aUpdate)
 LookupCache *
 Classifier::GetLookupCache(const nsACString& aTable, bool aForUpdate)
 {
-  if (aForUpdate) {
-    MOZ_ASSERT(NS_GetCurrentThread() == mUpdateThread,
-               "GetLookupCache(aForUpdate==true) can only be called on update thread.");
-  }
+  // GetLookupCache(aForUpdate==true) can only be called on update thread.
+  MOZ_ASSERT_IF(aForUpdate, NS_GetCurrentThread() == mUpdateThread);
 
   nsTArray<LookupCache*>& lookupCaches = aForUpdate ? mNewLookupCaches
                                                     : mLookupCaches;
@@ -1444,6 +1444,11 @@ Classifier::GetLookupCache(const nsACString& aTable, bool aForUpdate)
     }
   }
 
+  // We don't want to create lookupcache when shutdown is already happening.
+  if (nsUrlClassifierDBService::ShutdownHasStarted()) {
+    return nullptr;
+  }
+
   // TODO : Bug 1302600, It would be better if we have a more general non-main
   //        thread method to convert table name to protocol version. Currently
   //        we can only know this by checking if the table name ends with '-proto'.
diff --git toolkit/components/url-classifier/nsUrlClassifierDBService.cpp toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
index 78dbfaeaf046..e1a1be065aaf 100644
--- toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -817,6 +817,20 @@ nsUrlClassifierDBServiceWorker::CloseDb()
   return NS_OK;
 }
 
+nsresult
+nsUrlClassifierDBServiceWorker::PreShutdown()
+{
+  if (mClassifier) {
+    // Classifier close will release all lookup caches which may be a time-consuming job.
+    // See Bug 1408631.
+    mClassifier->Close();
+  }
+
+  // WARNING: nothing we put here should affect an ongoing update thread. When in doubt,
+  // put things in Shutdown() instead.
+  return NS_OK;
+}
+
 nsresult
 nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results)
 {
@@ -2427,6 +2441,13 @@ nsUrlClassifierDBService::Observe(nsISupports *aSubject, const char *aTopic,
   } else if (!strcmp(aTopic, "quit-application")) {
     // Tell the update thread to finish as soon as possible.
     gShuttingDownThread = true;
+
+    // The code in ::Shutdown() is run on a 'profile-before-change' event and
+    // ensures that objects are freed by blocking on this freeing.
+    // We can however speed up the shutdown time by using the worker thread to
+    // release, in an earlier event, any objects that cannot affect an ongoing
+    // update on the update thread.
+    PreShutdown();
   } else if (!strcmp(aTopic, "profile-before-change")) {
     gShuttingDownThread = true;
     Shutdown();
@@ -2437,6 +2458,22 @@ nsUrlClassifierDBService::Observe(nsISupports *aSubject, const char *aTopic,
   return NS_OK;
 }
 
+// Post a PreShutdown task to worker thread to release objects without blocking
+// main-thread. Notice that shutdown process may still be blocked by PreShutdown task
+// when ::Shutdown() is executed and synchronously waits for worker thread to finish
+// PreShutdown event.
+nsresult
+nsUrlClassifierDBService::PreShutdown()
+{
+  MOZ_ASSERT(XRE_IsParentProcess());
+
+  if (mWorkerProxy) {
+    mWorkerProxy->PreShutdown();
+  }
+
+  return NS_OK;
+}
+
 // Join the background thread if it exists.
 nsresult
 nsUrlClassifierDBService::Shutdown()
diff --git toolkit/components/url-classifier/nsUrlClassifierDBService.h toolkit/components/url-classifier/nsUrlClassifierDBService.h
index a4c5952e91bb..9d9671d9d8fa 100644
--- toolkit/components/url-classifier/nsUrlClassifierDBService.h
+++ toolkit/components/url-classifier/nsUrlClassifierDBService.h
@@ -140,6 +140,9 @@ private:
                      nsIUrlClassifierCallback* c,
                      bool forceCheck, bool *didCheck);
 
+  // Post an event to worker thread to release objects when receive 'quit-application'
+  nsresult PreShutdown();
+
   // Close db connection and join the background thread if it exists.
   nsresult Shutdown();
 
@@ -220,6 +223,8 @@ public:
   // Provide a way to forcibly close the db connection.
   nsresult GCC_MANGLING_WORKAROUND CloseDb();
 
+  nsresult GCC_MANGLING_WORKAROUND PreShutdown();
+
   nsresult CacheCompletions(CacheResultArray * aEntries);
 
   // Used to probe the state of the worker thread. When the update begins,
diff --git toolkit/components/url-classifier/nsUrlClassifierProxies.cpp toolkit/components/url-classifier/nsUrlClassifierProxies.cpp
index 1af4f9266aeb..38e294df7320 100644
--- toolkit/components/url-classifier/nsUrlClassifierProxies.cpp
+++ toolkit/components/url-classifier/nsUrlClassifierProxies.cpp
@@ -214,6 +214,16 @@ UrlClassifierDBServiceWorkerProxy::CloseDb()
   return DispatchToWorkerThread(r);
 }
 
+nsresult
+UrlClassifierDBServiceWorkerProxy::PreShutdown()
+{
+  nsCOMPtr<nsIRunnable> r =
+    NewRunnableMethod("nsUrlClassifierDBServiceWorker::PreShutdown",
+                      mTarget,
+                      &nsUrlClassifierDBServiceWorker::PreShutdown);
+  return DispatchToWorkerThread(r);
+}
+
 nsresult
 UrlClassifierDBServiceWorkerProxy::CacheCompletions(CacheResultArray * aEntries)
 {
diff --git toolkit/components/url-classifier/nsUrlClassifierProxies.h toolkit/components/url-classifier/nsUrlClassifierProxies.h
index f8cf5229e1ca..39cf47f837a2 100644
--- toolkit/components/url-classifier/nsUrlClassifierProxies.h
+++ toolkit/components/url-classifier/nsUrlClassifierProxies.h
@@ -229,6 +229,7 @@ public:
 
   nsresult OpenDb();
   nsresult CloseDb();
+  nsresult PreShutdown();
 
   nsresult CacheCompletions(mozilla::safebrowsing::CacheResultArray * aEntries);