On Sun, Sep 23, 2018 at 7:54 PM 'Kenton Varda' via v8-users <
v8-users@googlegroups.com> wrote:

> Hi v8-users,
>
> I'm trying to understand how to correctly instrument my wrappers for
> multi-threaded garbage collection, and it seems I'm doing something wrong.
> Currently, my code seems to work only in --predictable mode
> (single-threaded), but I'd like to take advantage of GC in a background
> thread.
>
> When in multi-threaded mode, I observe that occasionally, when V8 traces
> into a particular wrapper, the weak handles held by that wrapper have
> already been collected. The tracing is happening, but V8 seemingly
> prematurely collects some handles before I get a chance to mark them.
>
>
fyi: The cross-component tracing used for EmbedderHeapTracer always goes
through the main thread, i.e., none of its methods are called concurrently.


> Here are the things I've done:
>
> Per-object:
>
>    - When I create a new wrapper object, I give it two internal fields
>    set to the native object pointer and a tracing callback function.
>    - The tracing callback, when run, invokes RegisterExternalReference()
>    on each persistent handle reachable through the native object.
>    - Once the internal pointers are set, I invoke SetWeak() (with no
>    arguments) on all handles reachable through the native object.
>
> Globally:
>
>    - I call AddGCPrologueCallback() to register a callback for
>    v8::kGCTypeScavenge.
>    - In the scavenge callback, I call VisitWeakHandles() with a visitor
>    that calls MarkActive() on every handle.
>
>
Looks like Isolate::VisitWeakHandles
<https://cs.chromium.org/chromium/src/v8/src/api.cc?q=Isolate::VisitWeakHandles&sq=package:chromium&g=0&l=8852>
only iterates over weak handles with a non-zero class id. Any chance that
this one is still 0?


>
>    - I call SetEmbedderHeapTracer() to register my own heap tracer
>    implementation.
>    - In the heap tracer's RegisterV8References() method, I invoke each
>    tracing callback using the pair of internal pointers (which calls
>    RegisterExternalReference() on reachable weak handles, as described above).
>    - Currently, I don't do anything in AdvanceTracing() -- I complete all
>    tracing before RegisterV8References() returns. (It appears this is what
>    Blink does in its unified_heap_controller.cc, so I followed the example.)
>    However, if I instead only save a copy of the pointer list in
>    RegisterV8References() and then do the tracing later in AdvanceTracing(),
>    then I observe problems with prematurely-collected handles more often. I'd
>    guess this is because the underlying problem is a race condition, in which
>    another thread is collecting those handles before I get a chance to trace
>    them.
>
>
The symptom described here hints to the Scavenger collecting objects. From
the above description it looks like you wanted to preserve those objects on
Scavenge.

Some ideas:
- Maybe class id is 0 (see above)?
- Maybe the handle are not SetWeak
<https://cs.chromium.org/chromium/src/v8/include/v8.h?type=cs&q=v8::PersistentBase::SetWeak&sq=package:chromium&g=0&l=514>
immediately but only after some time where a Scavenge could've happened?


>
>    - I don't do anything in any of the other EmbedderHeapTracer callbacks.
>
> Anything I'm missing here?
>
> I haven't been able to find any documentation on how to use these
> interfaces. Please let me know if there are docs I missed.
>
>
There's no additional documentation and you already found the existing
implementations for wrapper tracing
<https://cs.chromium.org/chromium/src/third_party/blink/renderer/platform/bindings/script_wrappable_marking_visitor.h?sq=package:chromium&dr&q=blink::ScriptWrappableMarking&g=0&l=32>
and unified heap garbage collections
<https://cs.chromium.org/chromium/src/third_party/blink/renderer/platform/heap/unified_heap_controller.h?sq=package:chromium&dr&q=blink::UnifiedHeap&g=0&l=31>
.

The Scavenger quirks are definitely something we should document though.

Cheers, -Michael

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to