@@ -281,26 +281,28 @@ def _directory_stack_resource_generator(
281281@pytest .fixture (scope = "class" )
282282def directory_factory (directory_shared_namespace , vpc_stacks_shared , cfn_stacks_factory , request ):
283283 """
284- Class-scoped factory that lazily creates per-(region, directory_type) SharedFixtures on first use.
285- For each key we 'acquire()' exactly once per worker, cache the stack name, and 'release()' at teardown.
284+ Class-scoped factory: build a per-(region, directory_type) SharedFixture and acquire it on call.
285+ We purposely track only the last acquire (per worker) and release it once in teardown,
286+ based on the current assumption that each worker invokes this factory only once.
286287 """
287288 base_dir = Path (directory_shared_namespace ) # shared path provided by the session fixture
288- local_cache = {} # key -> (shared_fixture_obj, stack_name)
289+ last_shared_fixture = None
289290
290291 def _factory (existing_directory_stack_name : str , directory_type : str , region : str ) -> str :
291- # Use-only path: explicit stack name, no sharing/cleanup
292+ xdist_worker_id = get_xdist_worker_id (request )
293+ nodeid = getattr (request .node , "nodeid" , "N/A" )
294+ logging .info (
295+ "directory_factory invoked: region=%s, directory_type=%s, existing_provided=%s, worker=%s, nodeid=%s" ,
296+ region , directory_type , bool (existing_directory_stack_name ), xdist_worker_id , nodeid
297+ )
298+
299+ # Use-only path: explicit stack name, no sharing/cleanup.
292300 if existing_directory_stack_name :
293301 return existing_directory_stack_name
294302
295303 if not is_directory_supported (region , directory_type ):
296304 raise RuntimeError (f"Directory type { directory_type } not supported in { region } " )
297305
298- key = f"{ region } :{ directory_type } "
299- if key in local_cache :
300- return local_cache [key ][1 ]
301-
302- # Build a per-key SharedFixture with a stable name so all workers rendezvous on the same files.
303- xdist_worker_id = get_xdist_worker_id (request )
304306 pid = os .getpid ()
305307 xdist_worker_id_and_pid = f"{ xdist_worker_id } : { pid } "
306308
@@ -309,7 +311,7 @@ def _factory(existing_directory_stack_name: str, directory_type: str, region: st
309311 shared_fixture = SharedFixture (
310312 name = f"directory_stack_{ region } _{ directory_type } " ,
311313 shared_save_location = base_dir ,
312- fixture_func = _directory_stack_resource_generator ,
314+ fixture_func = _directory_stack_resource_generator , # generator does find-or-create + cleanup
313315 fixture_func_args = (
314316 existing_directory_stack_name ,
315317 directory_type ,
@@ -324,17 +326,19 @@ def _factory(existing_directory_stack_name: str, directory_type: str, region: st
324326 )
325327
326328 data = shared_fixture .acquire ()
329+ nonlocal last_shared_fixture
330+ last_shared_fixture = shared_fixture
327331 payload = data .fixture_return_value or {}
328332 stack_name = payload .get ("name" )
329- local_cache [key ] = (shared_fixture , stack_name )
330333 return stack_name
331334
332335 yield _factory
333336
334- # release once per key so the last releaser triggers generator cleanup when managed=True
335- for shared_fixture , _ in local_cache .values ():
337+ # Release once (per worker). If future changes call this factory multiple times per worker,
338+ # switch to tracking all acquisitions and releasing each.
339+ if last_shared_fixture :
336340 try :
337- shared_fixture .release ()
341+ last_shared_fixture .release ()
338342 except Exception as e :
339343 logging .warning ("Error releasing shared fixture: %s" , e )
340344
0 commit comments