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: concurrent map write cause by concurrent invocation of function … #2168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

liushv0
Copy link

@liushv0 liushv0 commented Feb 19, 2024

…DeepCopy (#2167)

Type of change:

  • Bugfix
  • New feature provided
  • Improve performance
  • Backport patches
  • Documentation
  • Refactor
  • Chore
  • CI/CD or Tests

What this PR does / why we need it:

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 37.66%. Comparing base (6705ea5) to head (a9241e9).

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2168      +/-   ##
==========================================
+ Coverage   37.38%   37.66%   +0.28%     
==========================================
  Files          94       94              
  Lines        7971     8007      +36     
==========================================
+ Hits         2980     3016      +36     
  Misses       4597     4597              
  Partials      394      394              

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

@Revolyssup
Copy link
Contributor

The struct on which unmarshal is being performed is created with new() everytime when DeepCopy is called. Which means the data which is being written is not shared between function calls, so how can there be concurrent writes? I am trying to understand that right now.

@Revolyssup
Copy link
Contributor

Because we have the same kind of implementation as there are in kubernetes repos and nowhere do they use a lock
https://github.com/search?q=org%3Akubernetes%20DeepCopy(&type=code

@Revolyssup
Copy link
Contributor

@liushv0 Can you answer the above questions to help me review this PR?

@liushv0
Copy link
Author

liushv0 commented Mar 6, 2024

image

sorry for late :(

apisix-route sync goroutine(gr1)will get test-ar from MemDB,
apisix-plugin-config sync goroutine (gr2 )will get test-apc from MemDB,
test-apc has be refered by test-ar。

now gr1 begin to sync CR test-apc,func PluginConfig.DeepCopyInto will be called,in this function,Plugins#DeepCopyInto be called。
image

at the same time, gr2 begin to sync CR test-apc,func Route#DeepCopyInto will be called,in this function,Plugins#DeepCopyInto also be called。
image

Plugins in test-apc and Plugins in test-ar pointer to same instance,so two goroutine concurrent marshal this Plugins instance,and panic

@Revolyssup
Copy link
Contributor

@liushv0 I think there is no point in having locks as well as DeepCopy. I think this line need to be removed

. This line may have been added when this file was autto generated.

@Revolyssup
Copy link
Contributor

@liushv0 Are you available to make changes here?

@Revolyssup Revolyssup self-requested a review March 15, 2024 08:25
@@ -113,6 +127,8 @@ func (c *dbCache) GetUpstream(id string) (*v1.Upstream, error) {
if err != nil {
return nil, err
}
c.upstreamLock.Lock()
defer c.upstreamLock.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

@liushv0 I think there is no point in having locks as well as DeepCopy. I think this line need to be removed

. This line may have been added when this file was autto generated.

Copy link
Author

Choose a reason for hiding this comment

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

I guess remove this line is effective,and less change,
but honesty,I am not exactly understand the intention of this line,set *out = *in for what。
other side,this line is generated by code-gen tools,maybe somebody will rerun code-gen tools,and overwrite the modification。
that is why I choose use locks。

@Revolyssup
Copy link
Contributor

@liushv0 Can you modify this PR to remove locks and instead apply this suggestion?#2168 (comment)

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.

None yet

3 participants