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

Issue 7677028: Make WeakPtr thread-safe, i.e. allow cross-thread copying of WeakPtr (Closed)

Created:
9 years, 4 months ago by no sievers
Modified:
9 years, 4 months ago
CC:
chromium-reviews, brettw-cc_chromium.org, ajwong
Visibility:
Public.

Description

Make WeakPtr thread-safe, i.e. allow cross-thread copying of WeakPtr and destruction on a thread other than the one where the original reference was created. The problem with the current implementation is modifying the flag pointer from WeakPtr::~WeakPtr which might happen on a different thread (potentially racing with somebody invalidating the flag on the original thread). For compatibility reasons, creating the initial reference attaches to the thread and governs the thread-safety checking logic with respect to checking a reference to be valid and invalidating it, which should both only be done on the same thread. BUG=82509 TEST=added unit tests Added memleak suppression: http://crbug.com/94345 TBR=timurrrr@chromium.org Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=98443

Patch Set 1 #

Total comments: 6

Patch Set 2 : address comments, add a unit test #

Total comments: 2

Patch Set 3 : address comments #

Total comments: 3

Patch Set 4 : add unit tests #

Patch Set 5 : remove a bit of unneeded code #

Total comments: 7

Patch Set 6 : address comments #

Patch Set 7 : add suppression #

Unified diffs Side-by-side diffs Delta from patch set Stats (+230 lines, -18 lines) Patch
M base/memory/weak_ptr.h View 1 2 3 6 chunks +15 lines, -8 lines 0 comments Download
M base/memory/weak_ptr.cc View 1 2 3 3 chunks +12 lines, -10 lines 0 comments Download
M base/memory/weak_ptr_unittest.cc View 1 2 3 4 5 3 chunks +193 lines, -0 lines 0 comments Download
M tools/valgrind/memcheck/suppressions.txt View 1 2 3 4 5 6 1 chunk +10 lines, -0 lines 0 comments Download

Messages

