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
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@justkeepsimple
Copy link

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 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.

@mvdan mvdan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 10, 2020
@ianlancetaylor
Copy link
Contributor

Show us a complete standalone test case.

@justkeepsimple
Copy link
Author

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

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

@justkeepsimple
Copy link
Author

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

thx!!!

@golang golang locked and limited conversation to collaborators Nov 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants