Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Issue 5691839784943616: Change the way persistent handles are desctructed. Add tests. (Closed)

Created:
July 10, 2014, 11:44 p.m. by Oleksandr
Modified:
Oct. 23, 2014, 10:48 a.m.
Visibility:
Public.

Description

This introduces a correct way of destructing V8's persistent handles. We should rely on GC to release them when there are no more refferences to them. With this fix occasional crashes in libadblockplus seem to be gone. However this reveals another bug we have in libadblockplus, which leaks memory pretty bad. Apparently GC never actually cleans persistent handles with primitive types. Those should never be created (https://groups.google.com/forum/#!msg/v8-users/616ZM3UWh2k/W3QhdhhK6XQJ) The two tests added here more or less prove this point. First (PersistentWeakHandleDisposalComplex) succeedes, and the second one (PersistentWeakHandleDisposalPrimitive) fails. This means that we might need to rewrite V8ValueHandler to treat primitive types differently.

Patch Set 1 #

Total comments: 20
Unified diffs Side-by-side diffs Delta from patch set Stats (+71 lines, -7 lines) Patch
M include/AdblockPlus/JsValue.h View 1 chunk +0 lines, -1 line 0 comments Download
M include/AdblockPlus/V8ValueHolder.h View 2 chunks +20 lines, -5 lines 20 comments Download
M test/JsValue.cpp View 3 chunks +51 lines, -1 line 0 comments Download

Messages

Total messages: 9
Oleksandr
July 10, 2014, 11:53 p.m. (2014-07-10 23:53:59 UTC) #1
Wladimir Palant
http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/include/AdblockPlus/V8ValueHolder.h File include/AdblockPlus/V8ValueHolder.h (right): http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/include/AdblockPlus/V8ValueHolder.h#newcode40 include/AdblockPlus/V8ValueHolder.h:40: value = NULL; Note that we generally use 0 ...
July 11, 2014, 6:19 a.m. (2014-07-11 06:19:42 UTC) #2
Felix Dahlke
I also don't see how these changes change anything - the copy constructor looks like ...
July 11, 2014, 9:22 a.m. (2014-07-11 09:22:20 UTC) #3
Felix Dahlke
http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/include/AdblockPlus/V8ValueHolder.h File include/AdblockPlus/V8ValueHolder.h (right): http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/include/AdblockPlus/V8ValueHolder.h#newcode76 include/AdblockPlus/V8ValueHolder.h:76: this->value->MakeWeak(this->value, (v8::WeakReferenceCallback)&AdblockPlus::V8ValueHolder<T>::ABPDisposeCallback); On 2014/07/11 09:22:20, Felix H. Dahlke wrote: ...
July 11, 2014, 9:24 a.m. (2014-07-11 09:24:16 UTC) #4
sergei
I went through the code of v8, there is indeed a bug. When the copy-ctr ...
July 22, 2014, 4:25 p.m. (2014-07-22 16:25:38 UTC) #5
Wladimir Palant
http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/include/AdblockPlus/V8ValueHolder.h File include/AdblockPlus/V8ValueHolder.h (right): http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/include/AdblockPlus/V8ValueHolder.h#newcode40 include/AdblockPlus/V8ValueHolder.h:40: value = NULL; On 2014/07/22 16:25:38, sergei wrote: > ...
July 23, 2014, 11:21 a.m. (2014-07-23 11:21:29 UTC) #6
sergei
The idea how it's done in the changeset looks completely correct for me, but unfortunately ...
Oct. 23, 2014, 8:58 a.m. (2014-10-23 08:58:00 UTC) #7
Oleksandr
I will close this, if no one objects. On 2014/10/23 08:58:00, sergei wrote: > The ...
Oct. 23, 2014, 10:44 a.m. (2014-10-23 10:44:56 UTC) #8
Oleksandr
Oct. 23, 2014, 10:48 a.m. (2014-10-23 10:48:23 UTC) #9
I had unpublished draft comments pending here, which I didn't publish since
there was no clear solution anyway. I'll just publish them now for future
reference.

http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/incl...
File include/AdblockPlus/V8ValueHolder.h (right):

http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/incl...
include/AdblockPlus/V8ValueHolder.h:76: this->value->MakeWeak(this->value,
(v8::WeakReferenceCallback)&AdblockPlus::V8ValueHolder<T>::ABPDisposeCallback);
I think ideally we should let V8 dispose the handle during GC, since it can
probably optimize the process and it's safer. Assuming that there are no other
instances referencing current handle is a bit of a wishful thinking. What if
somewhere in the code the V8ValueHolder will go out of scope, but the handle
itself would still be available? Like:

v8::Handle<v8::Value> someValue = ...;
if (true)
{
  V8ValueHolder vh(isolate, someValue);
}
// This would be problematic
someHandle->Foo();

However, seeing that it raises a whole bunch of problems with primitive types, I
guess it's better to leave that change for future.

On 2014/07/11 06:19:42, Wladimir Palant wrote:
> So, why is calling Dispose() an issue here? This V8ValueHolder instance is
> supposed to be the only place with a reference to this particular handle.
> 
> I see no copy assignment operator here, we should add it (rule of three).
> However, it doesn't see like any code is triggering it. My guess is that the
> real issue is the copy constructor declared above. It simply copies the v8
> handle into a new V8ValueHolder instance. As a result, that handle is owned by
> two instances and both will attempt to destroy it. We should create a new
handle
> there instead.

http://codereview.adblockplus.org/5691839784943616/diff/5629499534213120/incl...
include/AdblockPlus/V8ValueHolder.h:105: v8::Persistent<T>* value;
The reason of a raw pointer here is that we want to delete it in a callback, not
in destructor.

On 2014/07/11 09:22:20, Felix H. Dahlke wrote:
> On 2014/07/11 06:19:42, Wladimir Palant wrote:
> > I don't consider using raw pointers a good idea - as you've probably noticed
> for
> > this change, they add a fair amount of additional code and a fair amount of
> > additional potential failure points. And I don't see anything that's wrong
> with
> > auto_ptr here.
> 
> Agreed, auto_ptr is evidently what we want here: Defaults to 0, is deleted
> automatically invoking the correct destructor. It doesn't look like this
> behaviour has changed with your changes.

Powered by Google App Engine
This is Rietveld