You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by te...@apache.org on 2026/10/24 08:13:25 UTC
(incubator-seata-go) branch master updated: fix(tm): isolate nested global transaction context so the outer launcher commits (#1161)
This is an automated email from the ASF dual-hosted git repository.
thunguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-seata-go.git
The following commit(s) were added to refs/heads/master by this push:
new fc7613c8 fix(tm): isolate nested global transaction context so the outer launcher commits (#1161)
fc7613c8 is described below
commit fc7613c82dacc8e27aca01b4eb1a9a2cbb991033
Author: tachibana22 <19...@qq.com>
AuthorDate: Sat Oct 24 16:13:21 2026 +0800
fix(tm): isolate nested global transaction context so the outer launcher commits (#1161)
* feat: add tests for nested global transaction handling and propagation scenarios
* fix(tm): isolate nested global transaction context so the outer launcher commits
---
changes/dev.md | 1 +
changes/dev_zh.md | 1 +
pkg/tm/transaction_executor.go | 9 +-
pkg/tm/transaction_executor_test.go | 295 +++++++++++++++++++++++++++++++++---
4 files changed, 281 insertions(+), 25 deletions(-)
diff --git a/changes/dev.md b/changes/dev.md
index 97898f16..f967fe13 100755
--- a/changes/dev.md
+++ b/changes/dev.md
@@ -46,6 +46,7 @@
- [[#1143](https://github.com/apache/incubator-seata-go/issues/1143)] read the xid of `XA RECOVER` as the `[]byte` the MySQL driver actually returns, and close the result set
- dispatch `ConsistentHashLoadBalance` and `LeastActiveLoadBalance` in `loadbalance.Select()`: both strategies were already implemented and accepted as config values, but missing from the dispatch `switch`, so they silently fell back to random load balancing
- [[#1073](https://github.com/apache/incubator-seata-go/issues/1073)] fix what the two newly reachable load balancing strategies got wrong: the selection key was read from the whole `RpcMessage` instead of its body, so consistent hashing pinned every request to one TC; `rpc.Status.GetActive` / `GetTotal` read the in-flight counters without `sync/atomic`; the hash circle stayed empty when the first selection saw no session and did not wrap around past the last virtual node
+ - [[#1161](https://github.com/apache/incubator-seata-go/pull/1161)] fix context isolation in nested transaction to restore outer launcher commit
### optimize:
diff --git a/changes/dev_zh.md b/changes/dev_zh.md
index 91dd3458..4fe7c3f3 100644
--- a/changes/dev_zh.md
+++ b/changes/dev_zh.md
@@ -43,6 +43,7 @@ Seata-go 是一款开源的分布式事务解决方案,提供高性能和简
- [[#1143](https://github.com/apache/incubator-seata-go/issues/1143)] 按 MySQL 驱动实际返回的 `[]byte` 读取 `XA RECOVER` 的 xid,并关闭结果集
- 修复 `loadbalance.Select()` 在 `load-balance.type` 配置为 `ConsistentHashLoadBalance` / `LeastActiveLoadBalance` 时静默回退为随机负载均衡的问题:两个策略均已实现且配置层已接受,仅分发 `switch` 缺少对应分支
- [[#1073](https://github.com/apache/incubator-seata-go/issues/1073)] 修复上述两个策略在真正可达之后暴露出的问题:负载均衡 key 从整个 `RpcMessage` 而非其 body 中提取,导致一致性哈希把所有请求固定到同一台 TC;`rpc.Status.GetActive` / `GetTotal` 未用 `sync/atomic` 读取在途计数;首次选择时若还没有 session,哈希环会一直为空,且 key 越过最后一个虚拟节点时不会环绕到环首
+- [[#1161](https://github.com/apache/incubator-seata-go/pull/1161)] 修复嵌套事务中的上下文隔离,以恢复外层发起者的提交
### optimize:
diff --git a/pkg/tm/transaction_executor.go b/pkg/tm/transaction_executor.go
index 5a9c7808..c41716dd 100644
--- a/pkg/tm/transaction_executor.go
+++ b/pkg/tm/transaction_executor.go
@@ -56,7 +56,9 @@ func WithGlobalTx(ctx context.Context, gc *GtxConfig, business CallbackWithCtx)
}
if IsGlobalTx(ctx) {
- ClearTxConf(ctx)
+ // derive isolated context to prevent inner calls from mutating outer role,
+ // unbinding outer xid in RequiresNew/NotSupported, or overwriting name, status, and timeInfo
+ ctx = transferTx(ctx)
}
// TODO(#277): Revisit explicit dynamic degradation after defining its consistency guarantees and usage constraints.
@@ -220,11 +222,6 @@ func UseExistGtx(ctx context.Context, gc *GtxConfig) {
}
}
-// ClearTxConf When using global transactions in local mode, you need to clear tx config to use the propagation of global transactions.
-func ClearTxConf(ctx context.Context) {
- SetTx(ctx, &GlobalTransaction{Xid: GetXID(ctx)})
-}
-
// transferTx transfer the gtx into a new ctx from old ctx.
// use it to implement suspend and resume instead
func transferTx(ctx context.Context) context.Context {
diff --git a/pkg/tm/transaction_executor_test.go b/pkg/tm/transaction_executor_test.go
index ecdedd55..4494bbfc 100644
--- a/pkg/tm/transaction_executor_test.go
+++ b/pkg/tm/transaction_executor_test.go
@@ -350,25 +350,6 @@ func TestCommitOrRollback(t *testing.T) {
}
}
}
-
-func TestClearTxConf(t *testing.T) {
- ctx := tm.InitSeataContext(context.Background())
-
- tm.SetTx(ctx, &tm.GlobalTransaction{
- Xid: "123456",
- TxName: "MockTxName",
- TxStatus: message.GlobalStatusBegin,
- TxRole: tm.Launcher,
- })
-
- tm.ClearTxConf(ctx)
-
- assert.Equal(t, "123456", tm.GetXID(ctx))
- assert.Equal(t, tm.UnKnow, *tm.GetTxRole(ctx))
- assert.Equal(t, message.GlobalStatusUnKnown, *tm.GetTxStatus(ctx))
- assert.Equal(t, "", tm.GetTxName(ctx))
-}
-
func TestUseExistGtx(t *testing.T) {
ctx := tm.InitSeataContext(context.Background())
tm.SetXID(ctx, "123456")
@@ -603,3 +584,279 @@ func TestWithGlobalTxBusinessPanic(t *testing.T) {
}
globalTransactionManagerStub.reset()
}
+func TestGlobalTxPropagationMatrix(t *testing.T) {
+ type testMatrixCase struct {
+ name string
+ innerPropagation tm.Propagation
+ innerAction func(ctx context.Context) error
+ outerAction func(ctx context.Context) error
+ wantInnerExecuted bool
+ wantInnerTx bool
+ wantInnerXid string
+ wantInnerRole tm.GlobalTransactionRole
+ wantOuterErr bool
+ wantCommits []string
+ wantRollbacks []string
+ }
+
+ tests := []testMatrixCase{
+ // Required -> Required
+ {
+ name: "Required_Required_Success",
+ innerPropagation: tm.Required,
+ innerAction: func(ctx context.Context) error { return nil },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantCommits: []string{"xid-1"},
+ },
+ {
+ name: "Required_Required_InnerError",
+ innerPropagation: tm.Required,
+ innerAction: func(ctx context.Context) error { return fmt.Errorf("inner business error") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+ {
+ name: "Required_Required_InnerPanic",
+ innerPropagation: tm.Required,
+ innerAction: func(ctx context.Context) error { panic("inner runtime panic") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+
+ // Required -> RequiresNew
+ {
+ name: "Required_RequiresNew_Success",
+ innerPropagation: tm.RequiresNew,
+ innerAction: func(ctx context.Context) error { return nil },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-2",
+ wantInnerRole: tm.Launcher,
+ wantCommits: []string{"xid-2", "xid-1"},
+ },
+ {
+ name: "Required_RequiresNew_InnerError",
+ innerPropagation: tm.RequiresNew,
+ innerAction: func(ctx context.Context) error { return fmt.Errorf("requires_new error") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-2",
+ wantInnerRole: tm.Launcher,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-2", "xid-1"},
+ },
+ {
+ name: "Required_RequiresNew_InnerPanic",
+ innerPropagation: tm.RequiresNew,
+ innerAction: func(ctx context.Context) error { panic("requires_new panic") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-2",
+ wantInnerRole: tm.Launcher,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-2", "xid-1"},
+ },
+ {
+ name: "Required_RequiresNew_OuterError_InnerCommitted",
+ innerPropagation: tm.RequiresNew,
+ innerAction: func(ctx context.Context) error { return nil },
+ outerAction: func(ctx context.Context) error { return fmt.Errorf("outer business error") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-2",
+ wantInnerRole: tm.Launcher,
+ wantOuterErr: true,
+ wantCommits: []string{"xid-2"},
+ wantRollbacks: []string{"xid-1"},
+ },
+
+ // Required -> NotSupported
+ {
+ name: "Required_NotSupported_Success",
+ innerPropagation: tm.NotSupported,
+ innerAction: func(ctx context.Context) error { return nil },
+ wantInnerExecuted: true,
+ wantInnerTx: false,
+ wantInnerXid: "",
+ wantInnerRole: tm.UnKnow,
+ wantCommits: []string{"xid-1"},
+ },
+ {
+ name: "Required_NotSupported_InnerError",
+ innerPropagation: tm.NotSupported,
+ innerAction: func(ctx context.Context) error { return fmt.Errorf("not_supported error") },
+ wantInnerExecuted: true,
+ wantInnerTx: false,
+ wantInnerXid: "",
+ wantInnerRole: tm.UnKnow,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+ {
+ name: "Required_NotSupported_InnerPanic",
+ innerPropagation: tm.NotSupported,
+ innerAction: func(ctx context.Context) error { panic("not_supported panic") },
+ wantInnerExecuted: true,
+ wantInnerTx: false,
+ wantInnerXid: "",
+ wantInnerRole: tm.UnKnow,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+
+ // Required -> Supports
+ {
+ name: "Required_Supports_Success",
+ innerPropagation: tm.Supports,
+ innerAction: func(ctx context.Context) error { return nil },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantCommits: []string{"xid-1"},
+ },
+ {
+ name: "Required_Supports_InnerError",
+ innerPropagation: tm.Supports,
+ innerAction: func(ctx context.Context) error { return fmt.Errorf("supports error") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+ {
+ name: "Required_Supports_InnerPanic",
+ innerPropagation: tm.Supports,
+ innerAction: func(ctx context.Context) error { panic("supports panic") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+
+ // Required -> Mandatory
+ {
+ name: "Required_Mandatory_Success",
+ innerPropagation: tm.Mandatory,
+ innerAction: func(ctx context.Context) error { return nil },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantCommits: []string{"xid-1"},
+ },
+ {
+ name: "Required_Mandatory_InnerError",
+ innerPropagation: tm.Mandatory,
+ innerAction: func(ctx context.Context) error { return fmt.Errorf("mandatory error") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+ {
+ name: "Required_Mandatory_InnerPanic",
+ innerPropagation: tm.Mandatory,
+ innerAction: func(ctx context.Context) error { panic("mandatory panic") },
+ wantInnerExecuted: true,
+ wantInnerTx: true,
+ wantInnerXid: "xid-1",
+ wantInnerRole: tm.Participant,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+
+ // Required -> Never
+ {
+ name: "Required_Never_Reject",
+ innerPropagation: tm.Never,
+ innerAction: func(ctx context.Context) error { return nil },
+ wantInnerExecuted: false,
+ wantInnerTx: false,
+ wantOuterErr: true,
+ wantRollbacks: []string{"xid-1"},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ globalTransactionManagerStub.reset()
+ defer globalTransactionManagerStub.reset()
+
+ var committedXids []string
+ var rollbackedXids []string
+ xidCounter := 0
+
+ globalTransactionManagerStub.beginFunc = func(ctx context.Context, timeout time.Duration) error {
+ xidCounter++
+ tm.SetXID(ctx, fmt.Sprintf("xid-%d", xidCounter))
+ return nil
+ }
+ globalTransactionManagerStub.commitFunc = func(ctx context.Context, gtr *tm.GlobalTransaction) error {
+ if gtr != nil {
+ committedXids = append(committedXids, gtr.Xid)
+ }
+ return nil
+ }
+ globalTransactionManagerStub.rollbackFunc = func(ctx context.Context, gtr *tm.GlobalTransaction) error {
+ if gtr != nil {
+ rollbackedXids = append(rollbackedXids, gtr.Xid)
+ }
+ return nil
+ }
+
+ innerExecuted := false
+ err := tm.WithGlobalTx(context.Background(), &tm.GtxConfig{
+ Name: "outer", Propagation: tm.Required,
+ }, func(ctx context.Context) error {
+ innerErr := tm.WithGlobalTx(ctx, &tm.GtxConfig{
+ Name: "inner", Propagation: tt.innerPropagation,
+ }, func(innerCtx context.Context) error {
+ innerExecuted = true
+ assert.Equal(t, tt.wantInnerTx, tm.IsGlobalTx(innerCtx))
+ assert.Equal(t, tt.wantInnerXid, tm.GetXID(innerCtx))
+ assert.Equal(t, tt.wantInnerRole, *tm.GetTxRole(innerCtx))
+ return tt.innerAction(innerCtx)
+ })
+
+ // directly assert that outer context xid and launcher role remain intact
+ assert.Equal(t, "xid-1", tm.GetXID(ctx))
+ assert.Equal(t, tm.Launcher, *tm.GetTxRole(ctx))
+
+ if innerErr != nil {
+ return innerErr
+ }
+ if tt.outerAction != nil {
+ return tt.outerAction(ctx)
+ }
+ return nil
+ })
+
+ assert.Equal(t, tt.wantInnerExecuted, innerExecuted)
+ if tt.wantOuterErr {
+ assert.Error(t, err)
+ } else {
+ assert.NoError(t, err)
+ }
+ assert.Equal(t, tt.wantCommits, committedXids)
+ assert.Equal(t, tt.wantRollbacks, rollbackedXids)
+ })
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org