From bcf33d786ca2c5518d0504e8034c0dba30705170 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Fri, 25 Nov 2022 22:00:25 +0530 Subject: [PATCH] chore: log mutex deadlocks --- src/Utils/event-buffer.ts | 2 +- src/Utils/make-mutex.ts | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Utils/event-buffer.ts b/src/Utils/event-buffer.ts index ea7803a..71c9385 100644 --- a/src/Utils/event-buffer.ts +++ b/src/Utils/event-buffer.ts @@ -21,7 +21,7 @@ const BUFFERABLE_EVENT = [ 'groups.update', ] as const -const BUFFER_TIMEOUT_MS = 30_000 +const BUFFER_TIMEOUT_MS = 60_000 type BufferableEvent = typeof BUFFERABLE_EVENT[number] diff --git a/src/Utils/make-mutex.ts b/src/Utils/make-mutex.ts index d897903..1b1d70b 100644 --- a/src/Utils/make-mutex.ts +++ b/src/Utils/make-mutex.ts @@ -1,16 +1,35 @@ +import logger from './logger' + +const MUTEX_TIMEOUT_MS = 60_000 + export const makeMutex = () => { let task = Promise.resolve() as Promise + + let taskTimeout: NodeJS.Timeout | undefined + return { mutex(code: () => Promise | T): Promise { task = (async() => { + const stack = new Error('mutex start').stack + let waitOver = false + taskTimeout = setTimeout(() => { + logger.warn({ stack, waitOver }, 'possible mutex deadlock') + }, MUTEX_TIMEOUT_MS) // wait for the previous task to complete // if there is an error, we swallow so as to not block the queue try { await task } catch{ } - // execute the current task - return code() + waitOver = true + + try { + // execute the current task + const result = await code() + return result + } finally { + clearTimeout(taskTimeout) + } })() // we replace the existing task, appending the new piece of execution to it // so the next task will have to wait for this one to finish