Skip to content

Commit 3e643c1

Browse files
authored
Fix data race of racingio Reader and Writer
1 parent c8d2795 commit 3e643c1

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

neo4j/internal/racingio/reader.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,9 @@ func (rr *racingReader) race(ctx context.Context, bytes []byte, readFn func(io.R
5252
return 0, wrapRaceError(err)
5353
}
5454
resultChan := make(chan *ioResult, 1)
55-
defer close(resultChan)
5655
go func() {
56+
defer close(resultChan)
5757
n, err := readFn(rr.reader, bytes)
58-
defer func() {
59-
// When the read operation completes, the outer function may have returned already.
60-
// In that situation, the channel will have been closed and the result emission will crash.
61-
// Let's just swallow the panic that may happen and ignore it
62-
_ = recover()
63-
}()
6458
resultChan <- &ioResult{
6559
n: n,
6660
err: err,

neo4j/internal/racingio/reader_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ func TestRacingReader(outer *testing.T) {
8585
})
8686

8787
outer.Run(fmt.Sprintf(`[%s] completes before read occurs`, testCase.qualifier), func(t *testing.T) {
88-
reader := &slowFailingReader{sleep: 2 * time.Minute}
88+
reader := &slowFailingReader{sleep: 50 * time.Millisecond}
8989
racingReader := rio.NewRacingReader(reader)
9090
result := make([]byte, 2)
91-
ctx, cancelFunc := context.WithTimeout(context.Background(), 200*time.Millisecond)
91+
ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Millisecond)
9292
defer cancelFunc()
9393

9494
n, err := testCase.readOperation(racingReader)(ctx, result)
@@ -102,7 +102,7 @@ func TestRacingReader(outer *testing.T) {
102102
})
103103

104104
outer.Run("connection read times out after 1 successful read", func(t *testing.T) {
105-
timeout := 400 * time.Millisecond
105+
timeout := 100 * time.Millisecond
106106
server, client := net.Pipe()
107107
defer closePipe(t, server, client)
108108
go func() {

testkit/unittests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ def run(args):
2121

2222
run(cmd + ["./neo4j"])
2323
run(cmd + ["./neo4j/internal/..."])
24+
# Repeat racing tests
25+
run(cmd + ["-race", "-count", "50", "./neo4j/internal/racingio"])

0 commit comments

Comments
 (0)