protobuf.js
protobuf.js copied to clipboard
bug : concurrent invocation trigger bug . (when protobuf load file to an existed root.)
protobuf.js version: <6.7.3>
concurrent invocation trigger bugs
let __protoRoot = new protobuf.Root();
function _getMessage(path,filename,cb){
var mess = __protoRoot.lookup(path);
if(!mess){
protobuf.load(filename,__protoRoot,(err,root)=>{
if(err)
throw err;
if(!__protoRoot.nested)
throw '__protoRoot.nested is not a object'
else
return cb(__protoRoot.lookup(path));
});
}
else
cb(mess);
}
when concurrent invocate function _getMessage, it throw '__protoRoot.nested is not a object' error.
nodejs only.
const protobuf = require("protobufjs");
// Create a mutex to synchronize access to __protoRoot
const protoRootMutex = new Mutex();
let __protoRoot = new protobuf.Root();
function _getMessage(path, filename, cb) {
// Use the mutex to synchronize access to __protoRoot
protoRootMutex.lock(() => {
const mess = __protoRoot.lookup(path);
if (!mess) {
protobuf.load(filename, __protoRoot, (err, root) => {
if (err) {
protoRootMutex.unlock();
throw err;
}
if (!__protoRoot.nested) {
protoRootMutex.unlock();
throw '__protoRoot.nested is not an object';
} else {
protoRootMutex.unlock();
return cb(__protoRoot.lookup(path));
}
});
} else {
// Make sure to unlock the mutex when done
protoRootMutex.unlock();
cb(mess);
}
});
}