diff --git a/pkg/core/region.go b/pkg/core/region.go index be8fcddc179..affe6e14406 100644 --- a/pkg/core/region.go +++ b/pkg/core/region.go @@ -1041,10 +1041,10 @@ func (r *RegionsInfo) CheckAndPutRootTree(ctx *MetaProcessContext, region *Regio // Usually used with CheckAndPutRootTree together. func (r *RegionsInfo) CheckAndPutSubTree(region *RegionInfo) { // new region get from root tree again - var newRegion *RegionInfo - newRegion = r.GetRegion(region.GetID()) + newRegion := r.GetRegion(region.GetID()) if newRegion == nil { - newRegion = region + // Make sure there is this region in the root tree, so as to ensure the correctness of reference count + return } r.UpdateSubTreeOrderInsensitive(newRegion) } diff --git a/pkg/core/region_test.go b/pkg/core/region_test.go index 43629fccda0..4a418a16ebc 100644 --- a/pkg/core/region_test.go +++ b/pkg/core/region_test.go @@ -1021,3 +1021,12 @@ func TestUpdateRegionEventualConsistency(t *testing.T) { re.Equal(int32(2), item.GetRef()) } } + +func TestCheckAndPutSubTree(t *testing.T) { + re := require.New(t) + regions := NewRegionsInfo() + region := NewTestRegionInfo(1, 1, []byte("a"), []byte("b")) + regions.CheckAndPutSubTree(region) + // should failed to put because the root tree is missing + re.Equal(0, regions.tree.length()) +}