|
| 1 | +diff --git a/gossip/discovery/discovery_test.go b/gossip/discovery/discovery_test.go |
| 2 | +index 01b80fc4f..34d0f968b 100644 |
| 3 | +--- a/gossip/discovery/discovery_test.go |
| 4 | ++++ b/gossip/discovery/discovery_test.go |
| 5 | +@@ -1975,3 +1975,60 @@ func TestHandleAliveMessage_RelearnsMemberAfterConcurrentPurge(t *testing.T) { |
| 6 | + require.True(t, inID2Member, "member should be present in id2Member after re-learning") |
| 7 | + require.True(t, inAliveLastTS, "member should be present in aliveLastTS after re-learning") |
| 8 | + } |
| 9 | ++ |
| 10 | ++func TestLearnExistingMembers_NilMemberAfterConcurrentPurge(t *testing.T) { |
| 11 | ++ // 1) Initialize a discovery instance (use existing helpers like createDiscoveryInstanceWithNoGossip). |
| 12 | ++ inst := createDiscoveryInstanceWithNoGossip(10000, "testInst", nil) |
| 13 | ++ defer inst.Stop() |
| 14 | ++ |
| 15 | ++ // Access the underlying implementation |
| 16 | ++ d := inst.discoveryImpl() |
| 17 | ++ |
| 18 | ++ // 2) Prepare a PKIid and endpoint. |
| 19 | ++ pkiID := common.PKIidType("test-pki-id") |
| 20 | ++ endpoint := "localhost:1234" |
| 21 | ++ |
| 22 | ++ // 3) Under lock, insert an entry into aliveLastTS for that PKIid. |
| 23 | ++ // 4) Do NOT insert the corresponding entry into id2Member (simulate it being purged). |
| 24 | ++ d.lock.Lock() |
| 25 | ++ d.aliveLastTS[string(pkiID)] = ×tamp{ |
| 26 | ++ incTime: time.Now(), |
| 27 | ++ seqNum: 1, |
| 28 | ++ lastSeen: time.Now(), |
| 29 | ++ } |
| 30 | ++ d.lock.Unlock() |
| 31 | ++ |
| 32 | ++ // 5) Build a valid AliveMessage and wrap it with protoext.NoopSign. |
| 33 | ++ aliveMsg := &proto.GossipMessage{ |
| 34 | ++ Tag: proto.GossipMessage_EMPTY, |
| 35 | ++ Content: &proto.GossipMessage_AliveMsg{ |
| 36 | ++ AliveMsg: &proto.AliveMessage{ |
| 37 | ++ Membership: &proto.Member{ |
| 38 | ++ PkiId: pkiID, |
| 39 | ++ Endpoint: endpoint, |
| 40 | ++ }, |
| 41 | ++ Timestamp: &proto.PeerTime{ |
| 42 | ++ IncNum: uint64(time.Now().UnixNano()), |
| 43 | ++ SeqNum: 2, |
| 44 | ++ }, |
| 45 | ++ }, |
| 46 | ++ }, |
| 47 | ++ } |
| 48 | ++ signedMsg, err := protoext.NoopSign(aliveMsg) |
| 49 | ++ require.NoError(t, err) |
| 50 | ++ |
| 51 | ++ // We invoke learnExistingMembers() directly to deterministically reproduce |
| 52 | ++ // the inconsistent state where the member is present in aliveLastTS but |
| 53 | ++ // missing from id2Member. |
| 54 | ++ // |
| 55 | ++ // In the real flow, handleAliveMessage first reads state under a read lock, |
| 56 | ++ // and then learnExistingMembers acquires a write lock. A concurrent purge |
| 57 | ++ // can remove the member between these two steps, leading to a nil access. |
| 58 | ++ // |
| 59 | ++ // Reproducing this via the full handleAliveMessage path would require a |
| 60 | ++ // timing-dependent race, so we simulate the exact post-condition directly |
| 61 | ++ // to keep the test deterministic and reliable. |
| 62 | ++ require.NotPanics(t, func() { |
| 63 | ++ d.learnExistingMembers([]*protoext.SignedGossipMessage{signedMsg}) |
| 64 | ++ }, "learnExistingMembers should not panic when member is nil in id2Member") |
| 65 | ++} |
0 commit comments