Skip to content

fix(billing): guard syncJob lifecycle with a dedicated mutex#1706

Merged
AmanGIT07 merged 2 commits into
mainfrom
fix/billing-syncjob-race-conditions
Jun 18, 2026
Merged

fix(billing): guard syncJob lifecycle with a dedicated mutex#1706
AmanGIT07 merged 2 commits into
mainfrom
fix/billing-syncjob-race-conditions

Conversation

@AmanGIT07

Copy link
Copy Markdown
Contributor

Summary

Synchronize access to the syncJob cron field across the four billing services, and make the blob resource cache read in GetAll race-safe.

Changes

  • Add a dedicated syncJobMu (distinct from the sync-operation mutex mu) to the customer, checkout, subscription, and invoice services; guard syncJob reads/writes in Init and Close.
  • Extract invoice's cron setup into initSyncJob so the lock uses defer while the credit-overdraft setup stays outside the lock.
  • blob.ResourcesRepository.GetAll: re-read repo.cached under the mutex after refresh() instead of reading it unlocked.
  • Add TestService_InitClose_Concurrent to each billing service.

Test Plan

  • go test -race ./billing/subscription/ ./billing/checkout/ ./billing/customer/ ./billing/invoice/
  • go build ./billing/... ./internal/store/blob/... and go vet pass

SQL Safety (if your PR touches *_repository.go or goqu.*)

  • N/A — the resources_repository.go change is concurrency-only (mutex placement); no SQL or goqu query building touched.

🤖 Generated with Claude Code

Init and Close mutated the syncJob cron field without holding a lock, so concurrent or repeated Init calls could race on the field and leak a started cron. Adds a dedicated syncJobMu across the four billing services and makes the blob GetAll cache read race-safe.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
frontier Ready Ready Preview, Comment Jun 17, 2026 7:01pm

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 37f2d69b-7b1b-458e-9540-9b568ac9d8a8

📥 Commits

Reviewing files that changed from the base of the PR and between e4d53c8 and 526225d.

📒 Files selected for processing (4)
  • billing/checkout/service.go
  • billing/customer/service.go
  • billing/invoice/service.go
  • billing/subscription/service.go
💤 Files with no reviewable changes (3)
  • billing/subscription/service.go
  • billing/checkout/service.go
  • billing/customer/service.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • billing/invoice/service.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability of billing service initialization and shutdown by serializing scheduled sync-job lifecycle handling across customer, checkout, invoice, and subscription.
    • Fixed thread-safety in cached resource reads to prevent potential unlocked access during refresh.
  • Tests
    • Added concurrency-focused Init/Close tests to exercise race-prone billing service behavior under simultaneous calls.

Walkthrough

Four billing service structs (checkout, customer, invoice, subscription) each gain a syncJobMu sync.Mutex field; Init and Close in each service acquire this mutex to serialize cron sync-job lifecycle operations. The invoice service additionally extracts cron setup into a new initSyncJob helper. Concurrent race-detector tests are added for all four services. A separate fix in internal/store/blob/resources_repository.go makes GetAll re-read cached data under a mutex after refresh completes.

Changes

Billing Service syncJob Concurrency Fixes

Layer / File(s) Summary
syncJobMu field additions across all four billing services
billing/checkout/service.go, billing/customer/service.go, billing/invoice/service.go, billing/subscription/service.go
Each Service struct gains syncJobMu sync.Mutex field.
Init and Close mutex guards in checkout, customer, and subscription services
billing/checkout/service.go, billing/customer/service.go, billing/subscription/service.go
Init and Close in each service acquire syncJobMu with defer unlock before any syncJob read or write; Close no longer returns syncJob.Stop().Err() and instead returns nil.
invoice service initSyncJob helper and syncJobMu guard
billing/invoice/service.go
Init delegates cron setup to a new initSyncJob helper that locks syncJobMu, waits for any existing job to fully stop via Stop().Done(), then creates and starts a new cron; Close also acquires syncJobMu before stopping and returns nil.
Concurrent Init/Close race-detector tests
billing/checkout/service_concurrent_test.go, billing/customer/service_concurrent_test.go, billing/invoice/service_concurrent_test.go, billing/subscription/service_concurrent_test.go
New TestService_InitClose_Concurrent tests for all four packages launch two goroutines calling Init concurrently via sync.WaitGroup, then call Close, asserting no errors and exposing races under go test -race.

Blob Resources Repository Cache Read Fix

Layer / File(s) Summary
Mutex-protected cache read after refresh in GetAll
internal/store/blob/resources_repository.go
After repo.refresh(ctx), GetAll now acquires repo.mu to read repo.cached before returning, replacing the prior unlocked direct field access. Import block is also reordered with no functional impact.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
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.


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
billing/checkout/service.go (1)

175-181: ⚠️ Potential issue | 🟠 Major

Use a single Stop() result in Close instead of stopping twice.

At Line 179 you already wait for shutdown completion, but Line 180 calls Stop() again and returns that second context's Err(). That return value is detached from the stop you actually awaited and can yield flaky/non-actionable close errors.

💡 Proposed fix
 func (s *Service) Close() error {
 	s.syncJobMu.Lock()
 	defer s.syncJobMu.Unlock()
 	if s.syncJob != nil {
-		<-s.syncJob.Stop().Done()
-		return s.syncJob.Stop().Err()
+		stopCtx := s.syncJob.Stop()
+		<-stopCtx.Done()
+		s.syncJob = nil
 	}
 	return nil
 }

Apply the same pattern in:

  • billing/customer/service.go (Line 384-390)
  • billing/subscription/service.go (Line 142-148)

Note: billing/invoice/service.go has a different issue—it calls Stop().Err() without awaiting completion and should also be fixed, but with a different approach.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4313c410-b272-4223-9359-a21f45816dcb

📥 Commits

Reviewing files that changed from the base of the PR and between 9f793c1 and e4d53c8.

📒 Files selected for processing (9)
  • billing/checkout/service.go
  • billing/checkout/service_concurrent_test.go
  • billing/customer/service.go
  • billing/customer/service_concurrent_test.go
  • billing/invoice/service.go
  • billing/invoice/service_concurrent_test.go
  • billing/subscription/service.go
  • billing/subscription/service_concurrent_test.go
  • internal/store/blob/resources_repository.go

Close called Cron.Stop() twice and returned the second context's Err(), which races the stop goroutine and intermittently returns context.Canceled. Wait on Stop().Done() and return nil.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 27712777114

Coverage increased (+0.2%) to 43.804%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 13 uncovered changes across 2 files (30 of 43 lines covered, 69.77%).
  • 1 coverage regression across 1 file.

Uncovered Changes

File Changed Covered %
billing/invoice/service.go 27 18 66.67%
internal/store/blob/resources_repository.go 4 0 0.0%
Total (5 files) 43 30 69.77%

Coverage Regressions

1 previously-covered line in 1 file lost coverage.

File Lines Losing Coverage Coverage
billing/invoice/service.go 1 5.54%

Coverage Stats

Coverage Status
Relevant Lines: 37058
Covered Lines: 16233
Line Coverage: 43.8%
Coverage Strength: 12.39 hits per line

💛 - Coveralls

@AmanGIT07 AmanGIT07 merged commit adbb040 into main Jun 18, 2026
8 checks passed
@AmanGIT07 AmanGIT07 deleted the fix/billing-syncjob-race-conditions branch June 18, 2026 06:15
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.

3 participants