Total messages: 51 (0 generated)
darin (slow to review)
Looks good, but I think we need some unit tests. Will, Daniel and I spent ...
9 years, 4 months ago (2011-08-18 18:29:06 UTC) #1
willchan no longer on Chromium
http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.cc File base/memory/weak_ptr.cc (right): http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.cc#newcode49 base/memory/weak_ptr.cc:49: if (!flag_ || flag_->HasOneRef()) On 2011/08/18 18:29:06, darin wrote: ...
9 years, 4 months ago (2011-08-18 18:49:52 UTC) #2
willchan no longer on Chromium
http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.h File base/memory/weak_ptr.h (right): http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.h#newcode89 base/memory/weak_ptr.h:89: WeakReference(scoped_refptr<Flag> flag); On 2011/08/18 18:29:06, darin wrote: > nit: ...
9 years, 4 months ago (2011-08-18 19:14:12 UTC) #3
darin (slow to review)
On 2011/08/18 18:49:52, willchan wrote: > http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.cc > File base/memory/weak_ptr.cc (right): > > http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.cc#newcode49 > ...
9 years, 4 months ago (2011-08-18 19:17:40 UTC) #4
darin (slow to review)
On 2011/08/18 19:14:12, willchan wrote: > http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.h > File base/memory/weak_ptr.h (right): > > http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.h#newcode89 > ...
9 years, 4 months ago (2011-08-18 19:18:39 UTC) #5
willchan no longer on Chromium
On 2011/08/18 19:17:40, darin wrote: > On 2011/08/18 18:49:52, willchan wrote: > > http://codereview.chromium.org/7677028/diff/1/base/memory/weak_ptr.cc > ...
9 years, 4 months ago (2011-08-18 19:33:07 UTC) #6
akalin
On Thu, Aug 18, 2011 at 11:29 AM, <darin@chromium.org> wrote: > Looks good, but I ...
9 years, 4 months ago (2011-08-18 19:40:35 UTC) #7
willchan no longer on Chromium
On Thu, Aug 18, 2011 at 12:40 PM, Fred Akalin <akalin@chromium.org> wrote: > On Thu, ...
9 years, 4 months ago (2011-08-18 19:45:23 UTC) #8
akalin
On Thu, Aug 18, 2011 at 12:45 PM, William Chan (陈智昌) <willchan@chromium.org> wrote: > Are ...
9 years, 4 months ago (2011-08-18 19:48:50 UTC) #9
darin (slow to review)
On Thu, Aug 18, 2011 at 12:47 PM, Fred Akalin <akalin@chromium.org> wrote: > On Thu, ...
9 years, 4 months ago (2011-08-18 19:54:00 UTC) #10
darin (slow to review)
On Thu, Aug 18, 2011 at 12:33 PM, <willchan@chromium.org> wrote: > On 2011/08/18 19:17:40, darin ...
9 years, 4 months ago (2011-08-18 19:59:06 UTC) #11
akalin
On Thu, Aug 18, 2011 at 12:53 PM, Darin Fisher <darin@chromium.org> wrote: > The WeakPtr ...
9 years, 4 months ago (2011-08-18 19:59:16 UTC) #12
willchan no longer on Chromium
On Thu, Aug 18, 2011 at 12:59 PM, Darin Fisher <darin@chromium.org> wrote: > > > ...
9 years, 4 months ago (2011-08-18 20:04:30 UTC) #13
darin (slow to review)
On Thu, Aug 18, 2011 at 1:04 PM, William Chan (陈智昌) <willchan@chromium.org>wrote: > On Thu, ...
9 years, 4 months ago (2011-08-18 20:08:11 UTC) #14
darin (slow to review)
On Thu, Aug 18, 2011 at 12:59 PM, Fred Akalin <akalin@chromium.org> wrote: > On Thu, ...
9 years, 4 months ago (2011-08-18 20:10:19 UTC) #15
no sievers
I think I addressed all the comments. I added one unit tests, but am still ...
9 years, 4 months ago (2011-08-18 20:18:31 UTC) #16
no sievers
On 2011/08/18 19:59:16, akalin wrote: > On Thu, Aug 18, 2011 at 12:53 PM, Darin ...
9 years, 4 months ago (2011-08-18 20:25:37 UTC) #17
darin (slow to review)
LGTM http://codereview.chromium.org/7677028/diff/5002/base/memory/weak_ptr.cc File base/memory/weak_ptr.cc (right): http://codereview.chromium.org/7677028/diff/5002/base/memory/weak_ptr.cc#newcode49 base/memory/weak_ptr.cc:49: if (!flag_ || !HasRefs()) HasRefs() already null checks ...
9 years, 4 months ago (2011-08-18 20:29:52 UTC) #18
no sievers
Will, Fred?
9 years, 4 months ago (2011-08-18 22:33:25 UTC) #19
willchan no longer on Chromium
I'm about to catch my transfer flight, so I don't have time to look. Darin's ...
9 years, 4 months ago (2011-08-18 22:39:10 UTC) #20
ajwong
will...if you're on vacation, you're the worst vacation taker evar. On Thu, Aug 18, 2011 ...
9 years, 4 months ago (2011-08-18 22:40:43 UTC) #21
akalin
LGTM So just to confirm, we've all agreed that WeakPtrs will be valid to pass ...
9 years, 4 months ago (2011-08-18 22:51:54 UTC) #22
no sievers
I think that makes sense, since you cannot do anything with an object anyway if ...
9 years, 4 months ago (2011-08-18 23:00:43 UTC) #23
awong
Sorry for coming late to the game. Looks like a good improvement. Yay! I have ...
9 years, 4 months ago (2011-08-18 23:58:19 UTC) #24
darin (slow to review)
From my phone... I thought Will had already added some of these tests. On Aug ...
9 years, 4 months ago (2011-08-19 03:22:29 UTC) #25
ajwong
Oh you're right! SingleThreaded1 and SingleThreaded2 do indeed cover a couple of these cases. I ...
9 years, 4 months ago (2011-08-19 05:09:28 UTC) #26
no sievers
Don't all your suggestions involve deleting either weak reference owners or pointers on the 'off ...
9 years, 4 months ago (2011-08-19 18:21:48 UTC) #27
no sievers
Also, one more thing, consider this case: 1) Thread A creates one or more weak ...
9 years, 4 months ago (2011-08-19 19:45:08 UTC) #28
darin (slow to review)
On Fri, Aug 19, 2011 at 12:45 PM, <sievers@chromium.org> wrote: > Also, one more thing, ...
9 years, 4 months ago (2011-08-19 21:27:31 UTC) #29
ajwong
On Fri, Aug 19, 2011 at 2:27 PM, Darin Fisher <darin@chromium.org> wrote: > > > ...
9 years, 4 months ago (2011-08-19 21:41:21 UTC) #30
darin (slow to review)
On Fri, Aug 19, 2011 at 2:40 PM, Albert Wong (王重傑) <ajwong@google.com>wrote: > On Fri, ...
9 years, 4 months ago (2011-08-19 23:21:20 UTC) #31
no sievers
I have added the unit tests you suggested (the last 3), while I combined the ...
9 years, 4 months ago (2011-08-20 00:10:30 UTC) #32
awong
LGTM w/ style nits http://codereview.chromium.org/7677028/diff/16001/base/memory/weak_ptr_unittest.cc File base/memory/weak_ptr_unittest.cc (right): http://codereview.chromium.org/7677028/diff/16001/base/memory/weak_ptr_unittest.cc#newcode48 base/memory/weak_ptr_unittest.cc:48: Start(); This constitutes work in ...
9 years, 4 months ago (2011-08-23 00:11:15 UTC) #33
Timur Iskhodzhanov
Are you aware this change has introduced (or uncovered?) some leaks that made the memory ...
9 years, 4 months ago (2011-08-23 08:20:58 UTC) #34
cbentzel
He replied personally. I reverted this morning. On Tue, Aug 23, 2011 at 4:20 AM, ...
9 years, 4 months ago (2011-08-23 12:16:52 UTC) #35
Timur Iskhodzhanov
Thanks!
9 years, 4 months ago (2011-08-23 12:51:46 UTC) #36
no sievers
All the supposed leaks look the same: The leak is a WeakReference::Flag allocated through ObserverListBase::AsWeakPtr ...
9 years, 4 months ago (2011-08-23 18:01:10 UTC) #37
Timur Iskhodzhanov
On Tue, Aug 23, 2011 at 10:01 PM, <sievers@chromium.org> wrote: > Also, interesting that this ...
9 years, 4 months ago (2011-08-24 05:40:26 UTC) #38
no sievers
Ok, I have investigated the problem with the unit_tests. It is indeed a more-or-less bogus ...
9 years, 4 months ago (2011-08-24 23:08:30 UTC) #39
awong
It sounds to me like a DEATH test should somehow not be heapchecked. Am I ...
9 years, 4 months ago (2011-08-24 23:15:48 UTC) #40
ajwong
(btw, nice job tracking that down...that is a tricky sequence) On Wed, Aug 24, 2011 ...
9 years, 4 months ago (2011-08-24 23:16:20 UTC) #41
no sievers
Yes, I'd also think the child process which is just created to be killed should ...
9 years, 4 months ago (2011-08-24 23:21:50 UTC) #42
awong
On Wed, Aug 24, 2011 at 4:21 PM, <sievers@chromium.org> wrote: > > Yes, I'd also ...
9 years, 4 months ago (2011-08-24 23:33:58 UTC) #43
no sievers
http://code.google.com/p/chromium/issues/detail?id=94174 filed. I'll try to work around the unit_tests leak in the meantime and will ...
9 years, 4 months ago (2011-08-24 23:56:15 UTC) #44
Timur Iskhodzhanov
> It sounds to me like a DEATH test should somehow not be heapchecked. Why? ...
9 years, 4 months ago (2011-08-25 07:48:44 UTC) #45
awong
On 2011/08/25 07:48:44, Timur Iskhodzhanov wrote: > > It sounds to me like a DEATH ...
9 years, 4 months ago (2011-08-25 21:10:23 UTC) #46
awong
LGTM I think I've convinced myself that this isn't nearly as general as it looks ...
9 years, 4 months ago (2011-08-26 00:50:23 UTC) #47
no sievers
For reference, here is a demangled stack: operator new(unsigned int) base::internal::WeakReferenceOwner::GetRef() const base::SupportsWeakPtr<ObserverListBase<MessageLoop::TaskObserver>>::AsWeakPtr() ObserverListBase<MessageLoop::TaskObserver>::Iterator::Iterator(ObserverListBase<MessageLoop::TaskObserver>&) MessageLoop::RunTask(MessageLoop::PendingTask ...
9 years, 4 months ago (2011-08-26 01:06:29 UTC) #48
Timur Iskhodzhanov
On 2011/08/25 21:10:23, awong wrote: > On 2011/08/25 07:48:44, Timur Iskhodzhanov wrote: > > > ...
9 years, 4 months ago (2011-08-26 09:56:28 UTC) #49
awong
On Fri, Aug 26, 2011 at 2:56 AM, <timurrrr@chromium.org> wrote: > On 2011/08/25 21:10:23, awong ...
9 years, 4 months ago (2011-08-26 15:50:23 UTC) #50
no sievers
9 years, 4 months ago (2011-08-26 17:42:26 UTC) #51
> What I'm saying is that Valgrind is not treating an allocated memory as a leak
> if there are pointers referencing it.

Maybe that explains why my naive test in http://crbug.com/94174 does not show a
leak, while this issue does. Should probably continue the discussion in that
bug. 

Since this is a 10-headed hydra (i.e. there are many ui tests apparently with a
similar msg loop leak and the same leakl stack), and it is really not an issue
with this change itself at all, I added a suppression for now. Could you
double-check it?

Powered by Google App Engine
This is Rietveld 408576698