good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
websocket
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
github
nhooyr
websocket
Commits
f178ccfa
Unverified
Commit
f178ccfa
authored
Nov 5, 2019
by
Anmol Sethi
Committed by
GitHub
Nov 5, 2019
Browse files
Options
Downloads
Plain Diff
Merge pull request #169 from nhooyr/race
Fix race with c.readerShouldLock
parents
e36318f9
8b47056a
Branches
Branches containing commit
Tags
v1.7.3
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
conn.go
+40
-17
40 additions, 17 deletions
conn.go
with
40 additions
and
17 deletions
conn.go
+
40
−
17
View file @
f178ccfa
...
...
@@ -82,7 +82,6 @@ type Conn struct {
readerMsgHeader
header
readerFrameEOF
bool
readerMaskPos
int
readerShouldLock
bool
setReadTimeout
chan
context
.
Context
setWriteTimeout
chan
context
.
Context
...
...
@@ -237,6 +236,10 @@ func (c *Conn) readTillMsg(ctx context.Context) (header, error) {
if
h
.
opcode
.
controlOp
()
{
err
=
c
.
handleControl
(
ctx
,
h
)
if
err
!=
nil
{
// Pass through CloseErrors when receiving a close frame.
if
h
.
opcode
==
opClose
&&
CloseStatus
(
err
)
!=
-
1
{
return
header
{},
err
}
return
header
{},
fmt
.
Errorf
(
"failed to handle control frame %v: %w"
,
h
.
opcode
,
err
)
}
continue
...
...
@@ -445,7 +448,6 @@ func (c *Conn) reader(ctx context.Context, lock bool) (MessageType, io.Reader, e
c
.
readerFrameEOF
=
false
c
.
readerMaskPos
=
0
c
.
readMsgLeft
=
c
.
msgReadLimit
.
Load
()
c
.
readerShouldLock
=
lock
r
:=
&
messageReader
{
c
:
c
,
...
...
@@ -465,7 +467,11 @@ func (r *messageReader) eof() bool {
// Read reads as many bytes as possible into p.
func
(
r
*
messageReader
)
Read
(
p
[]
byte
)
(
int
,
error
)
{
n
,
err
:=
r
.
read
(
p
)
return
r
.
exportedRead
(
p
,
true
)
}
func
(
r
*
messageReader
)
exportedRead
(
p
[]
byte
,
lock
bool
)
(
int
,
error
)
{
n
,
err
:=
r
.
read
(
p
,
lock
)
if
err
!=
nil
{
// Have to return io.EOF directly for now, we cannot wrap as errors.Is
// isn't used widely yet.
...
...
@@ -477,17 +483,29 @@ func (r *messageReader) Read(p []byte) (int, error) {
return
n
,
nil
}
func
(
r
*
messageReader
)
read
(
p
[]
byte
)
(
int
,
error
)
{
if
r
.
c
.
readerShouldLock
{
err
:=
r
.
c
.
acquireLock
(
r
.
c
.
readerMsgCtx
,
r
.
c
.
readLock
)
if
err
!=
nil
{
return
0
,
err
func
(
r
*
messageReader
)
readUnlocked
(
p
[]
byte
)
(
int
,
error
)
{
return
r
.
exportedRead
(
p
,
false
)
}
func
(
r
*
messageReader
)
read
(
p
[]
byte
,
lock
bool
)
(
int
,
error
)
{
if
lock
{
// If we cannot acquire the read lock, then
// there is either a concurrent read or the close handshake
// is proceeding.
select
{
case
r
.
c
.
readLock
<-
struct
{}{}
:
defer
r
.
c
.
releaseLock
(
r
.
c
.
readLock
)
default
:
if
r
.
c
.
closing
.
Load
()
==
1
{
<-
r
.
c
.
closed
return
0
,
r
.
c
.
closeErr
}
return
0
,
errors
.
New
(
"concurrent read detected"
)
}
}
if
r
.
eof
()
{
return
0
,
fmt
.
Errorf
(
"cannot use EOFed reader"
)
return
0
,
errors
.
New
(
"cannot use EOFed reader"
)
}
if
r
.
c
.
readMsgLeft
<=
0
{
...
...
@@ -950,8 +968,6 @@ func (c *Conn) waitClose() error {
return
c
.
closeReceived
}
c
.
readerShouldLock
=
false
b
:=
bpool
.
Get
()
buf
:=
b
.
Bytes
()
buf
=
buf
[
:
cap
(
buf
)]
...
...
@@ -965,7 +981,8 @@ func (c *Conn) waitClose() error {
}
}
_
,
err
=
io
.
CopyBuffer
(
ioutil
.
Discard
,
c
.
activeReader
,
buf
)
r
:=
readerFunc
(
c
.
activeReader
.
readUnlocked
)
_
,
err
=
io
.
CopyBuffer
(
ioutil
.
Discard
,
r
,
buf
)
if
err
!=
nil
{
return
err
}
...
...
@@ -1019,6 +1036,12 @@ func (c *Conn) ping(ctx context.Context, p string) error {
}
}
type
readerFunc
func
(
p
[]
byte
)
(
int
,
error
)
func
(
f
readerFunc
)
Read
(
p
[]
byte
)
(
int
,
error
)
{
return
f
(
p
)
}
type
writerFunc
func
(
p
[]
byte
)
(
int
,
error
)
func
(
f
writerFunc
)
Write
(
p
[]
byte
)
(
int
,
error
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment