mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: log mutex deadlocks
This commit is contained in:
@@ -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]
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user