Skip to content

fix: eliminate race condition in concurrent mode with custom font#574

Open
mimol91 wants to merge 1 commit into
johnfercher:masterfrom
mimol91:fix-raceCondition
Open

fix: eliminate race condition in concurrent mode with custom font#574
mimol91 wants to merge 1 commit into
johnfercher:masterfrom
mimol91:fix-raceCondition

Conversation

@mimol91
Copy link
Copy Markdown

@mimol91 mimol91 commented May 12, 2026

Description
gofpdf mutates the slice during GenerateCutFont via append-padding in generateChecksum, causing write/write races when the same bytes are shared across Fpdf instances.

This PR fixes the issue by copying byte slice

Related Issue

Checklist

check with "x", ONLY IF APPLIED to your change

  • All methods associated with structs has func (<first letter of struct> *struct) method() {} name style.
  • Wrote unit tests for new/changed features.
  • Followed the unit test when,should naming pattern.
  • All mocks created with m := mocks.NewConstructor(t).
  • All mocks using m.EXPECT().MethodName() method to mock methods.
  • Updated docs/*
  • Updated example_test.go.
  • Updated README.md
  • New public methods/structs/interfaces has comments upside them explaining they responsibilities
  • Executed make dod with none issues pointed out by golangci-lint

Clone font bytes before passing to gofpdf so each worker goroutine owns
its backing array; gofpdf mutates the slice during GenerateCutFont via
append-padding in generateChecksum, causing write/write races when the
same bytes were shared across Fpdf instances.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds font byte slice cloning to the PDF builder to prevent aliasing issues during custom font registration, and introduces a concurrent test that validates the fix by repeatedly generating PDFs while using a registered TTF font under concurrent access.

Changes

Font Byte Cloning and Validation

Layer / File(s) Summary
Font byte slice cloning in builder
internal/providers/gofpdf/builder.go
The slices package is imported and slices.Clone(font.GetBytes()) wraps font bytes before AddUTF8FontFromBytes, replacing direct slice pass-through.
Concurrent font access test
maroto_test.go
Test imports expand to support font setup; a new concurrent test loads arial-unicode-ms.ttf, registers it as a custom UTF-8 font, applies it as the default, and repeatedly adds rows while generating the PDF to exercise concurrent font access.

🎯 2 (Simple) | ⏱️ ~8 minutes

🐰 A slice that was shared, now safely cloned with care,
Concurrent fonts dance without a despair,
TTF bytes flow through the builder so bright,
No aliasing shadows in the PDF light!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The PR description explains the race condition, the fix, and includes a related issue. However, it does not fully follow the template structure and is incomplete in some sections. Clarify the technical details of the race condition, verify the related issue link, and ensure the description provides complete context for reviewers to understand the fix's necessity.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies the main change: fixing a race condition in concurrent mode with custom fonts, which aligns perfectly with the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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