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.

274 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/pkg/errors"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. "github.com/tendermint/tendermint/libs/log"
  11. "github.com/tendermint/tendermint/config"
  12. "github.com/tendermint/tendermint/p2p/conn"
  13. )
  14. const testCh = 0x01
  15. //------------------------------------------------
  16. type mockNodeInfo struct {
  17. addr *NetAddress
  18. }
  19. func (ni mockNodeInfo) ID() ID { return ni.addr.ID }
  20. func (ni mockNodeInfo) NetAddress() (*NetAddress, error) { return ni.addr, nil }
  21. func (ni mockNodeInfo) Validate() error { return nil }
  22. func (ni mockNodeInfo) CompatibleWith(other NodeInfo) error { return nil }
  23. func AddPeerToSwitchPeerSet(sw *Switch, peer Peer) {
  24. sw.peers.Add(peer)
  25. }
  26. func CreateRandomPeer(outbound bool) *peer {
  27. addr, netAddr := CreateRoutableAddr()
  28. p := &peer{
  29. peerConn: peerConn{
  30. outbound: outbound,
  31. socketAddr: netAddr,
  32. },
  33. nodeInfo: mockNodeInfo{netAddr},
  34. mconn: &conn.MConnection{},
  35. metrics: NopMetrics(),
  36. }
  37. p.SetLogger(log.TestingLogger().With("peer", addr))
  38. return p
  39. }
  40. func CreateRoutableAddr() (addr string, netAddr *NetAddress) {
  41. for {
  42. var err error
  43. addr = fmt.Sprintf("%X@%v.%v.%v.%v:26656", cmn.RandBytes(20), cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256)
  44. netAddr, err = NewNetAddressString(addr)
  45. if err != nil {
  46. panic(err)
  47. }
  48. if netAddr.Routable() {
  49. break
  50. }
  51. }
  52. return
  53. }
  54. //------------------------------------------------------------------
  55. // Connects switches via arbitrary net.Conn. Used for testing.
  56. const TEST_HOST = "localhost"
  57. // MakeConnectedSwitches returns n switches, connected according to the connect func.
  58. // If connect==Connect2Switches, the switches will be fully connected.
  59. // initSwitch defines how the i'th switch should be initialized (ie. with what reactors).
  60. // NOTE: panics if any switch fails to start.
  61. func MakeConnectedSwitches(cfg *config.P2PConfig, n int, initSwitch func(int, *Switch) *Switch, connect func([]*Switch, int, int)) []*Switch {
  62. switches := make([]*Switch, n)
  63. for i := 0; i < n; i++ {
  64. switches[i] = MakeSwitch(cfg, i, TEST_HOST, "123.123.123", initSwitch)
  65. }
  66. if err := StartSwitches(switches); err != nil {
  67. panic(err)
  68. }
  69. for i := 0; i < n; i++ {
  70. for j := i + 1; j < n; j++ {
  71. connect(switches, i, j)
  72. }
  73. }
  74. return switches
  75. }
  76. // Connect2Switches will connect switches i and j via net.Pipe().
  77. // Blocks until a connection is established.
  78. // NOTE: caller ensures i and j are within bounds.
  79. func Connect2Switches(switches []*Switch, i, j int) {
  80. switchI := switches[i]
  81. switchJ := switches[j]
  82. c1, c2 := conn.NetPipe()
  83. doneCh := make(chan struct{})
  84. go func() {
  85. err := switchI.addPeerWithConnection(c1)
  86. if err != nil {
  87. panic(err)
  88. }
  89. doneCh <- struct{}{}
  90. }()
  91. go func() {
  92. err := switchJ.addPeerWithConnection(c2)
  93. if err != nil {
  94. panic(err)
  95. }
  96. doneCh <- struct{}{}
  97. }()
  98. <-doneCh
  99. <-doneCh
  100. }
  101. func (sw *Switch) addPeerWithConnection(conn net.Conn) error {
  102. pc, err := testInboundPeerConn(conn, sw.config, sw.nodeKey.PrivKey)
  103. if err != nil {
  104. if err := conn.Close(); err != nil {
  105. sw.Logger.Error("Error closing connection", "err", err)
  106. }
  107. return err
  108. }
  109. ni, err := handshake(conn, time.Second, sw.nodeInfo)
  110. if err != nil {
  111. if err := conn.Close(); err != nil {
  112. sw.Logger.Error("Error closing connection", "err", err)
  113. }
  114. return err
  115. }
  116. p := newPeer(
  117. pc,
  118. MConnConfig(sw.config),
  119. ni,
  120. sw.reactorsByCh,
  121. sw.chDescs,
  122. sw.StopPeerForError,
  123. )
  124. if err = sw.addPeer(p); err != nil {
  125. pc.CloseConn()
  126. return err
  127. }
  128. return nil
  129. }
  130. // StartSwitches calls sw.Start() for each given switch.
  131. // It returns the first encountered error.
  132. func StartSwitches(switches []*Switch) error {
  133. for _, s := range switches {
  134. err := s.Start() // start switch and reactors
  135. if err != nil {
  136. return err
  137. }
  138. }
  139. return nil
  140. }
  141. func MakeSwitch(
  142. cfg *config.P2PConfig,
  143. i int,
  144. network, version string,
  145. initSwitch func(int, *Switch) *Switch,
  146. opts ...SwitchOption,
  147. ) *Switch {
  148. nodeKey := NodeKey{
  149. PrivKey: ed25519.GenPrivKey(),
  150. }
  151. nodeInfo := testNodeInfo(nodeKey.ID(), fmt.Sprintf("node%d", i))
  152. addr, err := NewNetAddressString(
  153. IDAddressString(nodeKey.ID(), nodeInfo.(DefaultNodeInfo).ListenAddr),
  154. )
  155. if err != nil {
  156. panic(err)
  157. }
  158. t := NewMultiplexTransport(nodeInfo, nodeKey, MConnConfig(cfg))
  159. if err := t.Listen(*addr); err != nil {
  160. panic(err)
  161. }
  162. // TODO: let the config be passed in?
  163. sw := initSwitch(i, NewSwitch(cfg, t, opts...))
  164. sw.SetLogger(log.TestingLogger().With("switch", i))
  165. sw.SetNodeKey(&nodeKey)
  166. ni := nodeInfo.(DefaultNodeInfo)
  167. for ch := range sw.reactorsByCh {
  168. ni.Channels = append(ni.Channels, ch)
  169. }
  170. nodeInfo = ni
  171. // TODO: We need to setup reactors ahead of time so the NodeInfo is properly
  172. // populated and we don't have to do those awkward overrides and setters.
  173. t.nodeInfo = nodeInfo
  174. sw.SetNodeInfo(nodeInfo)
  175. return sw
  176. }
  177. func testInboundPeerConn(
  178. conn net.Conn,
  179. config *config.P2PConfig,
  180. ourNodePrivKey crypto.PrivKey,
  181. ) (peerConn, error) {
  182. return testPeerConn(conn, config, false, false, ourNodePrivKey, nil)
  183. }
  184. func testPeerConn(
  185. rawConn net.Conn,
  186. cfg *config.P2PConfig,
  187. outbound, persistent bool,
  188. ourNodePrivKey crypto.PrivKey,
  189. socketAddr *NetAddress,
  190. ) (pc peerConn, err error) {
  191. conn := rawConn
  192. // Fuzz connection
  193. if cfg.TestFuzz {
  194. // so we have time to do peer handshakes and get set up
  195. conn = FuzzConnAfterFromConfig(conn, 10*time.Second, cfg.TestFuzzConfig)
  196. }
  197. // Encrypt connection
  198. conn, err = upgradeSecretConn(conn, cfg.HandshakeTimeout, ourNodePrivKey)
  199. if err != nil {
  200. return pc, errors.Wrap(err, "Error creating peer")
  201. }
  202. // Only the information we already have
  203. return newPeerConn(outbound, persistent, conn, socketAddr), nil
  204. }
  205. //----------------------------------------------------------------
  206. // rand node info
  207. func testNodeInfo(id ID, name string) NodeInfo {
  208. return testNodeInfoWithNetwork(id, name, "testing")
  209. }
  210. func testNodeInfoWithNetwork(id ID, name, network string) NodeInfo {
  211. return DefaultNodeInfo{
  212. ProtocolVersion: defaultProtocolVersion,
  213. ID_: id,
  214. ListenAddr: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  215. Network: network,
  216. Version: "1.2.3-rc0-deadbeef",
  217. Channels: []byte{testCh},
  218. Moniker: name,
  219. Other: DefaultNodeInfoOther{
  220. TxIndex: "on",
  221. RPCAddress: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
  222. },
  223. }
  224. }
  225. func getFreePort() int {
  226. port, err := cmn.GetFreePort()
  227. if err != nil {
  228. panic(err)
  229. }
  230. return port
  231. }