hopefully fix potential race condition

This commit is contained in:
tsmethurst 2022-02-08 13:17:10 +01:00
parent c4ece24654
commit dba9ad4348

View File

@ -265,18 +265,18 @@ func (d *deref) fetchRemoteAccountMedia(ctx context.Context, targetAccount *gtsm
if targetAccount.AvatarRemoteURL != "" && (targetAccount.AvatarMediaAttachmentID == "" || refresh) { if targetAccount.AvatarRemoteURL != "" && (targetAccount.AvatarMediaAttachmentID == "" || refresh) {
var processingMedia *media.ProcessingMedia var processingMedia *media.ProcessingMedia
d.dereferencingAvatarsLock.Lock() // LOCK HERE
// first check if we're already processing this media // first check if we're already processing this media
d.dereferencingAvatarsLock.Lock()
if alreadyProcessing, ok := d.dereferencingAvatars[targetAccount.ID]; ok { if alreadyProcessing, ok := d.dereferencingAvatars[targetAccount.ID]; ok {
// we're already on it, no worries // we're already on it, no worries
processingMedia = alreadyProcessing processingMedia = alreadyProcessing
} }
d.dereferencingAvatarsLock.Unlock()
if processingMedia == nil { if processingMedia == nil {
// we're not already processing it so start now // we're not already processing it so start now
avatarIRI, err := url.Parse(targetAccount.AvatarRemoteURL) avatarIRI, err := url.Parse(targetAccount.AvatarRemoteURL)
if err != nil { if err != nil {
d.dereferencingAvatarsLock.Unlock()
return changed, err return changed, err
} }
@ -290,16 +290,15 @@ func (d *deref) fetchRemoteAccountMedia(ctx context.Context, targetAccount *gtsm
Avatar: &avatar, Avatar: &avatar,
}) })
if err != nil { if err != nil {
d.dereferencingAvatarsLock.Unlock()
return changed, err return changed, err
} }
// store it in our map to indicate it's in process // store it in our map to indicate it's in process
d.dereferencingAvatarsLock.Lock()
d.dereferencingAvatars[targetAccount.ID] = newProcessing d.dereferencingAvatars[targetAccount.ID] = newProcessing
d.dereferencingAvatarsLock.Unlock()
processingMedia = newProcessing processingMedia = newProcessing
} }
d.dereferencingAvatarsLock.Unlock() // UNLOCK HERE
// block until loaded if required... // block until loaded if required...
if blocking { if blocking {
@ -324,18 +323,18 @@ func (d *deref) fetchRemoteAccountMedia(ctx context.Context, targetAccount *gtsm
if targetAccount.HeaderRemoteURL != "" && (targetAccount.HeaderMediaAttachmentID == "" || refresh) { if targetAccount.HeaderRemoteURL != "" && (targetAccount.HeaderMediaAttachmentID == "" || refresh) {
var processingMedia *media.ProcessingMedia var processingMedia *media.ProcessingMedia
d.dereferencingHeadersLock.Lock() // LOCK HERE
// first check if we're already processing this media // first check if we're already processing this media
d.dereferencingHeadersLock.Lock()
if alreadyProcessing, ok := d.dereferencingHeaders[targetAccount.ID]; ok { if alreadyProcessing, ok := d.dereferencingHeaders[targetAccount.ID]; ok {
// we're already on it, no worries // we're already on it, no worries
processingMedia = alreadyProcessing processingMedia = alreadyProcessing
} }
d.dereferencingHeadersLock.Unlock()
if processingMedia == nil { if processingMedia == nil {
// we're not already processing it so start now // we're not already processing it so start now
headerIRI, err := url.Parse(targetAccount.HeaderRemoteURL) headerIRI, err := url.Parse(targetAccount.HeaderRemoteURL)
if err != nil { if err != nil {
d.dereferencingAvatarsLock.Unlock()
return changed, err return changed, err
} }
@ -349,16 +348,15 @@ func (d *deref) fetchRemoteAccountMedia(ctx context.Context, targetAccount *gtsm
Header: &header, Header: &header,
}) })
if err != nil { if err != nil {
d.dereferencingAvatarsLock.Unlock()
return changed, err return changed, err
} }
// store it in our map to indicate it's in process // store it in our map to indicate it's in process
d.dereferencingHeadersLock.Lock()
d.dereferencingHeaders[targetAccount.ID] = newProcessing d.dereferencingHeaders[targetAccount.ID] = newProcessing
d.dereferencingHeadersLock.Unlock()
processingMedia = newProcessing processingMedia = newProcessing
} }
d.dereferencingHeadersLock.Unlock() // UNLOCK HERE
// block until loaded if required... // block until loaded if required...
if blocking { if blocking {