Skip to content

Conversation

@hholoubk
Copy link
Contributor

@hholoubk hholoubk commented Jan 2, 2026

Initialize server_session to None at function start to prevent
UnboundLocalError when referenced in finally block. The variable
was previously only defined within a conditional block, causing
an error when the condition was not met.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for HTX stress tool testing within virtual machines, including automated installation and execution.
    • Extended stress testing options with new "htxcmdline_in_vms" variant for VM-specific stress execution.
  • Bug Fixes

    • Fixed variable initialization in remote log retrieval logic to prevent undefined reference errors.

✏️ Tip: You can customize this high-level summary in your review settings.

Misbah Anjum N and others added 2 commits December 23, 2025 10:55
This patch can be used to run htx mdts stress_type
using htxcmdline tool
Additionally, this patch fixes DeprecationWarning of
invalid escape sequence 'x' W605

This patch introduces the following changes:
1. Allow running of different stress types in different
vms based on stress_type_%s parameter if provided in
the cfg file, where %s is the name of the vm. This will
allow us to introduce additional types of stress in the
future. If the stress_type_%s paramter is not provided,
then the default stress_type parameter will be used.

2. define a new function load_htxstress_tool() to run
htxcmdline tool in the guest. This function will be
called whenever stress_type_vmname = htxcmdline. The user
can provide the link to the .rpm/.deb package and the
function will handle the installation, followed by the
running of the htxcmdline tool by using the htxcmdline_args
parameter, all provided by user in the cfg file.

Signed-off-by: Misbah Anjum N <misanjum@linux.vnet.ibm.com>
Initialize server_session to None at function start to prevent
UnboundLocalError when referenced in finally block. The variable
was previously only defined within a conditional block, causing
an error when the condition was not met.

AI assisted code and commit. Human reviewed.

Signed-off-by: hholoubk <hholoubk@redhat.com>
@coderabbitai
Copy link

coderabbitai bot commented Jan 2, 2026

Walkthrough

The changes introduce HTX stress tool support and improve code robustness across two files. In virttest/utils_sys.py, a server_session variable is initialized to None before the try block in get_qemu_log to prevent undefined variable errors in the finally block. In virttest/utils_test/__init__.py, a new load_htxstress_tool method is added to manage HTX tool installation and execution in VMs. The stress loading flow is extended to recognize the "htxcmdline_in_vms" stress type. Additionally, stress type normalization is implemented in Stress.__init__ and VMStress.__init__, and multiple regex patterns are converted to raw strings for consistency and correctness.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix server session unbound error' is directly related to the primary fix described in the PR objectives, addressing the UnboundLocalError in the get_qemu_log function. However, the PR also contains a significant secondary change adding htx stress tool support, which is not reflected in the title.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 363ce48 and 2bb3117.

📒 Files selected for processing (2)
  • virttest/utils_sys.py
  • virttest/utils_test/__init__.py
🧰 Additional context used
🪛 GitHub Actions: pre-commit
virttest/utils_test/__init__.py

[error] 1-1: pre-commit hook 'black' reformatted virttest/utils_test/init.py and exited with code 1. Command: 'pre-commit run --show-diff-on-failure --color=always --all-files'. Re-run to apply formatting changes.

🪛 Ruff (0.14.10)
virttest/utils_test/__init__.py

2418-2418: Avoid specifying long messages outside the exception class

(TRY003)


2442-2442: Avoid specifying long messages outside the exception class

(TRY003)

🔇 Additional comments (6)
virttest/utils_sys.py (1)

150-150: LGTM!

This correctly prevents the UnboundLocalError by ensuring server_session is always defined before the try block. The finally block's check if server_session: will safely evaluate to False when server_session is None, preventing both the error and unnecessary close attempts.

virttest/utils_test/__init__.py (5)

483-486: LGTM!

Converting these regex patterns to raw strings is the correct approach to avoid potential issues with backslash escape sequences.


876-876: LGTM!

Raw string usage for regex patterns is appropriate.

Also applies to: 896-896


1507-1510: LGTM!

Raw string conversions for regex patterns are correct and consistent.

Also applies to: 2066-2066, 2083-2083


2834-2877: Logic looks correct, but verify integration with load_htxstress_tool.

The per-VM stress type customization and the branching logic for HTX are correctly implemented. The code appropriately preserves the original stress_type in default_stress_type before the loop modifications.

However, the reliability of this code path depends on fixing the error handling issues in load_htxstress_tool mentioned above.


2682-2683: LGTM!

The per-VM stress type configuration allows flexibility in running different stress tools on different VMs. The value is properly passed to the parent class where normalization occurs.

self.stress_wait_for_timeout = int(
self.params.get("stress_wait_for_timeout", 60)
)
stress_type = stress_type.split('_')[0]
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Pipeline failure: Black formatting required.

The pre-commit hook indicates this file needs reformatting. The single quotes here may need to be double quotes for consistency with the codebase style.

-        stress_type = stress_type.split('_')[0]
+        stress_type = stress_type.split("_")[0]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
stress_type = stress_type.split('_')[0]
stress_type = stress_type.split("_")[0]
🤖 Prompt for AI Agents
In virttest/utils_test/__init__.py around line 2330, the string literal uses
single quotes which fails the project's Black/formatting/pre-commit checks;
replace single quotes with double quotes for that literal (change 'stress_type'
usage to use double-quoted string) and re-run Black or pre-commit formatting to
ensure the file is properly reformatted.

Comment on lines +2419 to +2422
LOG.info("Installing htx rpm/deb in the guest")
self.cmd(remove_old_package)
self.cmd(get_stress)
self.cmd(install_stress)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Missing error handling for critical installation steps.

The wget and package installation commands are executed without checking their return status. Since self.cmd uses ignore_status=True / ignore_all_errors=True, failures will be silently ignored. If the download or installation fails, the function will proceed to start a stress tool that wasn't installed, resulting in a confusing failure later.

🔎 Proposed fix with error handling
         LOG.info("Installing htx rpm/deb in the guest")
-        self.cmd(remove_old_package)
-        self.cmd(get_stress)
-        self.cmd(install_stress)
+        self.cmd(remove_old_package)
+        
+        status, output = self.cmd_status_output(get_stress, timeout=self.stress_shell_timeout)
+        if status != 0:
+            raise exceptions.TestError("Failed to download HTX stress tool: %s" % output)
+        
+        status, output = self.cmd_status_output(install_stress, timeout=self.stress_shell_timeout)
+        if status != 0:
+            raise exceptions.TestError("Failed to install HTX stress tool: %s" % output)

Committable suggestion skipped: line range outside the PR's diff.

@hholoubk hholoubk closed this Jan 5, 2026
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.

1 participant