Skip to content

Fix UnboundLocalError in hub creators when import fails non-trivially - #14985

Merged
Nir-Az merged 2 commits into
realsenseai:developmentfrom
Nir-Az:fix-hub-import-error
Apr 30, 2026
Merged

Nir-Az merged 2 commits into
realsenseai:developmentfrom
Nir-Az:fix-hub-import-error

Conversation

@Nir-Az

@Nir-Az Nir-Az commented Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

In `unit-tests/py/rspy/device_hub.py`, both `_create_ykush()` and `_create_unifi()` had a latent bug: the `except .NoneFoundError` clause references the locally-imported module name. If the `from rspy import ` line raises any exception other than `ModuleNotFoundError`, that exception falls through to the `except .NoneFoundError` clause — Python evaluates the unbound name and raises `UnboundLocalError`, masking the real error.

This was hit on Python 3.14 / Windows: paramiko 3.5.1's import-time `importlib.metadata.version("paramiko")` triggers a `TypeError` due to a 3.14 stdlib regression. The TypeError propagates out of `from rspy import unifi`, hits `except unifi.NoneFoundError:`, and crashes pytest collection with `UnboundLocalError` instead of returning `None` like the function intends.

Fix

Split the import from the construction:

  • Import failure with `ModuleNotFoundError` → silent (package not installed).
  • Import failure for any other reason → log a warning (unexpected — e.g. transitive dep broken).
  • Construction `NoneFoundError` → silent (no hub connected, normal).
  • Construction failure for any other reason → log a warning.

This matches the original intent (silently return None when the hub isn't usable) while no longer masking unexpected errors and no longer hitting `UnboundLocalError` when the import fails non-trivially.

Test plan

  • Existing infra-tests still pass on the `Infra_Tests` GHA job (Ubuntu 22.04 + Python 3.10).
  • On Python 3.14 + Windows, `cd unit-tests && python -m pytest infra-tests/ -v` no longer crashes during collection (manually verified: paramiko TypeError is now caught and warned, `_create_unifi` returns None as intended).

@Nir-Az
Nir-Az requested review from AviaAv and Copilot April 28, 2026 18:05
Comment thread unit-tests/py/rspy/device_hub.py Outdated
Comment on lines +228 to +232
except EnvironmentError as e:
log.w(f"unifi hub init failed: {e}")
return None
except BaseException as e:
log.w(f"unifi hub init failed: {e}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

UniFiSwitch() can't throw EnvironmentError nor BaseException, we can remove it or simplify to except Exception as e to exit cleanly

@Nir-Az Nir-Az Apr 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch — UniFiSwitch.__init__ only raises NoneFoundError. The EnvironmentError is raised at module import time (unifi.py:49 when UNIFI_SSH_PASSWORD is unset), so it's now caught by the import block. Removed the redundant except EnvironmentError / except BaseException after construction and kept a single except Exception as e that warns. Fixed in a5d5af9.

return acroname.Acroname()
except ModuleNotFoundError:
return None
except acroname.NoneFoundError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we have a similar bug here, if the import fails and is not caught by ModuleNotFoundError this will throw

@Nir-Az Nir-Az Apr 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right — _create_acroname had the same latent bug. Applied the same import/construction split: import failure other than ModuleNotFoundError now gets logged and returns None instead of falling through to except acroname.NoneFoundError and crashing on the unbound name. Fixed in a5d5af9.

Comment thread unit-tests/py/rspy/device_hub.py Outdated
return None
except BaseException:
return None # no hub connected
except BaseException as e:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's validate we need BaseException and not Exception here - relevant for all uses

@Nir-Az Nir-Az Apr 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed — Exception is the right base here. BaseException would also swallow KeyboardInterrupt / SystemExit / GeneratorExit, which we don't want. Switched all BaseException to Exception across the three creators. Fixed in a5d5af9.

…eption to Exception, drop redundant EnvironmentError in _create_unifi

@AviaAv AviaAv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@Nir-Az
Nir-Az merged commit 72b002c into realsenseai:development Apr 30, 2026
30 checks passed
@Nir-Az
Nir-Az deleted the fix-hub-import-error branch April 30, 2026 10:21
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.

2 participants