Skip to content

Conversation

@lipoja
Copy link
Contributor

@lipoja lipoja commented Jan 15, 2026

[CLOUDDST-30851]

Summary by Sourcery

Bug Fixes:

  • Prevent leaving stale locks by only unlocking active locks and releasing them even when non-address-related exceptions occur during lock operations.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 15, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensure file locks are always released safely by only unlocking when a lock is still held and by unlocking on any unexpected exception, not just address-in-use errors.

Sequence diagram for lock handling on exceptions

sequenceDiagram
    participant Caller
    participant inner
    participant LockManager
    participant ActiveLock

    Caller->>inner: call inner(args, kwargs)
    loop retry_until_locked
        inner->>LockManager: attempt_lock()
        alt lock_acquired
            LockManager-->>inner: lock_success = True
            inner->>ActiveLock: perform_operation_with_lock()
            Note over inner,ActiveLock: (implicit work, not shown in code)
            inner->>ActiveLock: unlock_if_locked()
            ActiveLock-->>inner: lock_released
            inner-->>Caller: return result
        else AddressAlreadyInUse
            LockManager-->>inner: raise AddressAlreadyInUse
            inner->>ActiveLock: if locked then unlock()
            ActiveLock-->>inner: lock_released
            Note over inner,LockManager: lock_success = False, retry loop
        else other_Exception
            LockManager-->>inner: raise Exception
            inner->>ActiveLock: if locked then unlock()
            ActiveLock-->>inner: lock_released
            inner-->>Caller: re_raise_exception
        end
    end
Loading

Flow diagram for conditional lock unlocking on exceptions

flowchart TD
    A[start inner] --> B[attempt to acquire locks]
    B --> C{exception raised?}
    C -->|no| D[set lock_success based on acquisition]
    C -->|AddressAlreadyInUse| E[set lock_success to False]
    C -->|other Exception| F[set lock_success to False]
    E --> G[for each active_lock<br/>if active_lock.locked<br/>unlock]
    F --> H[for each active_lock<br/>if active_lock.locked<br/>unlock]
    H --> I[re_raise exception]
    D --> J{lock_success is True?}
    J -->|yes| K[for each active_lock<br/>if active_lock.locked<br/>unlock]
    J -->|no| L[retry loop or exit]
    K --> M[return result]
    L --> N[end inner]
    I --> N
    M --> N
Loading

File-Level Changes

Change Details Files
Guard all unlock operations with a locked check to avoid unlocking already-released locks.
  • Wrap unlock calls in a conditional that checks the lock’s locked attribute before calling unlock in the AddressAlreadyInUse handler.
  • Apply the same locked check in the finally block when releasing locks after successful acquisition.
iib/workers/tasks/opm_operations.py
Release all active locks when any unexpected exception is raised during lock acquisition and re-raise the exception.
  • Add a generic Exception handler that marks the lock attempt as failed, unlocks all currently active locks that are still locked, and then re-raises the exception to preserve error signaling.
iib/workers/tasks/opm_operations.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The unlocking logic for currently_active_locks is now duplicated in the AddressAlreadyInUse handler, the generic Exception handler, and the finally block; consider extracting this into a small helper or shared block to avoid divergence in future changes.
  • Catching a broad Exception and immediately re-raising is reasonable for cleanup, but you might want to narrow this to a more specific base class (e.g. OSError-related) if only operational errors are expected here, to avoid unexpected interactions with unrelated exceptions.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The unlocking logic for `currently_active_locks` is now duplicated in the `AddressAlreadyInUse` handler, the generic `Exception` handler, and the `finally` block; consider extracting this into a small helper or shared block to avoid divergence in future changes.
- Catching a broad `Exception` and immediately re-raising is reasonable for cleanup, but you might want to narrow this to a more specific base class (e.g. `OSError`-related) if only operational errors are expected here, to avoid unexpected interactions with unrelated exceptions.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@lipoja lipoja force-pushed the fix_locking branch 2 times, most recently from 4657c43 to 43e1d9a Compare January 16, 2026 11:04
Copy link
Contributor

@JAVGan JAVGan left a comment

Choose a reason for hiding this comment

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

LGTM

@lipoja lipoja merged commit 827984d into release-engineering:master Jan 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants