Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Issue 15688010: Detach the RulesRegistryWithCache::weak_ptr_factory_ on destruction. (Closed)

Created:
7 years, 6 months ago by vabr (Chromium)
Modified:
7 years, 6 months ago
Reviewers:
Jeffrey Yasskin, battre
CC:
chromium-reviews, Aaron Boodman, chromium-apps-reviews_chromium.org
Visibility:
Public.

Description

Detach the RulesRegistryWithCache::weak_ptr_factory_ on destruction. In tests I observed that RulesRegistryWithCache can be destructed on the UI thread. The destructor of the registry does not check this, but if the registry init ialised the WeakPtrFactory for itself, that factory expects to be destructed on the registry's thread. In case of WebRequestRulesRegistry (a subclass of RulesRegistryWithCache), this thread is IO, as opposed to the UI thread where the Rules RegistryService lives. Here I propose that the WeakPtrFactory should not care where it is destructed: - The only piece of code from where the weak pointer to the registry is used is the UI part (running entirely on a UI thread). - The UI part is owned by the RulesRegistryService (ui_parts_of_registries_). - The registry is referenced by RulesRegistryService, i.e., it lives at least until the service's (and UI parts') destruction. Based on the above, I don't think there is a moment when the registry would be destructed, and still there would be a pending task holding a weak pointer to it. Such task would need to be waiting on the UI thread, and coming from the UI part, but the UI part would get destructed (on UI) before that task could be scheduled. Destructing the UI part would invalidate the task (which would contain a weak pointer to the UI part as |this|). BUG=218451 Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=203710

Patch Set 1 #

Patch Set 2 : Always initialise the factory with |this| #

Total comments: 2

Patch Set 3 : Make sure WRRR is deleted by ExtensionWebRequestEventRouter #

Total comments: 4
Unified diffs Side-by-side diffs Delta from patch set Stats (+7 lines, -0 lines) Patch
M chrome/browser/extensions/api/declarative/rules_registry_service.cc View 1 2 1 chunk +7 lines, -0 lines 4 comments Download

Messages

Total messages: 14 (0 generated)
vabr (Chromium)
Hi Jeffrey, This is a follow-up to my recent refactoring under your supervision. Currently, a ...
7 years, 6 months ago (2013-05-28 16:04:05 UTC) #1
vabr (Chromium)
Patch set 2: In response to the failing tests -- I can only detach a ...
7 years, 6 months ago (2013-05-28 16:39:52 UTC) #2
vabr (Chromium)
Another fix: jeffreyc->jyasskin for reviewer! :) Sorry guys for that autocomplete-typo. Vaclav
7 years, 6 months ago (2013-05-28 16:47:23 UTC) #3
Jeffrey Yasskin
https://codereview.chromium.org/15688010/diff/6001/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc File chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc (right): https://codereview.chromium.org/15688010/diff/6001/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc#newcode220 chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc:220: weak_ptr_factory_.DetachFromThread(); This isn't safe, according to the thread-safety rules ...
7 years, 6 months ago (2013-05-28 22:37:10 UTC) #4
Jeffrey Yasskin
https://codereview.chromium.org/15688010/diff/6001/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc File chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc (right): https://codereview.chromium.org/15688010/diff/6001/chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc#newcode220 chrome/browser/extensions/api/declarative/rules_registry_with_cache.cc:220: weak_ptr_factory_.DetachFromThread(); On 2013/05/28 22:37:10, Jeffrey Yasskin wrote: > This ...
7 years, 6 months ago (2013-05-28 22:48:49 UTC) #5
vabr (Chromium)
Jeffrey, thanks for your answer, this is the direction to what I needed to be ...
7 years, 6 months ago (2013-05-29 14:22:31 UTC) #6
Jeffrey Yasskin
I want Dominic to double-check that releasing the RulesRegistries earlier is ok, but this short-term ...
7 years, 6 months ago (2013-05-29 17:06:33 UTC) #7
vabr (Chromium)
On 2013/05/29 17:06:33, Jeffrey Yasskin wrote: > I want Dominic to double-check that releasing the ...
7 years, 6 months ago (2013-05-31 07:09:35 UTC) #8
battre
https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions/api/declarative/rules_registry_service.cc File chrome/browser/extensions/api/declarative/rules_registry_service.cc (right): https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions/api/declarative/rules_registry_service.cc#newcode77 chrome/browser/extensions/api/declarative/rules_registry_service.cc:77: profile_, scoped_refptr<WebRequestRulesRegistry>(NULL))); Wouldn't the correct way of solving this ...
7 years, 6 months ago (2013-06-03 10:22:14 UTC) #9
vabr (Chromium)
https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions/api/declarative/rules_registry_service.cc File chrome/browser/extensions/api/declarative/rules_registry_service.cc (right): https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions/api/declarative/rules_registry_service.cc#newcode77 chrome/browser/extensions/api/declarative/rules_registry_service.cc:77: profile_, scoped_refptr<WebRequestRulesRegistry>(NULL))); On 2013/06/03 10:22:15, battre wrote: > Wouldn't ...
7 years, 6 months ago (2013-06-03 10:45:45 UTC) #10
battre
LGTM https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions/api/declarative/rules_registry_service.cc File chrome/browser/extensions/api/declarative/rules_registry_service.cc (right): https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions/api/declarative/rules_registry_service.cc#newcode77 chrome/browser/extensions/api/declarative/rules_registry_service.cc:77: profile_, scoped_refptr<WebRequestRulesRegistry>(NULL))); On 2013/06/03 10:45:45, vabr (Chromium) wrote: ...
7 years, 6 months ago (2013-06-03 11:42:42 UTC) #11
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-status.appspot.com/cq/vabr@chromium.org/15688010/24001
7 years, 6 months ago (2013-06-03 11:50:23 UTC) #12
commit-bot: I haz the power
Change committed as 203710
7 years, 6 months ago (2013-06-03 14:05:24 UTC) #13
Jeffrey Yasskin
7 years, 6 months ago (2013-06-03 16:12:16 UTC) #14
Message was sent while issue was closed.
https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions...
File chrome/browser/extensions/api/declarative/rules_registry_service.cc
(right):

