-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
peer: test for startup writeHander data race #8198
peer: test for startup writeHander data race #8198
Conversation
We use unsynchronized counters to trigger a report under the race detector if multiple reads or writes happen concurrently.
The test reliably detects lightningnetwork#8184.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🐿️
I removed that earlier patch and confirms that when run with -race
this reliably fails:
⛰ go test -v -run=TestStartupWriteMessageRace -race
=== RUN TestStartupWriteMessageRace
=== PAUSE TestStartupWriteMessageRace
=== CONT TestStartupWriteMessageRace
==================
WARNING: DATA RACE
Read at 0x00c00010ec70 by goroutine 15:
github.com/lightningnetwork/lnd/peer.(*mockMessageConn).SetWriteDeadline()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/peer/test_utils.go:522 +0x34
github.com/lightningnetwork/lnd/peer.(*Brontide).writeMessage.func1()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/peer/brontide.go:2114 +0x70
github.com/lightningnetwork/lnd/peer.(*Brontide).writeMessage()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/peer/brontide.go:2159 +0x428
github.com/lightningnetwork/lnd/peer.(*Brontide).Start()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/peer/brontide.go:727 +0xe14
github.com/lightningnetwork/lnd/peer.TestStartupWriteMessageRace()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/peer/brontide_test.go:1441 +0xf7c
testing.tRunner()
/Users/roasbeef/go/src/testing/testing.go:1595 +0x194
testing.(*T).Run.func1()
/Users/roasbeef/go/src/testing/testing.go:1648 +0x40
Previous write at 0x00c00010ec70 by goroutine 27:
github.com/lightningnetwork/lnd/peer.(*mockMessageConn).WriteMessage()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/peer/test_utils.go:535 +0x58
github.com/lightningnetwork/lnd/peer.(*Brontide).writeMessage.func2()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/peer/brontide.go:2153 +0xb8
github.com/lightningnetwork/lnd/peer.(*Brontide).writeMessage.(*Write).Submit.func3()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/pool/write.go:53 +0x68
github.com/lightningnetwork/lnd/pool.(*Worker).spawnWorker()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/pool/worker.go:209 +0x27c
github.com/lightningnetwork/lnd/pool.(*Worker).requestHandler.func2()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/pool/worker.go:161 +0x40
Goroutine 15 (running) created at:
testing.(*T).Run()
/Users/roasbeef/go/src/testing/testing.go:1648 +0x5d8
testing.runTests.func1()
/Users/roasbeef/go/src/testing/testing.go:2054 +0x80
testing.tRunner()
/Users/roasbeef/go/src/testing/testing.go:1595 +0x194
testing.runTests()
/Users/roasbeef/go/src/testing/testing.go:2052 +0x6d8
testing.(*M).Run()
/Users/roasbeef/go/src/testing/testing.go:1925 +0x904
main.main()
_testmain.go:71 +0x294
Goroutine 27 (running) created at:
github.com/lightningnetwork/lnd/pool.(*Worker).requestHandler()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/pool/worker.go:161 +0x274
github.com/lightningnetwork/lnd/pool.(*Write).Start.(*Worker).Start.func1.1()
/Users/roasbeef/gocode/src/github.com/lightningnetwork/lnd/pool/worker.go:99 +0x34
==================
testing.go:1465: race detected during execution of test
--- FAIL: TestStartupWriteMessageRace (0.28s)
FAIL
exit status 1
FAIL github.com/lightningnetwork/lnd/peer 0.856s
// writeRaceDetectingCounter is incremented on any function call | ||
// associated with writing to the connection. The race detector will | ||
// trigger on this counter if a data race exists. | ||
writeRaceDetectingCounter int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh nice little trick: unsafe concurrent access here will cause the race detector to fire!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM🙏 Though I'd prefer using mock.Mock
to properly mock the interface and build tests based on the mockers, I guess that requires too much work at the moment. Also like the trick!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🦖
The test case reliably triggers #8184 under the race detector.
Tested: