From 5bb8a8b5aae16bd0b4066e206b735ea561adde26 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Mon, 18 Dec 2023 21:12:50 -0800 Subject: [PATCH] fix: address potential crash that can happen with queued object storage uploads. Fixes #3440 --- core/storageproviders/s3Storage.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/core/storageproviders/s3Storage.go b/core/storageproviders/s3Storage.go index d9e7d1604..b5f8b330f 100644 --- a/core/storageproviders/s3Storage.go +++ b/core/storageproviders/s3Storage.go @@ -8,6 +8,7 @@ import ( "path/filepath" "sort" "strings" + "sync" "time" "github.com/owncast/owncast/core/data" @@ -26,26 +27,29 @@ import ( // S3Storage is the s3 implementation of a storage provider. type S3Storage struct { - sess *session.Session + // If we try to upload a playlist but it is not yet on disk + // then keep a reference to it here. + queuedPlaylistUpdates map[string]string + s3Client *s3.S3 uploader *s3manager.Uploader - // If we try to upload a playlist but it is not yet on disk - // then keep a reference to it here. - queuedPlaylistUpdates map[string]string + sess *session.Session + s3Secret string s3Bucket string s3Region string s3ServingEndpoint string s3AccessKey string - s3Secret string s3ACL string s3PathPrefix string s3Endpoint string host string + lock sync.Mutex + s3ForcePathStyle bool } @@ -53,6 +57,7 @@ type S3Storage struct { func NewS3Storage() *S3Storage { return &S3Storage{ queuedPlaylistUpdates: make(map[string]string), + lock: sync.Mutex{}, } } @@ -126,6 +131,8 @@ func (s *S3Storage) VariantPlaylistWritten(localFilePath string) { // We are uploading the variant playlist after uploading the segment // to make sure we're not referring to files in a playlist that don't // yet exist. See SegmentWritten. + s.lock.Lock() + defer s.lock.Unlock() if _, ok := s.queuedPlaylistUpdates[localFilePath]; ok { if _, err := s.Save(localFilePath, 0); err != nil { log.Errorln(err)