Skip to content

Comments

fix(badge): make thread-safe with sync.Mutex#147

Open
vaskozl wants to merge 1 commit intokashalls:mainfrom
vaskozl:main
Open

fix(badge): make thread-safe with sync.Mutex#147
vaskozl wants to merge 1 commit intokashalls:mainfrom
vaskozl:main

Conversation

@vaskozl
Copy link
Contributor

@vaskozl vaskozl commented Feb 18, 2026

Currently go-badge is not thread safe.
This sometimes results in panics when using format=badge.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses concurrency issues in the format=badge response path by making go-badge usage thread-safe within the HTTP handler, preventing sporadic panics under concurrent requests.

Changes:

  • Add a sync.Mutex to KromgoHandler to serialize access to the shared badge.Generator.
  • Refactor badge rendering to generate SVG bytes under the mutex, then write the response.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +147 to +157
h.badgeMu.Lock()
var badgeBytes []byte
switch badgeStyle {
case "plastic":
badgeBytes = h.BadgeGenerator.GeneratePlastic(title, message, hex)
case "flat-square":
badgeBytes = h.BadgeGenerator.GenerateFlatSquare(title, message, hex)
default:
badgeBytes = h.BadgeGenerator.GenerateFlat(title, message, hex)
}
if badgeStyle == "flat-square" {
w.Write(h.BadgeGenerator.GenerateFlatSquare(title, message, hex))
return
}
//if badgeStyle == "flat" || badgeStyle == "" {
// w.Write(h.BadgeGenerator.GenerateFlat(title, message, hex))
// return
//}
h.badgeMu.Unlock()
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mutex is manually unlocked after the switch. To avoid accidental deadlocks if this block changes in the future (e.g., a new early-return path or an unexpected panic/recover), prefer Lock() followed immediately by defer Unlock() so the unlock is guaranteed.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant