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',
|
'groups.update',
|
||||||
] as const
|
] as const
|
||||||
|
|
||||||
const BUFFER_TIMEOUT_MS = 30_000
|
const BUFFER_TIMEOUT_MS = 60_000
|
||||||
|
|
||||||
type BufferableEvent = typeof BUFFERABLE_EVENT[number]
|
type BufferableEvent = typeof BUFFERABLE_EVENT[number]
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,35 @@
|
|||||||
|
import logger from './logger'
|
||||||
|
|
||||||
|
const MUTEX_TIMEOUT_MS = 60_000
|
||||||
|
|
||||||
export const makeMutex = () => {
|
export const makeMutex = () => {
|
||||||
let task = Promise.resolve() as Promise<any>
|
let task = Promise.resolve() as Promise<any>
|
||||||
|
|
||||||
|
let taskTimeout: NodeJS.Timeout | undefined
|
||||||
|
|
||||||
return {
|
return {
|
||||||
mutex<T>(code: () => Promise<T> | T): Promise<T> {
|
mutex<T>(code: () => Promise<T> | T): Promise<T> {
|
||||||
task = (async() => {
|
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
|
// wait for the previous task to complete
|
||||||
// if there is an error, we swallow so as to not block the queue
|
// if there is an error, we swallow so as to not block the queue
|
||||||
try {
|
try {
|
||||||
await task
|
await task
|
||||||
} catch{ }
|
} catch{ }
|
||||||
|
|
||||||
// execute the current task
|
waitOver = true
|
||||||
return code()
|
|
||||||
|
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
|
// 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
|
// so the next task will have to wait for this one to finish
|
||||||
|
|||||||
Reference in New Issue
Block a user