Hello,

I'm working on a library where multiple threads can run JS code 
independently at the same time. In the library, each thread creates and 
uses its own Isolate. However, I noticed clang's AddressSanitizer was 
complaining about a data race during Isolate creation. I managed to 
reproduce the problem outside our library, and I'm sending both the code 
and the sanitizer's error message (the code needs to be compiled with 
-std=c++11 and -pthread). I'm using V8 5.8.283.38, but I repeated the test 
on the master branch and got the same result. What the test does is spawn 
10 threads; each one of them just creates and destroys Isolates repeatedly.

I have found a recent issue in the bug tracker [1] that reports a similar 
problem, but the commit is not included in V8 5.8, and the problem is 
reproducible in the master branch anyway.

So, my question is: I know that multiple threads can run JS code 
simultaneously, provided that each thread has its own Isolate; but does 
Isolate creation need to be synchronous?

Best regards,
Andre

[1] https://bugs.chromium.org/p/v8/issues/detail?id=5807

-- 
-- 
v8-users mailing list
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.
#include <thread>
#include <vector>

#include "v8.h"
#include "libplatform/libplatform.h"

using namespace v8;

const size_t kNumThreads = 10;
const size_t kNumIterations = 1000;

int main() {
  V8::InitializeICUDefaultLocation("./v8/out.gn/x64.debug/");
  V8::InitializeExternalStartupData("./v8/out.gn/x64.debug/");
  Platform* platform = platform::CreateDefaultPlatform();
  V8::InitializePlatform(platform);
  V8::Initialize();

  std::vector<std::thread> threads(kNumThreads);
  for (size_t i = 0; i < kNumThreads; ++i) {
    threads[i] = std::thread([](){
      for (int i = 0; i < kNumIterations; ++i) {
        Isolate::CreateParams create_params;
        create_params.array_buffer_allocator =
            v8::ArrayBuffer::Allocator::NewDefaultAllocator();
        Isolate* isolate = Isolate::New(create_params);
        {
          Isolate::Scope isolate_scope(isolate);
        }      
        delete create_params.array_buffer_allocator;
        isolate->Dispose();
      }
    });
  }

  for (std::thread &t : threads) {
    t.join();
  }

  V8::Dispose();
  V8::ShutdownPlatform();
  delete platform;
  return 0;
}
==================
WARNING: ThreadSanitizer: data race (pid=19032)
  Atomic read of size 1 at 0x7fa59714e078 by thread T9:
    #0 pthread_mutex_lock <null> (stress+0x000000439dc5)
    #1 v8::base::LockNativeHandle(pthread_mutex_t*) 
/home/andre/Develop/v8/v8/out.gn/x64.debug/../../src/base/platform/mutex.cc:57:16
 (libv8_libbase.so+0x000000021284)
    #2 void std::_Bind_simple<main::$_0 ()>::_M_invoke<>(std::_Index_tuple<>) 
/usr/bin/../lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/functional:1390:18
 (stress+0x0000004b2cb8)
    #3 std::_Bind_simple<main::$_0 ()>::operator()() 
/usr/bin/../lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/functional:1380:16
 (stress+0x0000004b2c68)
    #4 std::thread::_State_impl<std::_Bind_simple<main::$_0 ()> >::_M_run() 
/usr/bin/../lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/thread:196:13
 (stress+0x0000004b2a9c)
    #5 execute_native_thread_routine 
/usr/src/debug/gcc-6.3.1-20161221/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/src/c++11/../../../../../libstdc++-v3/src/c++11/thread.cc:83
 (libstdc++.so.6+0x0000000bb5ce)

  Previous write of size 1 at 0x7fa59714e078 by thread T8:
    #0 pthread_mutex_init <null> (stress+0x00000042580a)
    #1 v8::base::InitializeNativeHandle(pthread_mutex_t*) 
/home/andre/Develop/v8/v8/out.gn/x64.debug/../../src/base/platform/mutex.cc:23:12
 (libv8_libbase.so+0x00000002108e)
    #2 void std::_Bind_simple<main::$_0 ()>::_M_invoke<>(std::_Index_tuple<>) 
/usr/bin/../lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/functional:1390:18
 (stress+0x0000004b2cb8)
    #3 std::_Bind_simple<main::$_0 ()>::operator()() 
/usr/bin/../lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/functional:1380:16
 (stress+0x0000004b2c68)
    #4 std::thread::_State_impl<std::_Bind_simple<main::$_0 ()> >::_M_run() 
/usr/bin/../lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/thread:196:13
 (stress+0x0000004b2a9c)
    #5 execute_native_thread_routine 
/usr/src/debug/gcc-6.3.1-20161221/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/src/c++11/../../../../../libstdc++-v3/src/c++11/thread.cc:83
 (libstdc++.so.6+0x0000000bb5ce)

  Location is global 'v8::base::entropy_mutex' of size 56 at 0x7fa59714e070 
(libv8_libbase.so+0x000000034078)

  Thread T9 (tid=19042, running) created by main thread at:
    #0 pthread_create <null> (stress+0x000000425346)
    #1 __gthread_create 
/usr/src/debug/gcc-6.3.1-20161221/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/x86_64-redhat-linux/bits/gthr-default.h:662
 (libstdc++.so.6+0x0000000bb8e4)
    #2 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, 
std::default_delete<std::thread::_State> >, void (*)()) 
/usr/src/debug/gcc-6.3.1-20161221/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/src/c++11/../../../../../libstdc++-v3/src/c++11/thread.cc:163
 (libstdc++.so.6+0x0000000bb8e4)
    #3 main /home/andre/Develop/v8/stress.cc:21:18 (stress+0x0000004b2569)

  Thread T8 (tid=19041, running) created by main thread at:
    #0 pthread_create <null> (stress+0x000000425346)
    #1 __gthread_create 
/usr/src/debug/gcc-6.3.1-20161221/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/x86_64-redhat-linux/bits/gthr-default.h:662
 (libstdc++.so.6+0x0000000bb8e4)
    #2 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, 
std::default_delete<std::thread::_State> >, void (*)()) 
/usr/src/debug/gcc-6.3.1-20161221/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/src/c++11/../../../../../libstdc++-v3/src/c++11/thread.cc:163
 (libstdc++.so.6+0x0000000bb8e4)
    #3 main /home/andre/Develop/v8/stress.cc:21:18 (stress+0x0000004b2569)

SUMMARY: ThreadSanitizer: data race (/home/andre/Develop/v8/stress+0x439dc5) in 
pthread_mutex_lock
==================

Reply via email to