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

Make the package safe for concurrent access #284

Merged
merged 7 commits into from
Sep 26, 2023
Merged

Make the package safe for concurrent access #284

merged 7 commits into from
Sep 26, 2023

Conversation

alovak
Copy link
Contributor

@alovak alovak commented Sep 21, 2023

With this PR, we've enhanced the concurrency safety of the methods in iso8583.Message and field.Composite.

Rationale

I encountered a data race when two goroutines executed message.GetString(11). This happened because the GetString method did more than just returning a value — it also updated an internal map. This concurrent access led to the race condition.

Assumptions on Concurrency:

  • Individual field access (like field.String, field.Numeric) is not thread-safe. Concurrency safety is ensured at the Message and Composite levels. If external concurrent access to individual fields is necessary, users should manage it with appropriate synchronization mechanisms, such as a mutex.
  • Concurrent access is expected to be infrequent. Given this, the overhead introduced by using a mutex within Message and Composite is likely to be minimal.

Concerns

A potential solution to the data race I encountered would have been to simply eliminate the unnecessary map modification, thus making GetXXX() methods pure getters (which is also done in the PR). However, because the message is referenced via a pointer and has a combination of getters, setters, Marshal, and Unmarshal methods, it doesn't feel entirely safe to assume that access to these will always be sequential. So, enhancing concurrency safety seems like a guarded measure.

@codecov-commenter
Copy link

codecov-commenter commented Sep 22, 2023

Codecov Report

Attention: 11 lines in your changes are missing coverage. Please review.

Comparison is base (b652df4) 73.70% compared to head (d2157a7) 73.95%.

❗ Current head d2157a7 differs from pull request most recent head a0dcd00. Consider uploading reports for the commit a0dcd00 to get more accurate results

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #284      +/-   ##
==========================================
+ Coverage   73.70%   73.95%   +0.24%     
==========================================
  Files          43       43              
  Lines        2282     2338      +56     
==========================================
+ Hits         1682     1729      +47     
- Misses        369      378       +9     
  Partials      231      231              
Files Coverage Δ
test/fuzz-reader/reader.go 55.55% <0.00%> (ø)
message.go 74.90% <91.83%> (+2.95%) ⬆️
field/composite.go 80.79% <85.36%> (-1.09%) ⬇️

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

field/composite.go Outdated Show resolved Hide resolved
field/composite_test.go Outdated Show resolved Hide resolved
message.go Outdated Show resolved Hide resolved
message_test.go Show resolved Hide resolved
@alovak alovak requested a review from bpross September 25, 2023 17:43
@alovak alovak merged commit b05481c into master Sep 26, 2023
9 checks passed
@alovak alovak deleted the solve-data-race branch September 26, 2023 13:29
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.

4 participants