chore: log mutex deadlocks

This commit is contained in:
Adhiraj Singh
2022-11-25 22:00:25 +05:30
parent b141ac2c00
commit bcf33d786c
2 changed files with 22 additions and 3 deletions

View File

@@ -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]

View File

@@ -1,16 +1,35 @@
import logger from './logger'
const MUTEX_TIMEOUT_MS = 60_000
export const makeMutex = () => {
let task = Promise.resolve() as Promise<any>
let taskTimeout: NodeJS.Timeout | undefined
return {
mutex<T>(code: () => Promise<T> | T): Promise<T> {
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