Avoid leaking realms when an error occurs within an event handler

This commit is contained in:
Thomas Goyne 2018-03-20 16:52:52 -07:00
parent b743952636
commit f663e4846c
2 changed files with 23 additions and 3 deletions

View File

@ -43,8 +43,15 @@ process.on('message', (m) => {
if (impl.onchange) {
const change = Realm.Sync._deserializeChangeSet(m.change);
if (!change.isEmpty) {
try {
impl.onchange(change);
}
catch (e) {
change.close();
process.send({change: m.change});
throw e;
}
}
change.close();
}
process.send({change: m.change});

View File

@ -65,11 +65,24 @@ class FunctionListener {
changes.release();
return;
}
const promise = Promise.resolve(this.fn(changes));
let promise;
try {
promise = Promise.resolve(this.fn(changes));
}
catch (e) {
changes.release();
throw e;
}
this.pending.push(promise);
promise.then(() => {
const release = () => {
changes.release();
this.pending.splice(this.pending.indexOf(promise), 1);
};
promise.then(release).catch(e => {
release();
throw e;
});
}
};