Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misuse of _ (underscore) inside DataSource.sync() #17713

Closed
amyasnikov opened this issue Oct 9, 2024 · 0 comments · Fixed by #17729
Closed

Misuse of _ (underscore) inside DataSource.sync() #17713

amyasnikov opened this issue Oct 9, 2024 · 0 comments · Fixed by #17729
Assignees
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@amyasnikov
Copy link
Contributor

amyasnikov commented Oct 9, 2024

Deployment Type

Self-hosted

NetBox Version

v4.1.3

Python Version

3.12

Steps to Reproduce

  1. Start two parallel syncs of one particular data source.
  2. Get UnboundLocalError instead of SyncError. Also, the data source has the probability to get stuck in "syncing" state.

It happens due to a common python mistake:

  1. Here _ (underscore) is used as an alias to gettext.
  2. But here inside the same function a value is assigned to _ (underscore).

Python interprets this situation as "reference before assignment". For example, here will happen the same error:

def a(): ...

def b():
   a()
   a = 10

b()  # UnboundLocalError: cannot access local variable 'a' where it is not associated with a value

Here is the full traceback:

"Unhandled error occured: `<class 'UnboundLocalError'>: cannot access local variable '_' where it is not associated with a value`
  File \"/opt/netbox/venv/lib/python3.12/site-packages/validity/scripts/runtests/base.py\", line 40, in terminate_job_on_error
    yield
  File \"/opt/netbox/venv/lib/python3.12/site-packages/validity/scripts/runtests/split.py\", line 109, in __call__
    self.sync_datasources(params.overriding_datasource, device_filter, logger)\n  File \"/opt/netbox/venv/lib/python3.12/site-packages/validity/scripts/runtests/split.py\", line 40, in sync_datasources
    self.datasource_sync_fn(datasources, device_filter)
  File \"/opt/netbox/venv/lib/python3.12/site-packages/validity/utils/misc.py\", line 74, in datasource_sync
    any(tp.map(sync_func, datasources))
  File \"/usr/lib/python3.12/concurrent/futures/_base.py\", line 619, in result_iterator yield _result_or_cancel(fs.pop())
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File \"/usr/lib/python3.12/concurrent/futures/_base.py\", line 317, in _result_or_cancel
    return fut.result(timeout)
           ^^^^^^^^^^^^^^^^^^^
  File \"/usr/lib/python3.12/concurrent/futures/_base.py\", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File \"/usr/lib/python3.12/concurrent/futures/_base.py\", line 401, in __get_result
    raise self._exception
  File \"/usr/lib/python3.12/concurrent/futures/thread.py\", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File \"/opt/netbox/venv/lib/python3.12/site-packages/validity/utils/misc.py\", line 66, in sync_func
    datasource.sync(device_filter)
  File \"/opt/netbox/venv/lib/python3.12/site-packages/validity/models/data.py\", line 112, in sync
    return super().sync()
           ^^^^^^^^^^^^^^
  File \"/opt/netbox/netbox/core/models/data.py\", line 164, in sync
    raise SyncError(_(\"Cannot initiate sync; syncing already in progress.\"))
                    ^"

Although the traceback is originally generated inside a plugin, I do believe the error is quite clear and not plugin-related. If you disagree, please let me know and I will provide the traceback without plugins.

Expected Behavior

Sync of the data source must generate SyncError if something goes wrong, but not UnboundLocalError

Observed Behavior

UnboundLocalError was raised

@amyasnikov amyasnikov added status: needs triage This issue is awaiting triage by a maintainer type: bug A confirmed report of unexpected behavior in the application labels Oct 9, 2024
@jeremystretch jeremystretch changed the title Misuse of _ (underscore) inside DataSource.sync Misuse of _ (underscore) inside DataSource.sync() Oct 10, 2024
@jeremystretch jeremystretch added status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation severity: medium Results in substantial degraded or broken functionality for specfic workflows and removed status: needs triage This issue is awaiting triage by a maintainer labels Oct 10, 2024
@arthanson arthanson self-assigned this Oct 10, 2024
@arthanson arthanson added status: accepted This issue has been accepted for implementation and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Oct 10, 2024
jeremystretch added a commit that referenced this issue Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants