Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors thrown inside of queue "completed" handler result in "Missing lock" errors #2213

Closed
sysrun opened this issue Nov 12, 2021 · 9 comments

Comments

@sysrun
Copy link

sysrun commented Nov 12, 2021

This one gave me hours of pain...

Description

If an error is throw inside the queue "completed" handler, the queues "error" event will be triggered with "Error: Missing lock for job failed" and errorMessage "Error processing job".

Minimal, Working Test code to reproduce the issue.

After the second iteration the described error will be triggered

const bull = require('bull');

const queue = bull('testqueue', 'redis://localhost:6380');

queue.process(async (job) => {
  console.log('>> processing', job.id);
  return Promise.resolve(true);
});

queue.on('completed', (job, result) => {
  console.log('>>> completed', job.id);
  if (job.data.counter >= 2) {
    console.log('>>> counter >= 2, throwing inside "completed" handler');
    throw Error();
  }
});

queue.on('error', (error, errorMessage) => {
  console.error('error', error);
  console.error('errorMessage', errorMessage);
});

let counter = 0;
setInterval(() => {
  const job = queue.add({counter: counter++});
  console.log('> added job', job.id);
}, 1000);

Bull version

4.1.0

@sysrun sysrun changed the title Errors thrown inside of queue "complete" handler result in "Missing lock" errors Errors thrown inside of queue "completed" handler result in "Missing lock" errors Nov 12, 2021
@sysrun
Copy link
Author

sysrun commented Nov 12, 2021

Looks like thats the problem

bull/lib/queue.js

Lines 1092 to 1097 in 019d612

const handleCompleted = result => {
return job.moveToCompleted(result, undefined, notFetch).then(jobData => {
this.emit('completed', job, result, 'active');
return jobData ? this.nextJobFromJobData(jobData[0], jobData[1]) : null;
});
};

I modified the code; error not happening anymore

const handleCompleted = result => {
   return job.moveToCompleted(result, undefined, notFetch).then(jobData => {
     try {
       this.emit('completed', job, result, 'active');
     } catch (err) {
       // ignore
     }
     return jobData ? this.nextJobFromJobData(jobData[0], jobData[1]) : null;
   });
 };

@manast
Copy link
Member

manast commented Nov 12, 2021

Isn't it insane that emit can throw exceptions? Actually now that I read this issue I have a weak recall about this problem in another context. This is really insane if you think about it, and not the way you expect events should work, which its main feature is to decouple the event emitter from the event listeners... anyway, thanks for reporting.

@manast manast added the bug label Nov 12, 2021
@sysrun
Copy link
Author

sysrun commented Nov 12, 2021

Yeah, i don't get it too. Thats why it took me hours to find the problem...

@sysrun
Copy link
Author

sysrun commented Nov 12, 2021

@manast
Copy link
Member

manast commented Nov 13, 2021

@sysrun not sure that would solve the problem actually. The case at hand is when a non async listener throws an exception, as your code above demonstrates. Best would be to wrap emit so that it catches all possible exceptions that can be generated inside the listener. If the listener would have been async the throw will just be ignored.

@sysrun
Copy link
Author

sysrun commented Nov 13, 2021

So an emit wrapper would catch possible exceptions and "redirect" them to the default "error" event?

@manast
Copy link
Member

manast commented Nov 13, 2021

Yeah, thats what I am planing to do to fix this issue anyway.

@manast
Copy link
Member

manast commented Nov 14, 2021

ahh, actually I fixed this on BullMQ already, I just forgot mostly about it: https://github.com/taskforcesh/bullmq/blob/master/src/classes/queue-base.ts#L51

manast added a commit that referenced this issue Nov 16, 2021
@manast manast closed this as completed in 4978a2b Nov 16, 2021
github-actions bot pushed a commit that referenced this issue Nov 16, 2021
## [4.1.1](v4.1.0...v4.1.1) (2021-11-16)

### Bug Fixes

* **emit:** protect emit calls fixes [#2213](#2213) ([4978a2b](4978a2b))
@manast
Copy link
Member

manast commented Nov 16, 2021

🎉 This issue has been resolved in version 4.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants