-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Fixes #5001 #5078
Fixes #5001 #5078
Changes from 3 commits
e8139fc
8cae2b2
0e1b634
fff8c7c
3330b4a
6c6880d
367cf4f
4907e18
8222642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,11 +319,18 @@ func (engine *PullEngine) OnRes(items []string, nonce uint64) { | |
} | ||
|
||
func (engine *PullEngine) newNONCE() uint64 { | ||
n := uint64(0) | ||
for { | ||
n = util.RandomUInt64() | ||
if !engine.outgoingNONCES.Exists(n) { | ||
engine.lock.Lock() | ||
defer engine.lock.Unlock() | ||
|
||
maxAttempts := 100 | ||
for i := 0; i < maxAttempts; i++ { | ||
n := util.RandomUInt64() | ||
if n != 0 && !engine.outgoingNONCES.Exists(n) { | ||
return n | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't try 100 attempts, but more attempts, like 1000,000. and if we still fail, then we should loop until we find a nonce that is not in use using the crypto rand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the second part it can result in slow nonces if this happens right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after looking at the code myself, I think we can just use |
||
// If we couldn't generate a unique NONCE after max attempts, | ||
// use time-based fallback | ||
return uint64(time.Now().UnixNano()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,8 @@ SPDX-License-Identifier: Apache-2.0 | |
package util | ||
|
||
import ( | ||
crand "crypto/rand" | ||
"fmt" | ||
"math/rand/v2" | ||
"math/rand" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change the library from v2 to the previous library? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, didn't mean to, changed back |
||
"reflect" | ||
"runtime" | ||
"sync" | ||
|
@@ -18,12 +17,13 @@ import ( | |
"github.com/spf13/viper" | ||
) | ||
|
||
var r *rand.Rand | ||
var ( | ||
r *rand.Rand | ||
rMutex sync.Mutex | ||
) | ||
|
||
func init() { // do we really need this? | ||
var seed [32]byte | ||
_, _ = crand.Read(seed[:]) | ||
r = rand.New(rand.NewChaCha8(seed)) | ||
r = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why would we seed this with a deterministic and non-random seed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, rolling back |
||
} | ||
|
||
// Equals returns whether a and b are the same | ||
|
@@ -179,14 +179,16 @@ func SetVal(key string, val interface{}) { | |
// RandomInt returns, as an int, a non-negative pseudo-random integer in [0,n) | ||
// It panics if n <= 0 | ||
func RandomInt(n int) int { | ||
return r.IntN(n) | ||
return r.Intn(n) | ||
} | ||
|
||
// RandomUInt64 returns a random uint64 | ||
// | ||
// If we want a rand that's non-global and specific to gossip, we can | ||
// establish one. Otherwise this uses the process-global locking RNG. | ||
func RandomUInt64() uint64 { | ||
rMutex.Lock() | ||
defer rMutex.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is the lock/unlock call only here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the panic that happens when there's a lot of concurrency: panic: runtime error: index out of range [-1] goroutine 3305 [running]: I suppose the problem is because we are accessing this function a lot of times, so adding a lock ensures that there are no concurrency issues There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If there is a panic stemming from gossip then I fail to see how is the chaincode related to the problem. We shouldn't fix two different problems in the same PR. Can you move all the non gossip related code changes to a new PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, but you see the problem, right? gossip is invoking randomUint64, and it panicking, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are right, these are two different problems for two different panic errors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. panic calls a different code than the one you're correcting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So @pfi79 compiling the code in main should fix the issue right about gossip right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's possible. After updating the package to v2 I stopped catching the panic error in tests. |
||
return r.Uint64() | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need to lock at all. We only create nonces from here and we already lock there.
Just loop until
engine.outgoingNONCES.Exists(n)
is false, no need for attempts or anything.