bullmq icon indicating copy to clipboard operation
bullmq copied to clipboard

[Bug]: `worker.close` hangs forever when queue is empty

Open trylovetom opened this issue 2 years ago • 4 comments

Version

bullmq: v4.12.4 redis: v7.2 bun: v1.0.6

Platform

NodeJS

What happened?

When the queue is empty (no jobs added), calling worker.close will hang and prevent a graceful shutdown.

How to reproduce.

import IORedis from 'ioredis'
import { Queue, Worker } from 'bullmq'

const connection = new IORedis(process.env.QUEUE_URL!, {
  maxRetriesPerRequest: null,
  enableOfflineQueue: false
})
const queue = new Queue('foo', { connection })
const worker = new Worker('foo', async (job) => console.info(job.data), {
  connection
})

await queue.waitUntilReady()
await worker.waitUntilReady()

async function handlerShutdown() {
  console.info('worker closing')
  await worker.close()
  console.info('worker closed')
  console.info('queue closing')
  await queue.close()
  console.info('queue closed')
  console.info('connection disconnecting')
  connection.disconnect()
  console.info('connection disconnected')
}

process.on('SIGINT', handlerShutdown).on('SIGTERM', handlerShutdown)

Relevant log output

# Expect
# ctrl+c
$ worker closing
$ worker closed
$ queue closing
$ queue closed
$ connection disconnecting
$ connection disconnected

# Actual
# ctrl+c
$ worker closing # hangs forever

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

trylovetom avatar Oct 19 '23 01:10 trylovetom

This issue comes from this other issue in IORedis: https://github.com/redis/ioredis/issues/1830 The only workaround I know of right now is to just ignore the "end" event... strangely, I am quite sure this works in some other situations, otherwise close would never end but it does most of the time.

manast avatar Oct 23 '23 15:10 manast

This is actually working perfectly using NodeJS instead of Bun. For some reason, I started reproducing the issue with Bun and forgot about it. I am trying with version 18.10.0 on MacOS and it does not hang at all.

manast avatar Oct 24 '23 08:10 manast

workaround

async function handlerShutdown() {
  console.info('worker closing')
  - await worker.close()
  + await Promise.all([worker.close(), sleep(3 * 1000)])
  + await worker.close(true)
  console.info('worker closed')
  console.info('queue closing')
  queue.close()
  console.info('queue closed')
  console.info('connection disconnecting')
  connection.disconnect()
  console.info('connection disconnected')
}

trylovetom avatar Oct 25 '23 11:10 trylovetom

With the latest version of Bun (1.1.9) this seems to have been fixed

nullndr avatar May 23 '24 06:05 nullndr