From f4284e7b3f88ad781f5614d74ab51d2e12e81d31 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 29 Jun 2023 15:18:54 +0300 Subject: [PATCH] Prevent attachment semaphore from blocking permanently --- attachments.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/attachments.go b/attachments.go index 34e6b91..9526211 100644 --- a/attachments.go +++ b/attachments.go @@ -279,10 +279,18 @@ func (br *DiscordBridge) copyAttachmentToMatrix(intent *appservice.IntentAPI, ur } const attachmentSizeVal = 1 - onceErr = br.parallelAttachmentSemaphore.Acquire(context.Background(), attachmentSizeVal) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + onceErr = br.parallelAttachmentSemaphore.Acquire(ctx, attachmentSizeVal) + cancel() if onceErr != nil { - onceErr = fmt.Errorf("failed to acquire semaphore: %w", onceErr) - return + br.ZLog.Warn().Err(onceErr).Msg("Failed to acquire semaphore") + ctx, cancel = context.WithTimeout(context.Background(), 50*time.Second) + onceErr = br.parallelAttachmentSemaphore.Acquire(ctx, attachmentSizeVal) + cancel() + if onceErr != nil { + onceErr = fmt.Errorf("reuploading timed out") + return + } } var semaWg sync.WaitGroup semaWg.Add(1)