Skip to content

Lock should not be release on ignored exception#3165

Merged
dbernstein merged 1 commit intomainfrom
fix-bug-in-redis-locking
Mar 23, 2026
Merged

Lock should not be release on ignored exception#3165
dbernstein merged 1 commit intomainfrom
fix-bug-in-redis-locking

Conversation

@dbernstein
Copy link
Copy Markdown
Contributor

@dbernstein dbernstein commented Mar 20, 2026

@jonathangreen : I ran into this one while fixing the issues you identified in my overdrive task fix PR.

Description

  • lock.py: Fixes the lock() context manager so that when an exception in ignored_exceptions is raised, the lock is not released. Previously, ignored exceptions were treated as clean exits (exception_occurred=False), so release_on_exit and not exception_occurred evaluated to True and the lock was released. The fix adds ignored_exception_occurred tracking and skips the release when an ignored exception occurs.
  • test_lock.py: Adds test_lock_not_released_on_ignored_exception, which asserts that the lock remains held when an exception in ignored_exceptions is raised. This test fails before the fix and passes after it.

Motivation and Context

The ignored_exceptions parameter is intended to keep the lock held when certain exceptions occur (e.g. Celery's Ignore from task.replace() or retry-related exceptions). With the old logic, the lock was still released for those exceptions, which could cause race conditions when retrying or chaining tasks.

How Has This Been Tested?

  • Added test_lock_not_released_on_ignored_exception, which acquires a lock with ignored_exceptions=(ValueError,), raises ValueError inside the context manager, and asserts the lock is still held.
  • Confirmed the test fails with the old implementation and passes with the fix.
  • Ran the full lock test suite to ensure no regressions.

Checklist

  • I have updated the documentation accordingly.
  • All new and existing tests passed.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.28%. Comparing base (a5d2c7c) to head (290e8fc).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3165   +/-   ##
=======================================
  Coverage   93.28%   93.28%           
=======================================
  Files         493      493           
  Lines       45711    45713    +2     
  Branches     6264     6264           
=======================================
+ Hits        42641    42645    +4     
+ Misses       1983     1982    -1     
+ Partials     1087     1086    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbernstein dbernstein marked this pull request as ready for review March 21, 2026 06:03
@jonathangreen jonathangreen added the bug Something isn't working label Mar 23, 2026
@jonathangreen jonathangreen changed the title Fix bug: lock should not be release on ignored exception. Lock should not be release on ignored exception Mar 23, 2026
@jonathangreen jonathangreen self-requested a review March 23, 2026 13:26
Copy link
Copy Markdown
Member

@jonathangreen jonathangreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Good catch.

Comment thread tests/manager/service/redis/models/test_lock.py
@dbernstein dbernstein force-pushed the fix-bug-in-redis-locking branch from 6ac1b7f to 290e8fc Compare March 23, 2026 15:08
@dbernstein dbernstein enabled auto-merge (squash) March 23, 2026 15:17
@dbernstein dbernstein merged commit c046b9e into main Mar 23, 2026
20 checks passed
@dbernstein dbernstein deleted the fix-bug-in-redis-locking branch March 23, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants