From 18b542f8b1f183f97b0d0e2c5cd178fdecabd613 Mon Sep 17 00:00:00 2001 From: forgejo-backport-action Date: Thu, 10 Jul 2025 14:44:56 +0200 Subject: [PATCH] [v12.0/forgejo] fix: use parent context for new transactions (#8474) **Backport:** https://codeberg.org/forgejo/forgejo/pulls/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. Co-authored-by: Gusted Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8474 Reviewed-by: Gusted Co-authored-by: forgejo-backport-action Co-committed-by: forgejo-backport-action --- 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 + })) + }) }