Skip to content

Commit

Permalink
Don't destroy v8::Platform global instance on exit (#323)
Browse files Browse the repository at this point in the history
This is not a bug fix per se but it does mitigate a number of crashes
at program exit. It's not safe to tear down the Platform instance if
the program terminates abnormally.
  • Loading branch information
bnoordhuis authored Oct 19, 2024
1 parent 5701956 commit 8d1e66c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions ext/mini_racer_extension/mini_racer_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ static VALUE rb_mJSON;
static VALUE rb_cFailedV8Conversion;
static VALUE rb_cDateTime = Qnil;

static std::unique_ptr<Platform> current_platform = NULL;
// note: |current_platform| is deliberately leaked on program exit
// because it's not safe to destroy it after main() has exited;
// abnormal termination may have left V8 in an undefined state
static Platform *current_platform;
static std::mutex platform_lock;

static pthread_attr_t *thread_attr_p;
Expand Down Expand Up @@ -366,11 +369,11 @@ static void init_v8() {
if (current_platform == NULL) {
V8::InitializeICU();
if (single_threaded) {
current_platform = platform::NewSingleThreadedDefaultPlatform();
current_platform = platform::NewSingleThreadedDefaultPlatform().release();
} else {
current_platform = platform::NewDefaultPlatform();
current_platform = platform::NewDefaultPlatform().release();
}
V8::InitializePlatform(current_platform.get());
V8::InitializePlatform(current_platform);
V8::Initialize();
}

Expand Down Expand Up @@ -1006,7 +1009,7 @@ static VALUE rb_isolate_pump_message_loop(VALUE self) {

Locker guard { isolate_info->isolate };

if (platform::PumpMessageLoop(current_platform.get(), isolate_info->isolate)){
if (platform::PumpMessageLoop(current_platform, isolate_info->isolate)){
return Qtrue;
} else {
return Qfalse;
Expand Down

0 comments on commit 8d1e66c

Please sign in to comment.