Skip to content

Commit ee2c17d

Browse files
committed
improve mutex locking
1 parent 9a48965 commit ee2c17d

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

cmd/goose/github.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -634,18 +634,12 @@ func (app *App) fetchTurnDataAsync(ctx context.Context, issues []*github.Issue,
634634
}
635635

636636
// Only check for newly blocked PRs if there were actual changes
637-
// This must happen before UI updates so FirstBlockedAt is set correctly
637+
// checkForNewlyBlockedPRs will handle UI updates internally if needed
638638
if actualChanges > 0 {
639639
app.checkForNewlyBlockedPRs(ctx)
640-
}
641-
642-
// Update tray title and menu with final Turn data if menu is already initialized
643-
// This happens after checkForNewlyBlockedPRs so party poppers show correctly
644-
app.setTrayTitle()
645-
if app.menuInitialized {
646-
// Only trigger menu update if PR data actually changed
647-
if actualChanges > 0 {
648-
app.updateMenu(ctx)
649-
}
640+
// UI updates are handled inside checkForNewlyBlockedPRs
641+
} else {
642+
// No changes, but still update tray title in case of initial load
643+
app.setTrayTitle()
650644
}
651645
}

cmd/goose/main.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,26 @@ func (app *App) notifyWithSound(ctx context.Context, pr PR, isIncoming bool, pla
568568

569569
// checkForNewlyBlockedPRs sends notifications for blocked PRs.
570570
func (app *App) checkForNewlyBlockedPRs(ctx context.Context) {
571-
app.mu.RLock()
572-
incoming := app.incoming
573-
outgoing := app.outgoing
571+
// Check for context cancellation early
572+
select {
573+
case <-ctx.Done():
574+
log.Print("[BLOCKED] Context cancelled, skipping newly blocked PR check")
575+
return
576+
default:
577+
}
578+
579+
app.mu.Lock()
580+
// Make deep copies to work with while holding the lock
581+
incoming := make([]PR, len(app.incoming))
582+
copy(incoming, app.incoming)
583+
outgoing := make([]PR, len(app.outgoing))
584+
copy(outgoing, app.outgoing)
574585
previousBlocked := app.previousBlockedPRs
575586
blockedTimes := app.blockedPRTimes
576587
autoBrowserEnabled := app.enableAutoBrowser
577588
startTime := app.startTime
578589
hideStaleIncoming := app.hideStaleIncoming
579-
app.mu.RUnlock()
590+
app.mu.Unlock()
580591

581592
currentBlocked := make(map[string]bool)
582593
newBlockedTimes := make(map[string]time.Time)
@@ -649,17 +660,20 @@ func (app *App) checkForNewlyBlockedPRs(ctx context.Context) {
649660
}
650661
}
651662

663+
// Update state with a lock
652664
app.mu.Lock()
653665
app.previousBlockedPRs = currentBlocked
654666
app.blockedPRTimes = newBlockedTimes
655667
// Update the PR lists with FirstBlockedAt times
656668
app.incoming = incoming
657669
app.outgoing = outgoing
670+
menuInitialized := app.menuInitialized
658671
app.mu.Unlock()
659672

660-
// Update tray title and menu when called from main.go (not from github.go)
661-
// This ensures party popper shows for newly blocked PRs
662-
if app.menuInitialized {
673+
// Update UI after releasing the lock
674+
// Only update if there are newly blocked PRs
675+
if menuInitialized && len(currentBlocked) > len(previousBlocked) {
676+
log.Print("[BLOCKED] Updating UI for newly blocked PRs")
663677
app.setTrayTitle()
664678
app.updateMenu(ctx)
665679
}

0 commit comments

Comments
 (0)