From 7ca2397492982c30fd41a2f4f6f336cec71531f2 Mon Sep 17 00:00:00 2001 From: aschumann-virtualcable Date: Thu, 15 Oct 2026 11:37:13 +0200 Subject: [PATCH 1/2] fix(userservice): assign the L1 cache found by the second lookup get_assignation_for_user looks for a USABLE L1 cache whose OS is ready and, if there is none, for a USABLE L1 cache regardless of the OS state. The second lookup reserves the cache for the user in the database, but the "early assign" block hung off the `else` of `if not cache`, so a cache found by the second lookup was never assigned nor returned. The flow then went on as a cache miss and gave the user another service, leaving two assigned services in the same pool. Run the early assign whenever a cache was found, as it was before 70d3e386a. Adds a test with a USABLE L1 cache whose OS is still preparing. --- server/src/uds/core/managers/userservice.py | 2 +- .../tests/core/managers/test_userservice.py | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/server/src/uds/core/managers/userservice.py b/server/src/uds/core/managers/userservice.py index efa516f90..77bacb921 100644 --- a/server/src/uds/core/managers/userservice.py +++ b/server/src/uds/core/managers/userservice.py @@ -633,7 +633,7 @@ def get_assignation_for_user( cache = None else: cache = None - else: + if cache: # Early assign cache.assign_to(user) diff --git a/server/tests/core/managers/test_userservice.py b/server/tests/core/managers/test_userservice.py index 570c49dd6..3613a8ab9 100644 --- a/server/tests/core/managers/test_userservice.py +++ b/server/tests/core/managers/test_userservice.py @@ -130,3 +130,30 @@ def test_get_user_service_with_initial_no_cache(self) -> None: self.assertEqual(assigned_info.userservice.src_ip, '1.2.3.4') self.assertEqual(assigned_info.userservice.src_hostname, '1.2.3.4') self.assertEqual(assigned_info.transport.uuid, service_pool.transports.all()[0].uuid) + + def test_get_user_service_with_usable_cache_and_os_not_ready(self) -> None: + userservice = services_fixtures.create_db_assigned_userservices()[0] + user = userservice.user + assert user is not None + + userservice.cache_level = core_types.services.CacheLevel.L1 + userservice.user = None + userservice.state = core_types.states.State.USABLE + userservice.os_state = core_types.states.State.PREPARING + userservice.save() + + service_pool = userservice.service_pool + + service_pool.initial_srvs = 1 + service_pool.cache_l1_srvs = 0 + service_pool.cache_l2_srvs = 0 + service_pool.max_srvs = 3 + service_pool.save() + + assigned = self.manager.get_assignation_for_user(service_pool, user) + + self.assertIsNotNone(assigned) + assert assigned is not None + self.assertEqual(assigned.uuid, userservice.uuid) + self.assertEqual(assigned.cache_level, core_types.services.CacheLevel.NONE) + self.assertEqual(service_pool.assigned_user_services().filter(user=user).count(), 1) From 959f6e6fd0c1ccb60e5e5ef14cf338b1eb41bb0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janier=20Rodr=C3=ADguez?= Date: Thu, 15 Oct 2026 12:28:36 +0200 Subject: [PATCH 2/2] test(userservice): a second L1 cache still preparing stays free on a cache hit Covers the other way out of the double assignment: with a USABLE L1 cache whose OS is preparing and a second L1 cache still PREPARING, the user gets the first one, ends up with exactly one user service, and the second cache keeps no user and its L1 level. Without the fix the cache missed path takes the second cache. --- .../tests/core/managers/test_userservice.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/server/tests/core/managers/test_userservice.py b/server/tests/core/managers/test_userservice.py index 3613a8ab9..3660ddbb6 100644 --- a/server/tests/core/managers/test_userservice.py +++ b/server/tests/core/managers/test_userservice.py @@ -157,3 +157,52 @@ def test_get_user_service_with_usable_cache_and_os_not_ready(self) -> None: self.assertEqual(assigned.uuid, userservice.uuid) self.assertEqual(assigned.cache_level, core_types.services.CacheLevel.NONE) self.assertEqual(service_pool.assigned_user_services().filter(user=user).count(), 1) + + def test_get_assignation_assigns_a_single_userservice_when_cache_os_is_not_ready(self) -> None: + """A cache hit on a service whose os is still preparing is resolved by the second + lookup, which already reserves the row on database. Falling through to the + "cache missed" lookups from there reserved a second user service for the same + user, leaving the first one assigned and unreachable. + """ + from tests.fixtures import authenticators as auth_fixtures + + provider = services_fixtures.create_db_provider() + service = services_fixtures.create_db_service(provider) + osmanager = services_fixtures.create_db_osmanager() + authenticator = auth_fixtures.create_db_authenticator() + groups = auth_fixtures.create_db_groups(authenticator, 1) + user = auth_fixtures.create_db_users(authenticator, 1, groups=groups)[0] + transport = services_fixtures.create_db_transport() + service_pool = services_fixtures.create_db_servicepool(service, osmanager, groups, [transport]) + + service_pool.initial_srvs = 0 + service_pool.cache_l1_srvs = 2 + service_pool.cache_l2_srvs = 0 + service_pool.max_srvs = 4 + service_pool.save() + + publication = services_fixtures.create_db_publication(service_pool) + + usable_cache = services_fixtures.create_db_userservice(service_pool, publication, None) + usable_cache.cache_level = core_types.services.CacheLevel.L1 + usable_cache.os_state = core_types.states.State.PREPARING + usable_cache.save() + + preparing_cache = services_fixtures.create_db_userservice(service_pool, publication, None) + preparing_cache.cache_level = core_types.services.CacheLevel.L1 + preparing_cache.state = core_types.states.State.PREPARING + preparing_cache.os_state = core_types.states.State.PREPARING + preparing_cache.save() + + assigned = self.manager.get_assignation_for_user(service_pool, user) + + self.assertIsNotNone(assigned) + assert assigned is not None + self.assertEqual(assigned.uuid, usable_cache.uuid) + self.assertEqual(assigned.cache_level, core_types.services.CacheLevel.NONE) + self.assertEqual(assigned.user, user) + self.assertEqual(models.UserService.objects.filter(user=user).count(), 1) + + preparing_cache.refresh_from_db() + self.assertIsNone(preparing_cache.user) + self.assertEqual(preparing_cache.cache_level, core_types.services.CacheLevel.L1)