Skip to content

Commit

Permalink
Fix deadlock from disposal under certain circumstances
Browse files Browse the repository at this point in the history
Fixes #302
  • Loading branch information
laverdet committed May 3, 2022
1 parent 218e87a commit 16b2bc1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/isolate/scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,24 @@ Scheduler::AsyncWait::~AsyncWait() {
}

void Scheduler::AsyncWait::Ready() {
auto lock = scheduler.Lock();
ready = true;
if (done) {
auto lock = state.write();
lock->ready = true;
if (lock->done) {
scheduler.cv.notify_one();
}
}

void Scheduler::AsyncWait::Wait() {
std::unique_lock<std::mutex> lock{scheduler.mutex};
while (!ready || !done) {
while (!state.read()->did_initialize()) {
scheduler.cv.wait(lock);
}
}

void Scheduler::AsyncWait::Wake() {
auto lock = scheduler.Lock();
done = true;
if (ready) {
auto lock = state.write();
lock->done = true;
if (lock->ready) {
scheduler.cv.notify_one();
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/isolate/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,17 @@ class Scheduler {
void Wake();

private:
struct state_t {
bool ready = false;
bool done = false;

bool did_initialize() const {
return ready || done;
}
};

class LockedScheduler& scheduler;
bool done = false;
bool ready = false;
lockable_t<state_t> state;
};

// Task queues
Expand Down
9 changes: 9 additions & 0 deletions tests/self-async-dispose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const ivm = require('isolated-vm');

const isolate = new ivm.Isolate();
const context = isolate.createContextSync();
context.global.setSync("getProm", () => {}, { reference: true });
context.eval("getProm.applySync()").catch(() => console.log('pass'));
context.release();
process.nextTick(() => {});
isolate.dispose();

0 comments on commit 16b2bc1

Please sign in to comment.