Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lock before checking sp.rendered to avoid datarace #391

Merged
merged 1 commit into from
Mar 13, 2025

Conversation

jvogit
Copy link
Contributor

@jvogit jvogit commented Mar 12, 2025

We are using libopenapi + vacuum for our internal tooling. We've run into data race in our unit tests:

==================
WARNING: DATA RACE
Write at 0x00c000520198 by goroutine 305:
  github.com/pb33f/libopenapi/datamodel/high/base.(*SchemaProxy).Schema()
      project/vendor/github.com/pb33f/libopenapi/datamodel/high/base/schema_proxy.go:146 +0xbb9
  github.com/pb33f/doctor/model/high/base.(*SchemaProxy).Walk()
      project/vendor/github.com/pb33f/doctor/model/high/base/schema_proxy.go:80 +0xfd
  github.com/pb33f/doctor/model/high/v3.(*MediaType).Walk.func1()
      project/vendor/github.com/pb33f/doctor/model/high/v3/media_type.go:39 +0x65
  github.com/sourcegraph/conc/panics.(*Catcher).Try()
      project/vendor/github.com/sourcegraph/conc/panics/panics.go:23 +0x77
  github.com/sourcegraph/conc.(*WaitGroup).Go.func1()
      project/vendor/github.com/sourcegraph/conc/waitgroup.go:32 +0x8f

Previous read at 0x00c000520198 by goroutine 296:
  github.com/pb33f/libopenapi/datamodel/high/base.(*SchemaProxy).Schema()
      project/vendor/github.com/pb33f/libopenapi/datamodel/high/base/schema_proxy.go:97 +0x85
  github.com/pb33f/doctor/model/high/v3.(*MediaType).GetSize()
      project/vendor/github.com/pb33f/doctor/model/high/v3/media_type.go:123 +0x304
  github.com/pb33f/doctor/model.(*DrDocument).processObject()
      project/vendor/github.com/pb33f/doctor/model/walk_model.go:602 +0xfc8
  github.com/pb33f/doctor/model.(*DrDocument).walkV3.func1()
      project/vendor/github.com/pb33f/doctor/model/walk_model.go:457 +0x604
  github.com/pb33f/doctor/model.(*DrDocument).walkV3.gowrap1()
      project/vendor/github.com/pb33f/doctor/model/walk_model.go:466 +0x61

A concurrent read and write is done on those lines, specifically with sp.rendered. This fixes this by deferring the unlock and moving sp.rendered into the critical section.

Copy link

codecov bot commented Mar 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.62%. Comparing base (d3dcac4) to head (f6f58b4).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #391      +/-   ##
==========================================
+ Coverage   99.55%   99.62%   +0.06%     
==========================================
  Files         165      165              
  Lines       21606    21603       -3     
==========================================
+ Hits        21509    21521      +12     
+ Misses         92       77      -15     
  Partials        5        5              
Flag Coverage Δ
unittests 99.62% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@daveshanley daveshanley left a comment

Choose a reason for hiding this comment

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

looks good to me! Thank you for tuning this up!

@daveshanley daveshanley merged commit b0c714b into pb33f:main Mar 13, 2025
4 checks passed
@jvogit jvogit deleted the fix-datarace branch March 13, 2025 22:39
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.

2 participants