Skip to content

Commit

Permalink
syncer(dm): fix the data race issue (#5881) (#5959)
Browse files Browse the repository at this point in the history
close #4811
  • Loading branch information
ti-chi-bot authored Jul 4, 2022
1 parent 0c79c15 commit 781562b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion dm/dm/worker/subtask.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,13 @@ func (st *SubTask) markResultCanceled() bool {
func (st *SubTask) Result() *pb.ProcessResult {
st.RLock()
defer st.RUnlock()
return st.result
if st.result == nil {
return nil
}
tempProcessResult, _ := st.result.Marshal()
newProcessResult := &pb.ProcessResult{}
_ = newProcessResult.Unmarshal(tempProcessResult)
return newProcessResult
}

// Close stops the sub task.
Expand Down
31 changes: 31 additions & 0 deletions dm/dm/worker/subtask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,34 @@ func (t *testSubTask) TestSubtaskFastQuit(c *C) {
}
c.Assert(st.Stage(), Equals, pb.Stage_Stopped)
}

func (t *testSubTask) TestSubtaskRace(c *C) {
// to test data race of Marshal() and markResultCanceled()
tempErrors := []*pb.ProcessError{}
tempDetail := []byte{}
tempProcessResult := pb.ProcessResult{
IsCanceled: false,
Errors: tempErrors,
Detail: tempDetail,
}
cfg := &config.SubTaskConfig{
Name: "testSubtaskScene",
Mode: config.ModeFull,
}
st := NewSubTask(cfg, nil, "worker")
c.Assert(st.Stage(), DeepEquals, pb.Stage_New)
st.result = &tempProcessResult
tempQueryStatusResponse := pb.QueryStatusResponse{}
tempQueryStatusResponse.SubTaskStatus = make([]*pb.SubTaskStatus, 1)
tempSubTaskStatus := pb.SubTaskStatus{}
tempSubTaskStatus.Result = st.Result()
tempQueryStatusResponse.SubTaskStatus[0] = &tempSubTaskStatus
st.result.IsCanceled = false
go func() {
for i := 0; i < 10; i++ {
_, _ = tempQueryStatusResponse.Marshal()
}
}()
st.markResultCanceled()
// this test is to test data race, so don't need assert here
}

0 comments on commit 781562b

Please sign in to comment.