protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

bug : concurrent invocation trigger bug . (when protobuf load file to an existed root.)

Open JJ2667699 opened this issue 8 years ago • 1 comments

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.

JJ2667699 avatar May 24 '17 02:05 JJ2667699

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);
    }
  });
}

ljluestc avatar Sep 10 '23 19:09 ljluestc