You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
6.5 KiB

fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/tendermint/tendermint/crypto"
  7. "github.com/tendermint/tendermint/crypto/ed25519"
  8. cmn "github.com/tendermint/tendermint/libs/common"
  9. "github.com/tendermint/tendermint/libs/log"
  10. "github.com/tendermint/tendermint/config"
  11. "github.com/tendermint/tendermint/p2p/conn"
  12. )
  13. const testCh = 0x01
  14. //------------------------------------------------
  15. type mockNodeInfo struct {
  16. addr *NetAddress
  17. }
  18. func (ni mockNodeInfo) ID() ID { return ni.addr.ID }
  19. func (ni mockNodeInfo) NetAddress() (*NetAddress, error) { return ni.addr, nil }
  20. func (ni mockNodeInfo) Validate() error { return nil }
  21. func (ni mockNodeInfo) CompatibleWith(other NodeInfo) error { return nil }
  22. func AddPeerToSwitch(sw *Switch, peer Peer) {
  23. sw.peers.Add(peer)
  24. }
  25. func CreateRandomPeer(outbound bool) *peer {
  26. addr, netAddr := CreateRoutableAddr()
  27. p := &peer{
  28. peerConn: peerConn{
  29. outbound: outbound,
  30. socketAddr: netAddr,
  31. },
  32. nodeInfo: mockNodeInfo{netAddr},
  33. mconn: &conn.MConnection{},
  34. metrics: NopMetrics(),
  35. }
  36. p.SetLogger(log.TestingLogger().With("peer", addr))
  37. return p
  38. }
  39. func CreateRoutableAddr() (addr string, netAddr *NetAddress) {
  40. for {
  41. var err error
  42. addr = fmt.Sprintf("%X@%v.%v.%v.%v:26656", cmn.RandBytes(20), cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256)
  43. netAddr, err = NewNetAddressString(addr)
  44. if err != nil {
  45. panic(err)
  46. }
  47. if netAddr.Routable() {
  48. break
  49. }
  50. }
  51. return
  52. }
  53. //------------------------------------------------------------------
  54. // Connects switches via arbitrary net.Conn. Used for testing.
  55. const TEST_HOST = "localhost"
  56. // MakeConnectedSwitches returns n switches, connected according to the connect func.
  57. // If connect==Connect2Switches, the switches will be fully connected.
  58. // initSwitch defines how the i'th switch should be initialized (ie. with what reactors).
  59. // NOTE: panics if any switch fails to start.
  60. func MakeConnectedSwitches(cfg *config.P2PConfig, n int, initSwitch func(int, *Switch) *Switch, connect func([]*Switch, int, int)) []*Switch {
  61. switches := make([]*Switch, n)
  62. for i := 0; i < n; i++ {
  63. switches[i] = MakeSwitch(cfg, i, TEST_HOST, "123.123.123", initSwitch)
  64. }
  65. if err := StartSwitches(switches); err != nil {
  66. panic(err)
  67. }
  68. for i := 0; i < n; i++ {
  69. for j := i + 1; j < n; j++ {
  70. connect(switches, i, j)
  71. }
  72. }
  73. return switches
  74. }
  75. // Connect2Switches will connect switches i and j via net.Pipe().
  76. // Blocks until a connection is established.
  77. // NOTE: caller ensures i and j are within bounds.
  78. func Connect2Switches(switches []*Switch, i, j int) {
  79. switchI := switches[i]
  80. switchJ := switches[j]
  81. c1, c2 := conn.NetPipe()
  82. doneCh := make(chan struct{})
  83. go func() {
  84. err := switchI.addPeerWithConnection(c1)
  85. if err != nil {
  86. panic(err)
  87. }
  88. doneCh <- struct{}{}
  89. }()
  90. go func() {
  91. err := switchJ.addPeerWithConnection(c2)
  92. if err != nil {
  93. panic(err)
  94. }
  95. doneCh <- struct{}{}
  96. }()
  97. <-doneCh
  98. <-doneCh
  99. }
  100. func (sw *Switch) addPeerWithConnection(conn net.Conn) error {
  101. pc, err := testInboundPeerConn(conn, sw.config, sw.nodeKey.PrivKey)
  102. if err != nil {
  103. if err := conn.Close(); err != nil {
  104. sw.Logger.Error("Error closing connection", "err", err)
  105. }
  106. return err
  107. }
  108. ni, err := handshake(conn, time.Second, sw.nodeInfo)
  109. if err != nil {
  110. if err := conn.Close(); err != nil {
  111. sw.Logger.Error("Error closing connection", "err", err)
  112. }
  113. return err
  114. }
  115. p := newPeer(
  116. pc,
  117. MConnConfig(sw.config),
  118. ni,
  119. sw.reactorsByCh,
  120. sw.chDescs,
  121. sw.StopPeerForError,
  122. )
  123. if err = sw.addPeer(p); err != nil {
  124. pc.CloseConn()
  125. return err
  126. }
  127. return nil
  128. }
  129. // StartSwitches calls sw.Start() for each given switch.
  130. // It returns the first encountered error.
  131. func StartSwitches(switches []*Switch) error {
  132. for _, s := range switches {
  133. err := s.Start() // start switch and reactors
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. return nil
  139. }
  140. func MakeSwitch(
  141. cfg *config.P2PConfig,
  142. i int,
  143. network, version string,
  144. initSwitch func(int, *Switch) *Switch,
  145. opts ...SwitchOption,
  146. ) *Switch {
  147. nodeKey := NodeKey{
  148. PrivKey: ed25519.GenPrivKey(),
  149. }
  150. nodeInfo := testNodeInfo(nodeKey.ID(), fmt.Sprintf("node%d", i))
  151. addr, err := NewNetAddressString(
  152. IDAddressString(nodeKey.ID(), nodeInfo.(DefaultNodeInfo).ListenAddr),
  153. )
  154. if err != nil {
  155. panic(err)
  156. }
  157. t := NewMultiplexTransport(nodeInfo, nodeKey, MConnConfig(cfg))
  158. if err := t.Listen(*addr); err != nil {
  159. panic(err)
  160. }
  161. // TODO: let the config be passed in?
  162. sw := initSwitch(i, NewSwitch(cfg, t, opts...))
  163. sw.SetLogger(log.TestingLogger().With("switch", i))
  164. sw.SetNodeKey(&nodeKey)
  165. ni := nodeInfo.(DefaultNodeInfo)
  166. for ch := range sw.reactorsByCh {
  167. ni.Channels = append(ni.Channels, ch)
  168. }
  169. nodeInfo = ni
  170. // TODO: We need to setup reactors ahead of time so the NodeInfo is properly
  171. // populated and we don't have to do those awkward overrides and setters.
  172. t.nodeInfo = nodeInfo
  173. sw.SetNodeInfo(nodeInfo)
  174. return sw
  175. }
  176. func testInboundPeerConn(
  177. conn net.Conn,
  178. config *config.P2PConfig,
  179. ourNodePrivKey crypto.PrivKey,
  180. ) (peerConn, error) {
  181. return testPeerConn(conn, config, false, false, ourNodePrivKey, nil)
  182. }
  183. func testPeerConn(
  184. rawConn net.Conn,
  185. cfg *config.P2PConfig,
  186. outbound, persistent bool,
  187. ourNodePrivKey crypto.PrivKey,
  188. socketAddr *NetAddress,
  189. ) (pc peerConn, err error) {
  190. conn := rawConn
  191. // Fuzz connection
  192. if cfg.TestFuzz {
  193. // so we have time to do peer handshakes and get set up
  194. conn = FuzzConnAfterFromConfig(conn, 10*time.Second, cfg.TestFuzzConfig)
  195. }
  196. // Encrypt connection
  197. conn, err = upgradeSecretConn(conn, cfg.HandshakeTimeout, ourNodePrivKey)
  198. if err != nil {
  199. return pc, cmn.ErrorWrap(err, "Error creating peer")
  200. }
  201. // Only the information we already have
  202. return newPeerConn(outbound, persistent, conn, socketAddr), nil
  203. }
  204. //----------------------------------------------------------------
  205. // rand node info
  206. func testNodeInfo(id ID, name string) NodeInfo {
  207. return testNodeInfoWithNetwork(id, name, "testing")
  208. }
  209. func testNodeInfoWithNetwork(id ID, name, network string) NodeInfo {
  210. return DefaultNodeInfo{
  211. ProtocolVersion: defaultProtocolVersion,
  212. ID_: id,
  213. ListenAddr: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  214. Network: network,
  215. Version: "1.2.3-rc0-deadbeef",
  216. Channels: []byte{testCh},
  217. Moniker: name,
  218. Other: DefaultNodeInfoOther{
  219. TxIndex: "on",
  220. RPCAddress: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  221. },
  222. }
  223. }
  224. func getFreePort() int {
  225. port, err := cmn.GetFreePort()
  226. if err != nil {
  227. panic(err)
  228. }
  229. return port
  230. }