@@ -49,9 +49,14 @@ def endpoint_name():
4949
5050
5151@pytest .fixture (scope = "session" , autouse = True )
52- def cleanup_e2e_endpoints ():
53- """Cleanup e2e endpoints before and after tests."""
52+ def cleanup_e2e_endpoints (tmp_path_factory ):
53+ """Cleanup e2e endpoints before tests.
54+
55+ Fixture session scope is per-worker in pytest-xdist, so filelock ensures only one
56+ worker actually performs the cleanup while the others wait and skip.
57+ """
5458 import os
59+ import filelock
5560 from botocore .exceptions import ClientError
5661
5762 # This file's tests use us-west-2 resources. Set SAGEMAKER_REGION so the
@@ -62,30 +67,26 @@ def cleanup_e2e_endpoints():
6267
6368 from sagemaker .core .resources import Endpoint
6469
65- # Cleanup before tests
66- try :
67- for endpoint in Endpoint .get_all ():
70+ # Use filelock so only one worker performs the cleanup in parallel runs
71+ root_tmp = tmp_path_factory .getbasetemp ().parent
72+ lock_file = root_tmp / "e2e_endpoint_cleanup.lock"
73+ done_file = root_tmp / "e2e_endpoint_cleanup.done"
74+
75+ with filelock .FileLock (str (lock_file )):
76+ if not done_file .exists ():
6877 try :
69- if endpoint .endpoint_name .startswith ('e2e-' ):
70- endpoint .delete ()
78+ for endpoint in Endpoint .get_all ():
79+ try :
80+ if endpoint .endpoint_name .startswith ('e2e-' ):
81+ endpoint .delete ()
82+ except (ClientError , Exception ):
83+ pass
7184 except (ClientError , Exception ):
7285 pass
73- except (ClientError , Exception ):
74- pass
86+ done_file .write_text ("done" )
7587
7688 yield
7789
78- # Cleanup after tests
79- try :
80- for endpoint in Endpoint .get_all ():
81- try :
82- if endpoint .endpoint_name .startswith ('e2e-' ):
83- endpoint .delete ()
84- except (ClientError , Exception ):
85- pass
86- except (ClientError , Exception ):
87- pass
88-
8990 # Restore original SAGEMAKER_REGION
9091 if original_sm_region :
9192 os .environ ["SAGEMAKER_REGION" ] = original_sm_region
0 commit comments