Skip to content

[Backport 2025.4] fix(nemesis): prevent UnboundLocalError when exception raised before DisruptionEvent context#13692

Draft
scylladbbot wants to merge 1 commit intoscylladb:branch-2025.4from
scylladbbot:backport/13628/to-2025.4
Draft

[Backport 2025.4] fix(nemesis): prevent UnboundLocalError when exception raised before DisruptionEvent context#13692
scylladbbot wants to merge 1 commit intoscylladb:branch-2025.4from
scylladbbot:backport/13628/to-2025.4

Conversation

@scylladbbot
Copy link

Description

nemesis_event was accessed in finally block but only defined within with DisruptionEvent() context manager. When KillNemesis raised during check_cluster_health() before entering the with block, variable was undefined → UnboundLocalError.

Before:

def wrapper(*args, **kwargs):
    runner = caller_obj.runner
    try:
        # ... 
        runner.cluster.check_cluster_health()  # KillNemesis raised here
        
        with DisruptionEvent(...) as nemesis_event:  # Never reached
            # ...
    finally:
        runner.last_nemesis_event = nemesis_event  # UnboundLocalError

After:

def wrapper(*args, **kwargs):
    runner = caller_obj.runner
    nemesis_event = None  # Initialize early
    try:
        # ...
        runner.cluster.check_cluster_health()  # KillNemesis raised here
        
        with DisruptionEvent(...) as nemesis_event:  # Never reached
            # ...
    finally:
        if nemesis_event is not None:  # Safe access
            runner.last_nemesis_event = nemesis_event

Changes:

  • sdcm/nemesis.py:6273: Initialize nemesis_event = None
  • sdcm/nemesis.py:6431-6432: Guard assignment with None check

Testing

  • Manual validation confirms fix prevents the UnboundLocalError
  • Python syntax validation passes
  • Note: Unit test for the wrapper function is not included as the function has complex dependencies and structure that make it difficult to test without significant refactoring

PR pre-checks (self review)

  • I added the relevant backport labels
  • I didn't leave commented-out/debugging code

Reminders

  • Add New configuration option and document them (in sdcm/sct_config.py)
  • Add unit tests to cover my changes (under unit-test/ folder)
  • Update the Readme/doc folder relevant to this change (if needed)

Fixes: SCT-92


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

  • (cherry picked from commit a3c67c0)

Parent PR: #13628

Fixed UnboundLocalError that occurred when KillNemesis exception was raised
before entering the DisruptionEvent context manager. The error happened
because nemesis_event variable was only defined inside the 'with' block,
but was accessed unconditionally in the finally block.

Changes:
- Initialize nemesis_event to None at start of wrapper function
- Add None check in finally block before accessing nemesis_event

Co-authored-by: fruch <340979+fruch@users.noreply.github.com>
(cherry picked from commit a3c67c0)
@scylladbbot
Copy link
Author

@copilot - This PR has conflicts, therefore it was moved to draft
Please resolve them and mark this PR as ready for review

@github-actions github-actions bot added P2 High Priority Bug Something isn't working right promoted-to-master labels Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working right conflicts P2 High Priority promoted-to-master

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments