Skip to content

virttest: Fix UnboundLocalError in restart_guest_network()#4321

Open
BulaYoungR wants to merge 1 commit intoavocado-framework:masterfrom
BulaYoungR:Utils_net_fix
Open

virttest: Fix UnboundLocalError in restart_guest_network()#4321
BulaYoungR wants to merge 1 commit intoavocado-framework:masterfrom
BulaYoungR:Utils_net_fix

Conversation

@BulaYoungR
Copy link
Copy Markdown

@BulaYoungR BulaYoungR commented Feb 11, 2026

Fix a bug in restart_guest_network() where restart_cmd variable was used before initialization when mac_addr parameter is None.

Error occurred at line 1761:
restart_cmd += "%s %s; " % (dhcp_cmd, release_flag)

The += operator attempted to append to restart_cmd, but the variable was never initialized in the else branch (when mac_addr is None).

The variable restart_cmd is only initialized in the if mac_addr block (line 1754), so when execution reaches the else block, restart_cmd does not exist, causing:
UnboundLocalError: local variable 'restart_cmd' referenced before assignment

Fix: Change line 1761 from += to = to properly initialize the variable:
restart_cmd = "%s %s; " % (dhcp_cmd, release_flag)

Committer: Bolatbek Issakh bissakh@redhat.com

Fix a bug in restart_guest_network() where restart_cmd variable
was used before initialization when mac_addr parameter is None.

Error occurred at line 1761:
    restart_cmd += "%s %s; " % (dhcp_cmd, release_flag)

The += operator attempted to append to restart_cmd, but the variable
was never initialized in the else branch (when mac_addr is None).

The variable restart_cmd is only initialized in the if mac_addr block
(line 1754), so when execution reaches the else block, restart_cmd
does not exist, causing:
    UnboundLocalError: local variable 'restart_cmd' referenced before assignment

Fix: Change line 1761 from += to = to properly initialize the variable:
    restart_cmd = "%s %s; " % (dhcp_cmd, release_flag)

Committer: Bolatbek Issakh <bissakh@gmail.com>
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @BulaYoungR, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical runtime error in the network utility function restart_guest_network(). The fix ensures that a command string variable is properly initialized under all execution paths, preventing an UnboundLocalError and improving the robustness of guest network restart operations.

Highlights

  • Bug Fix: Addressed an UnboundLocalError in the restart_guest_network() function that occurred when the mac_addr parameter was None.
  • Initialization Logic: Corrected the initialization of the restart_cmd variable by changing an additive assignment (+=) to a direct assignment (=) in the else branch, ensuring the variable is always defined before use.
Changelog
  • virttest/utils_net.py
    • Fixed an UnboundLocalError by ensuring the 'restart_cmd' variable is correctly initialized in all code paths.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@BulaYoungR
Copy link
Copy Markdown
Author

Before fix

(.libvirt-ci-venv-ci-runtest-Ts1Wmd) [root@ampere-mtsnow-altramax-12 libvirt-ci-latest-venv]# bin/avocado run --vt-type libvirt --vt-omit-data-loss --vt-machine-type arm64-mmio virtual_network.iface_bridge.default.shared_physical_network --job-timeout 1200 --vt-connect-uri qemu:///system
No python imaging library installed. Screendump and Windows guest BSOD detection are disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
No python imaging library installed. PPM image conversion to JPEG disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
No python imaging library installed. Screendump and Windows guest BSOD detection are disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
No python imaging library installed. PPM image conversion to JPEG disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
JOB ID     : f55cff5401c26e8dfd9d3b2ea16423cf2a7fe5d2
JOB LOG    : /var/log/avocado/job-results/job-2026-02-11T09.52-f55cff5/job.log
 (1/1) type_specific.io-github-autotest-libvirt.virtual_network.iface_bridge.default.shared_physical_network: STARTED
 (1/1) type_specific.io-github-autotest-libvirt.virtual_network.iface_bridge.default.shared_physical_network: ERROR: local variable 'restart_cmd' referenced before assignment (62.23 s)
RESULTS    : PASS 0 | ERROR 1 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0 | CANCEL 0
JOB HTML   : /var/log/avocado/job-results/job-2026-02-11T09.52-f55cff5/results.html
JOB TIME   : 63.62 s

After fix

(.libvirt-ci-venv-ci-runtest-Ts1Wmd) [root@ampere-mtsnow-altramax-12 libvirt-ci-latest-venv]# bin/avocado run --vt-type libvirt --vt-omit-data-loss --vt-machine-type arm64-mmio virtual_network.iface_bridge.default.shared_physical_network --job-timeout 1200 --vt-connect-uri qemu:///system
No python imaging library installed. Screendump and Windows guest BSOD detection are disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
No python imaging library installed. PPM image conversion to JPEG disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
No python imaging library installed. Screendump and Windows guest BSOD detection are disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
No python imaging library installed. PPM image conversion to JPEG disabled. In order to enable it, please install python-imaging or the equivalent for your distro.
JOB ID     : 80157eeb5be4c600ca25161f53e4bf2b3b2173af
JOB LOG    : /var/log/avocado/job-results/job-2026-02-11T09.56-80157ee/job.log
 (1/1) type_specific.io-github-autotest-libvirt.virtual_network.iface_bridge.default.shared_physical_network: STARTED
 (1/1) type_specific.io-github-autotest-libvirt.virtual_network.iface_bridge.default.shared_physical_network: PASS (141.00 s)
RESULTS    : PASS 1 | ERROR 0 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0 | CANCEL 0
JOB HTML   : /var/log/avocado/job-results/job-2026-02-11T09.56-80157ee/results.html
JOB TIME   : 142.57 s

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an UnboundLocalError in restart_guest_network() by correctly initializing the restart_cmd variable. The error occurred when mac_addr was None, as the variable was used with += before being assigned a value. The fix, changing += to =, is correct and effectively resolves the bug. The change is approved.

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