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: fix crash on selecting queue. #643

Merged
merged 1 commit into from
Apr 16, 2021
Merged

Conversation

NAND86
Copy link
Contributor

@NAND86 NAND86 commented Apr 8, 2021

What is the purpose of the change

fix crash on selecting queue.

Brief changelog

https://github.com/golang/go/blob/a7e16abb22f1b249d2691b32a5d20206282898f2/src/math/rand/rand.go#L7-L20

The default Source is safe for concurrent use by multiple goroutines, but Sources created by NewSource are not.
如上所述,math/rand中通过NewSource创建的rand方法不是线程安全的。多协程发送消息时,不加锁的情况下同时select queue会因数组越界导致panic。

panic如下:
panic: runtime error: index out of range [-1]

// Random numbers are generated by a Source. Top-level functions, such as
// Float64 and Int, use a default shared Source that produces a deterministic
// sequence of values each time a program is run. Use the Seed function to
// initialize the default Source if different behavior is required for each run.
// The default Source is safe for concurrent use by multiple goroutines, but
// Sources created by NewSource are not.
//
// Mathematical interval notation such as [0, n) is used throughout the
// documentation for this package.
//
// For random numbers suitable for security-sensitive work, see the crypto/rand
// package.
package rand

The default Source is safe for concurrent use by multiple goroutines, but Sources created by NewSource are not.
如上所述,使用NewSource 之后的rand不是线程安全的。多协程发送消息时,不加锁的情况下同时select queue会导致panic。
@maixiaohai
Copy link
Contributor

Plz create a related issue before submit a pr.

@wenfengwang
Copy link
Member

LGTM, will merge after an issue linked

@ShannonDing ShannonDing merged commit 2d513f3 into apache:master Apr 16, 2021
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