New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 671193 link

Starred by 1 user

Issue metadata

Status: Fixed
Owner:
Closed: Dec 2016
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: All
Pri: 1
Type: Bug



Sign in to add a comment

heap allocator usage is initialized on a different thread than memory-infra heap profiler

Project Member Reported by primiano@chromium.org, Dec 5 2016

Issue description

We just realized that the https://codereview.chromium.org/2386123003/ broke the memory-infra heap profiler on Android (but I think also on Linux and Windows) on non-official builds.

Fortunately it's really a minor thing, the breakage is simply hitting the paranoid   DCHECK(CalledOnValidThread()) in base/allocator/allocator_shim.cc when calling InsertAllocatorDispatch.

The crash happens when building in debug (or dcheck_always_on=true). Essentially the following is happening:
The aforementioned CL introduced the following change:

buildflag(ENABLE_MEMORY_TASK_PROFILER) == enable_memory_task_profiler ==  use_experimental_allocator_shim && !is_official_build
Which is true by default on non-official builds of all platforms % OSX/iOS.

ThreadData::InitializeThreadContext() is called from some thread (whatever comes first)
It calls into ThreadData::EnsureTlsInitialization()
Which does:
#if BUILDFLAG(ENABLE_MEMORY_TASK_PROFILER)
  ...
    base::debug::ThreadHeapUsageTracker::EnableHeapTracking();
  ...
#endif
Ultimately EnableHeapTracking() calls base::allocator::InsertAllocatorDispatch(...)

So the problem here is that InsertAllocatorDispatch is called from a somewhat arbitrary thread. Which would be fine % the fact that when we use --enable-heap-profiling, we also call InsertAllocatorDispatch() (from the main thread, IIRC).

The two threads don't match and we end up hitting that DCHECK (thanks primiano-from-the-past for putting it in place!)
We should make sure that both shim dispatches (memory-infra and heap profiler) are always register on the same thread. The current code is racy.

Unfortunately didn't spot this while revieweing crrev.com/2386123003

P.S. this kind of breakage would be caught if we had fixed  Issue 670828  (write a browser test for tracing with --enable-heap-profiling)
 
Status: Assigned (was: Unconfirmed)

Comment 2 by siggi@chromium.org, Dec 5 2016

Ah - I can (easily) repro on Windows. How about making InsertAllocatorDispatch thread safe? Prepending to a monotonically growing single-linked list is easy enough to do with a compareandswap primitive?
Hmm if possible I prefer to not have to think about threading instead of making things thread safe.
Can we just move your call to EnableHeapTracking to the same thread ?

> Prepending to a monotonically growing single-linked list is easy enough to do with a compareandswap primitive?
Not sure is enough. You also have to make sure then that the state of the inserted dispatcher is ready and properly barriered, to avoid that one dispatcher calls into the 2nd which is not fully ready yet.
I mean, is not impossible to do, but I'd avoid going to the trouble of having to think to that if possible.

Comment 5 by siggi@chromium.org, Dec 6 2016

Status: Fixed (was: Assigned)
Components: Internals>Instrumentation>Memory

Sign in to add a comment