bull icon indicating copy to clipboard operation
bull copied to clipboard

TypeError: Cannot read property 'timestamp' of null from queue#clean function

Open andrewtamura opened this issue 10 years ago • 4 comments

We see this exception originating from lib/queue.jsL825.

We're calling queue#clean periodically to clean up completed jobs.

queue.clean(1000, 'completed');

We also have a local listener for the "completed" event as follows:

queue.on('completed', function(job) {
  // Workaround Bull issue https://github.com/OptimalBits/bull/issues/253
  setImmediate(function() {
    job.remove();
  });
});

Here is L825 and some of the code that might be problematic:

getter = 'get' + type.charAt(0).toUpperCase() + type.slice(1);
if(getter !== 'getCompleted' &&
 getter !== 'getWaiting' &&
 getter !== 'getActive' &&
 getter !== 'getDelayed' &&
 getter !== 'getFailed') {
 return reject(new Error('Cannot clean unkown queue type'));
}

_this[getter]().then(function (jobs) {
 //take all jobs outside of the grace period
 return Promise.filter(jobs, function (job) {
   return (!job.timestamp) || (job.timestamp < Date.now() - grace);
 });
})

The exception appears to originate from queue#getCompleted() returning null values instead of redis keys. Is there a race condition that would result in queue#getCompleted() returning null values if job#remove() is called simultaneously? The exception can be prevented by checking the job passed by Promise#filter before accessing its timestamp property.

andrewtamura avatar Mar 03 '16 02:03 andrewtamura

the clean code is quite new, and there may be race conditions there as well. In the branch where I am converting all non atomic operations to lua scripts, the clean operation would be a clear candidate for that, and that would probably avoid the race condition that you are experimenting. In the mean while we could check if the job exists or not in the getCompleted method.

manast avatar Mar 03 '16 23:03 manast

I made a PR that checks the job before accessing the timestamp property. This check will prevent the TypeError that I'm seeing due to the race condition between queue#clean and job#remove.

andrewtamura avatar Mar 04 '16 18:03 andrewtamura

Oops, there was a mistake in the logic from my last PR. I've fixed it in https://github.com/OptimalBits/bull/pull/264

andrewtamura avatar Mar 04 '16 18:03 andrewtamura

Looks like #264 is merged, so this can be closed?

doublerebel avatar Nov 14 '16 18:11 doublerebel