From c5c617b1782afccaf0af8bd29933e2a5c8139b04 Mon Sep 17 00:00:00 2001 From: Elijah DeLee Date: Tue, 20 Aug 2024 16:24:56 -0400 Subject: [PATCH] Guard around race condition (#15452) I had the luck of running into this race condition that broke my deployment. No instance was ever able to register because on running "awx-manage" in some check of a setting, it would end up failing here with ``` File "/var/lib/awx/venv/awx/lib64/python3.11/site-packages/awx/conf/license.py", line 10, in _get_validated_license_data return get_licenser().validate() ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/lib/awx/venv/awx/lib64/python3.11/site-packages/awx/main/utils/licensing.py", line 453, in validate automated_since = int(Instance.objects.order_by('id').first().created.timestamp()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'created' ``` --- awx/main/utils/licensing.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/awx/main/utils/licensing.py b/awx/main/utils/licensing.py index 4890b1015c85..7453f56c81d1 100644 --- a/awx/main/utils/licensing.py +++ b/awx/main/utils/licensing.py @@ -450,7 +450,12 @@ def validate(self): if first_host: automated_since = int(first_host.first_automation.timestamp()) else: - automated_since = int(Instance.objects.order_by('id').first().created.timestamp()) + try: + automated_since = int(Instance.objects.order_by('id').first().created.timestamp()) + except AttributeError: + # In the odd scenario that create_preload_data was not run, there are no hosts + # Then we CAN end up here before any instance has registered + automated_since = int(time.time()) instance_count = int(attrs.get('instance_count', 0)) attrs['current_instances'] = current_instances attrs['automated_instances'] = automated_instances