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

net: conn.Read();Contents that are concurrently written twice are read at a time. #42480

Closed
justkeepsimple opened this issue Nov 10, 2020 · 5 comments

Comments

@justkeepsimple
Copy link

@justkeepsimple justkeepsimple commented Nov 10, 2020

What version of Go are you using (go version)?

go1.14.6

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

i used two gorutines to write data from serve to client concurrently, but the data are read at a time.

write code one:
...
hb, err := json.Marshal(HeartBeatMsg)
if err != nil {
panic(errstd.JsonSerializerErr)
}
for {
time.Sleep(time.Second * 2)
SendMsgLock.Lock()
_, errTcp := sc.conn.Write(hb)
SendMsgLock.Unlock()
if errTcp != nil {
panic(errstd.CommunicationErr)
}
}
...

write code two:
...
req := &Message{MsgNo: no, Data: data}
bytes, err := json.Marshal(req)
if err != nil {
panic(errstd.JsonSerializerErr)
}
SendMsgLock.Lock()
_, errTcp := sc.conn.Write(bytes)
SendMsgLock.Unlock()
if errTcp != nil {
panic(errstd.CommunicationErr)
}
...

read code:
...
rBuf := make([]byte, ReadBufferSize)
read, err := sc.conn.Read(rBuf)
if err != nil {
sc.conn.Close()
break
}
content := &Message{}
errDes := json.Unmarshal(rBuf[0:read], content)
if errDes != nil {
fmt.Println(string(rBuf[0:read]))
panic(errstd.JsonDeserializerErr)
}
...

error:

string(rBuf[0:read]):
{"MsgNo":6,"Data":"eyJJbmRleCI6MTkzdFJlcSI6ZmFsc2V9"}{"MsgNo":0,"Data":null}

i hope that i can receive the data '{"MsgNo":6,"Data":"eyJJbmRleCI6MTkzdFJlcSI6ZmFsc2V9"}' after calling conn.Read(rBuf),
and receive the data '{"MsgNo":0,"Data":null}' after the next calling conn.Read(rBuf),

@mvdan
Copy link
Member

@mvdan mvdan commented Nov 10, 2020

Ask this question in the forums first; see https://golang.org/wiki/Questions. You should only open an issue if you believe you've found a bug, and then carefully fill the issue template. You omitted at least two sections of the template.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 10, 2020

Show us a complete standalone test case.

@justkeepsimple
Copy link
Author

@justkeepsimple justkeepsimple commented Nov 11, 2020

this is test case for go: https://github.com/justkeepsimple/socketRead_go
this is test case for java: https://github.com/justkeepsimple/socketRead_java
java`s result is my expect

@davecheney
Copy link
Contributor

@davecheney davecheney commented Nov 11, 2020

@justkeepsimple there is a bug in your code. This is the fix, justkeepsimple/socketRead_go#3

@justkeepsimple
Copy link
Author

@justkeepsimple justkeepsimple commented Nov 11, 2020

@justkeepsimple there is a bug in your code. This is the fix, justkeepsimple/socketRead_go#3

thx!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants