fix(problem): resolve_tags 的 create 包一层 atomic
外层若在事务里,IntegrityError 会中止整个事务,导致回查抛 TransactionManagementError。当前未开 ATOMIC_REQUESTS 所以还没触发, 但仓库多处用了 transaction.atomic(),属于随时会踩的坑。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from django.core.cache import cache
|
||||
from django.db import IntegrityError
|
||||
from django.db import IntegrityError, transaction
|
||||
|
||||
from utils.constants import CacheKey
|
||||
|
||||
@@ -39,7 +39,10 @@ def resolve_tags(names):
|
||||
tag = ProblemTag.objects.filter(name__iexact=name).first()
|
||||
if tag is None:
|
||||
try:
|
||||
tag = ProblemTag.objects.create(name=name)
|
||||
# 包一层 atomic:外层若在事务里,IntegrityError 会中止整个事务,
|
||||
# 导致下面的回查抛 TransactionManagementError
|
||||
with transaction.atomic():
|
||||
tag = ProblemTag.objects.create(name=name)
|
||||
created = True
|
||||
except IntegrityError:
|
||||
# 并发下另一个请求刚建好同名标签,回查复用
|
||||
|
||||
Reference in New Issue
Block a user