From 7cd313951aa4f12d652305be708ed6a4a8d98d24 Mon Sep 17 00:00:00 2001 From: Gusted Date: Thu, 10 Jul 2025 14:07:18 +0200 Subject: [PATCH] fix: use parent context for new transactions (#8464) - Older code (154 places to be exact) that want to do transactions uses `TxContext`. If the context is not already in a transaction then it uses `DefaultContext` for the new transaction. - Not reusing the context it was given leads to two problems: for tracing (forgejo/forgejo#6470) any SQL code that is executed inside this transaction is not shown in the trace. If the context is cancelled then the transaction is not cancelled and will always complete (given there's no SQL error). - When this function was introduced it didn't take a parent context, hence the usage of `DefaultContext`. When `parentCtx` was introduced it was only used to avoid nested transactions and use the parent's transaction. I do not see any reasons why the parent context cannot be reused, it is reused in the newer transaction function `WithTx` without any known problems. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8464 Reviewed-by: Otto Reviewed-by: Earl Warren Co-authored-by: Gusted Co-committed-by: Gusted --- models/db/context.go | 2 +- models/db/context_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/models/db/context.go b/models/db/context.go index 35526936af..3e035cd733 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -141,7 +141,7 @@ func TxContext(parentCtx context.Context) (*Context, Committer, error) { return nil, nil, err } - return newContext(DefaultContext, sess, true), sess, nil + return newContext(parentCtx, sess, true), sess, nil } // WithTx represents executing database operations on a transaction, if the transaction exist, diff --git a/models/db/context_test.go b/models/db/context_test.go index 7ab327b7e9..d12d79ebe1 100644 --- a/models/db/context_test.go +++ b/models/db/context_test.go @@ -84,4 +84,16 @@ func TestTxContext(t *testing.T) { return nil })) } + + t.Run("Reuses parent context", func(t *testing.T) { + type unique struct{} + + ctx := context.WithValue(db.DefaultContext, unique{}, "yes!") + assert.False(t, db.InTransaction(ctx)) + + require.NoError(t, db.WithTx(ctx, func(ctx context.Context) error { + assert.Equal(t, "yes!", ctx.Value(unique{})) + return nil + })) + }) }