https://codereview.chromium.org/15688010/diff/24001/chrome/browser/extensions...
chrome/browser/extensions/api/declarative/rules_registry_service.cc:77:
profile_, scoped_refptr<WebRequestRulesRegistry>(NULL)));
On 2013/06/03 10:22:15, battre wrote:
> Wouldn't the correct way of solving this be:
> 
> void HoldReferenceUntilCalled(scoped_refptr<WebRequestRulesRegistry>
> rules_registry) {
>   // Do nothing. The purpose of this function is just to hold the ref-counted
> reference until
>   // it is called. This allows us to destruct the object on the right thread.
> }
> 
> void RulesRegistryService::Shutdown() {
>   RulesRegistryMap temp_rule_registries;
>   std::swap(&temp_rule_registries, &rule_registries_);
>   content::BrowserThread::PostTask(
>       content::BrowserThread::IO, FROM_HERE,
>       base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
>       profile_, scoped_refptr<WebRequestRulesRegistry>(NULL)));
>   for (rule registry : temp_rule_registries) {
>      post task HoldReferenceUntilCalled on owner thread of rule registry
>   }
> }

The above code is subtly incorrect because it's possible for any of those posted
tasks to be executed before Shutdown() finishes, or even before PostTask
returns, in which case the reference would be destroyed on the current thread
anyway.

To make this correct, if we had move semantics for Bind(), we could ensure the
reference was transferred over to the other thread instead of copied (we can
even do enough of this in C++98), or we could manually call AddRef in this
function and Release on the raw pointer in HoldReferenceUntilCalled. But I think
it's cleaner to move toward singly-owned RulesRegistries anyway instead of
refcounting them.

Powered by Google App Engine
This is Rietveld 408576698