From 9249e125a72bd05d80d9ea2f54db8914cd0cf1a0 Mon Sep 17 00:00:00 2001 From: Benjamin Schanzel Date: Tue, 12 Sep 2023 12:57:45 +0200 Subject: [PATCH] Fix UnboundLocalError in error handling of runStateMachine This fixes an UnboundLocalError that can arise while hanlding other errors. ``` [...] Traceback (most recent call last): File "/opt/nodepool/lib/python3.11/site-packages/nodepool/driver/statemachine.py", line 741, in _runStateMachines sm.runStateMachine() File "/opt/nodepool/lib/python3.11/site-packages/nodepool/driver/statemachine.py", line 326, in runStateMachine if state_machine and state_machine.external_id: ^^^^^^^^^^^^^ UnboundLocalError: cannot access local variable 'state_machine' where it is not associated with a value ``` Change-Id: I06ab0851cb2803f67d1d35fc12fabea9ea931501 --- nodepool/driver/statemachine.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nodepool/driver/statemachine.py b/nodepool/driver/statemachine.py index b5d02869d..f6ecacc33 100644 --- a/nodepool/driver/statemachine.py +++ b/nodepool/driver/statemachine.py @@ -309,25 +309,25 @@ class StateMachineNodeLauncher(stats.StatsReporter): "Lost ZooKeeper session trying to launch for node %s", node.id) node.state = zk.FAILED - if state_machine: - node.external_id = state_machine.external_id + if self.state_machine: + node.external_id = self.state_machine.external_id statsd_key = 'error.zksession' except exceptions.QuotaException: self.log.info("Aborting node %s due to quota failure", node.id) node.state = zk.ABORTED - if state_machine: - node.external_id = state_machine.external_id + if self.state_machine: + node.external_id = self.state_machine.external_id self.zk.storeNode(node) statsd_key = 'error.quota' self.manager.invalidateQuotaCache() except Exception as e: self.log.exception("Launch attempt %d/%d for node %s, failed:", self.attempts, self.retries, node.id) - if state_machine and state_machine.external_id: + if self.state_machine and self.state_machine.external_id: # If we're deleting, don't overwrite the node external # id, because we may make another delete state machine # below. - node.external_id = state_machine.external_id + node.external_id = self.state_machine.external_id self.zk.storeNode(node) if hasattr(e, 'statsd_key'):