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

peer: test for startup writeHander data race #8198

Merged

Conversation

morehouse
Copy link
Collaborator

The test case reliably triggers #8184 under the race detector.

Tested:

$ make unit-race pkg=peer
...
ok      github.com/lightningnetwork/lnd/peer    8.960s

$ git revert 7556402ea44048393bda99d257f60853521e077e

$ make unit-race pkg=peer
...
WARNING: DATA RACE 
Read at 0x00c0001e6090 by goroutine 85:
...
--- FAIL: TestStartupWriteMessageRace (0.10s) 

We use unsynchronized counters to trigger a report under the race
detector if multiple reads or writes happen concurrently.
Copy link
Member

@Roasbeef Roasbeef left a 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
Copy link
Member

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!

Copy link
Member

@yyforyongyu yyforyongyu left a 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!

Copy link
Collaborator

@Crypt-iQ Crypt-iQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🦖

@Roasbeef Roasbeef disabled auto-merge November 18, 2023 00:01
@Roasbeef Roasbeef merged commit d9b88fb into lightningnetwork:master Nov 18, 2023
21 of 26 checks passed
@morehouse morehouse deleted the brontide_startup_race_test branch November 20, 2023 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants