mirror of
https://github.com/rulego/gflow-engine.git
synced 2026-12-06 08:02:05 +00:00
fix: 修复实例孤儿/end去重锁故障卡死/子流程panic兜底
1. M4(中危)实例创建先于流程定义编译,initExecution 失败遗留 active 孤儿实例:
- startInstanceCore 原顺序为"先 instanceDAO.Create 再 initExecution",定义
非法时 Create 已落库、再 initExecution 报错返回,留下"已建但永不驱动"的
active 实例(卡死且无法续跑)。
- 修复:非草稿路径先 initExecution(校验/编译定义)后 Create;编译失败直接
返回错误、不落库。
2. M9(中危)end 节点去重锁服务异常时流程被卡死:
- Before 在 TryLock 报错时"保守放行"却不写 KeyEndExecLock,After 因
KeyEndExecLock=="" 跳过实例完成归档 → 锁故障反而让流程永久卡死,
与"宁可重复执行也不能卡死"的意图相悖。
- 修复:锁异常分支写入非空占位 KeyEndExecLock,让 After 走正常收尾
(Unlock 凭证不匹配仅 Debug 日志、无副作用)。
3. L6(低危)子流程 OnMsg panic 仅记日志不标记子实例失败:
- 子实例卡死在 active,父 subProcess 节点收不到子终止回调而永久挂起。
- 修复:panic 时以独立 ctx + 内部模式调用 TerminateProcessInstance 终止
子实例(归档 + 级联终止 + 触发父 subProcess 失败重入);终止失败仅告警。
测试:
- TestStartInstanceCore_InitFailureLeavesNoOrphanInstance:定义编译失败时
wf_instance 无孤儿行。
- TestTaskCreator_EndDedupLockErrorStillCompletesInstance:锁故障时 end 仍
完成实例归档(Before 写占位 + After 调 CompleteProcessInstance)。
This commit is contained in:
5 files changed
+152
-8
No files matched your search
@@ -127,8 +127,12 @@ func (aspect *TaskCreator) Before(ctx types.RuleContext, msg types.RuleMsg, rela
|
||||
if ctx.Self().Type() == types.NodeTypeEnd {
|
||||
lockVal, ok, err := aspect.endDedupLocker().TryLock(ctx.GetContext(), endNodeLockKey(instanceId), endDedupTTL)
|
||||
if err != nil {
|
||||
// 锁服务异常时保守放行(宁可重复执行也不能卡死流程)
|
||||
logrus.WithError(err).Warnf("end-node dedup TryLock error, allow execution, instanceId: %s", instanceId)
|
||||
// 锁服务异常时保守放行(宁可重复执行也不能卡死流程)。但必须写入非空
|
||||
// KeyEndExecLock 标记,否则 After 因"无锁值"会跳过实例完成归档,流程反而
|
||||
// 永久卡死。占位值让 After 走正常收尾;其 Unlock 因凭证不匹配失败,
|
||||
// 仅 Debug 级日志、无副作用。
|
||||
msg.GetMetadata().PutValue(constants.KeyEndExecLock, "end-dedup-unavailable")
|
||||
logrus.WithError(err).Warnf("end-node dedup TryLock error, allow execution without dedup, instanceId: %s", instanceId)
|
||||
} else if ok {
|
||||
msg.GetMetadata().PutValue(constants.KeyEndExecLock, lockVal)
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/rulego/gflow-engine/dao"
|
||||
"github.com/rulego/gflow-engine/model"
|
||||
"github.com/rulego/gflow-engine/types/constants"
|
||||
"github.com/rulego/gflow-engine/utils/lock"
|
||||
"github.com/rulego/rulego/api/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// M9 回归替身:嵌入 nil 接口、只覆写被测路径实际调用的方法。
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type endNodeCtx struct{ types.NodeCtx }
|
||||
|
||||
func (f *endNodeCtx) Type() string { return types.NodeTypeEnd }
|
||||
|
||||
type fakeRuleContext struct {
|
||||
types.RuleContext
|
||||
node types.NodeCtx
|
||||
selfID string
|
||||
}
|
||||
|
||||
func (f *fakeRuleContext) Self() types.NodeCtx { return f.node }
|
||||
func (f *fakeRuleContext) GetSelfId() string { return f.selfID }
|
||||
func (f *fakeRuleContext) GetContext() context.Context { return context.Background() }
|
||||
func (f *fakeRuleContext) RuleChain() types.NodeCtx { return nil }
|
||||
|
||||
type fakeTaskService struct{ TaskService }
|
||||
|
||||
func (f *fakeTaskService) CreateTask(_ context.Context, _ Actor, task *model.WfTask) (string, error) {
|
||||
task.ID = "task-1"
|
||||
return "task-1", nil
|
||||
}
|
||||
|
||||
func (f *fakeTaskService) Complete(_ context.Context, _ Actor, _ string, _ map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeRuntimeService struct {
|
||||
RuntimeService
|
||||
completed string
|
||||
}
|
||||
|
||||
func (f *fakeRuntimeService) CompleteProcessInstance(_ context.Context, _ Actor, id, _ string) error {
|
||||
f.completed = id
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeEngine struct {
|
||||
WorkflowEngine
|
||||
locker lock.Locker
|
||||
taskSvc TaskService
|
||||
rtSvc RuntimeService
|
||||
}
|
||||
|
||||
func (f *fakeEngine) GetLocker() lock.Locker { return f.locker }
|
||||
func (f *fakeEngine) GetTaskService() TaskService { return f.taskSvc }
|
||||
func (f *fakeEngine) GetRuntimeService() RuntimeService { return f.rtSvc }
|
||||
func (f *fakeEngine) GetRuleChainExecutor() RuleChainExecutor { return nil }
|
||||
|
||||
// M9 回归:end 节点去重锁服务异常时,仍需放行 end 收尾(完成实例归档),
|
||||
// 而非因"无锁值"被 After 跳过、把流程卡死在 active。
|
||||
func TestTaskCreator_EndDedupLockErrorStillCompletesInstance(t *testing.T) {
|
||||
q := rtImplTestDB(t)
|
||||
rtSvc := &fakeRuntimeService{}
|
||||
aspect := &TaskCreator{
|
||||
instanceDAO: dao.NewInstanceDAOWithQuery(q),
|
||||
workflowEngine: &fakeEngine{
|
||||
locker: &failingLocker{},
|
||||
taskSvc: &fakeTaskService{},
|
||||
rtSvc: rtSvc,
|
||||
},
|
||||
}
|
||||
|
||||
md := types.NewMetadata()
|
||||
md.PutValue(constants.KeyInstanceID, "inst-end")
|
||||
md.PutValue(constants.KeyProcessID, "proc-1")
|
||||
md.PutValue(constants.KeyTenantID, "t1")
|
||||
msg := types.NewMsg(0, "wf", types.JSON, md, `{}`)
|
||||
rctx := &fakeRuleContext{node: &endNodeCtx{}, selfID: "end1"}
|
||||
|
||||
// Before:锁服务异常时必须写入非空 KeyEndExecLock(保守放行 end 执行),
|
||||
// 否则 After 的 `KeyEndExecLock == ""` 判定会跳过实例完成。
|
||||
out := aspect.Before(rctx, msg, types.Success)
|
||||
require.NotEmpty(t, out.GetMetadata().GetValue(constants.KeyEndExecLock),
|
||||
"lock service error must still mark KeyEndExecLock")
|
||||
|
||||
// After:锁服务异常时实例仍应被完成归档。
|
||||
aspect.After(rctx, out, nil, types.Success)
|
||||
require.Equal(t, "inst-end", rtSvc.completed, "instance must complete even when end-dedup lock errors")
|
||||
}
|
||||
@@ -238,6 +238,19 @@ func (s *RuntimeServiceImpl) startInstanceCore(ctx context.Context, processDef *
|
||||
instance.ParentID = &parentInstanceID
|
||||
}
|
||||
|
||||
// 先装配执行引擎(校验/编译流程定义)再落库实例:定义非法时直接返回错误,
|
||||
// 不留下"已建但永不驱动"的 active 孤儿实例(卡死且无法被驱动)。草稿无需驱动,跳过装配。
|
||||
var (
|
||||
engine types.RuleEngine
|
||||
derr error
|
||||
)
|
||||
if !isDraft {
|
||||
engine, derr = s.initExecution(processDef.TenantID, processDef.ID, processDef.DefinitionJSON)
|
||||
if derr != nil {
|
||||
return "", nil, types.RuleMsg{}, derr
|
||||
}
|
||||
}
|
||||
|
||||
// 保存流程实例
|
||||
if err := s.instanceDAO.Create(ctx, instance); err != nil {
|
||||
// 唯一约束兜底:并发同 businessKey 双发起时先查会双双通过,靠数据库
|
||||
@@ -253,11 +266,6 @@ func (s *RuntimeServiceImpl) startInstanceCore(ctx context.Context, processDef *
|
||||
if isDraft {
|
||||
return instanceID, nil, types.RuleMsg{}, nil
|
||||
}
|
||||
|
||||
engine, err := s.initExecution(processDef.TenantID, processDef.ID, processDef.DefinitionJSON)
|
||||
if err != nil {
|
||||
return "", nil, types.RuleMsg{}, err
|
||||
}
|
||||
md := types.NewMetadata()
|
||||
md.PutValue(constants.KeyTenantID, processDef.TenantID)
|
||||
md.PutValue(constants.KeyInstanceID, instanceID)
|
||||
|
||||
@@ -407,6 +407,33 @@ func TestExecuteNext_MissingInstanceReturnsNil(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// M4 回归:流程定义编译失败(initExecution 报错)时,不得留下"已建但永不驱动"的
|
||||
// active 孤儿实例——它卡死又无法续跑。修复顺序:先 initExecution 后落库。
|
||||
func TestStartInstanceCore_InitFailureLeavesNoOrphanInstance(t *testing.T) {
|
||||
q := rtImplTestDB(t)
|
||||
instDAO := dao.NewInstanceDAOWithQuery(q)
|
||||
svc := &RuntimeServiceImpl{
|
||||
instanceDAO: instDAO,
|
||||
idGenerator: &testSeqIDGen{},
|
||||
}
|
||||
|
||||
procDef := &model.WfProcess{
|
||||
ID: "proc-bad-def",
|
||||
ProcessKey: "bad-def",
|
||||
Name: "bad def",
|
||||
TenantID: "t1",
|
||||
DefinitionJSON: "{not-valid-json", // rulego 解析必然失败
|
||||
}
|
||||
|
||||
_, _, _, err := svc.startInstanceCore(context.Background(), procDef,
|
||||
Actor{UserID: "u1", TenantID: "t1"}, "", nil, false, "")
|
||||
require.Error(t, err)
|
||||
|
||||
cnt, cerr := q.WfInstance.WithContext(context.Background()).Count()
|
||||
require.NoError(t, cerr)
|
||||
require.Equal(t, int64(0), cnt, "initExecution 失败不应落库孤儿实例")
|
||||
}
|
||||
|
||||
// 批量写变量走行锁事务:并发写不丢更新。
|
||||
func TestSetProcessInstanceVariables_ConcurrentMerge(t *testing.T) {
|
||||
q := rtImplTestDB(t)
|
||||
|
||||
@@ -49,11 +49,19 @@ func (s *RuntimeServiceImpl) StartSubProcessInstance(ctx context.Context, parent
|
||||
// 异步驱动同时避免子流程同步完成重入父 OnMsg。
|
||||
s.subProcessParentNodes.Store(childID, parentNodeID)
|
||||
if engine != nil {
|
||||
// panic 不能打挂宿主进程;失败要留痕,否则子流程静默不启动无从排查
|
||||
// panic 不能打挂宿主进程;失败要留痕并终止子实例,否则子实例卡死在 active、
|
||||
// 父 subProcess 节点也因收不到子完成/终止回调而永久挂起。
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logrus.Errorf("subProcess child %s OnMsg panicked: %v", childID, r)
|
||||
// 用独立 context 兜底(调用方 ctx 可能已被取消)、以内部模式终止子实例:
|
||||
// 归档 + 级联终止 + 触发父 subProcess 失败重入。终止失败仅告警不 panic。
|
||||
actx := WithInternalCallingMode(context.Background())
|
||||
reason := fmt.Sprintf("subProcess child panicked: %v", r)
|
||||
if terr := s.TerminateProcessInstance(actx, SystemActor(), childID, reason); terr != nil {
|
||||
logrus.WithError(terr).Errorf("failed to terminate panicked subProcess child %s", childID)
|
||||
}
|
||||
}
|
||||
}()
|
||||
engine.OnMsg(msg)
|
||||
|
||||
Reference in new issue
Block a user