Repository: getmoto/moto

Issue:
test_describe_vpc_endpoint_services_bad_args is racy
Running the moto 4.2.13 (will upgrade to 5.x shortly) testsuite on big and busy machines like we do in nixpkgs uncovers racy tests regularly.

The following test fails to switch its error code in time for the assertion to hit.

```pytb
_________________ test_describe_vpc_endpoint_services_bad_args _________________
[gw0] linux -- Python 3.11.8 /nix/store/3v2ch16fkl50i85n05h5ckss8pxx6836-python3-3.11.8/bin/python3.11

    @mock_ec2
    def test_describe_vpc_endpoint_services_bad_args() -> None:
        """Verify exceptions are raised for bad arguments."""
        ec2 = boto3.client("ec2", region_name="us-west-1")
    
        # Bad service name -- a default service would typically be of the format:
        # 'com.amazonaws.<region>.<service_name>'.
        with pytest.raises(ClientError) as exc:
            ec2.describe_vpc_endpoint_services(ServiceNames=["s3"])
        err = exc.value.response["Error"]
        assert err["Code"] == "InvalidServiceName"
        assert "The Vpc Endpoint Service 's3' does not exist" in err["Message"]
    
        # Bad filter specification -- the filter name should be "service-type"
        # not "ServiceType".
        with pytest.raises(ClientError) as exc:
            ec2.describe_vpc_endpoint_services(
                ServiceNames=["com.amazonaws.us-west-1.s3"],
                Filters=[{"Name": "ServiceType", "Values": ["Gateway"]}],
            )
        err = exc.value.response["Error"]
>       assert err["Code"] == "InvalidFilter"
E       AssertionError: assert 'InvalidServiceName' == 'InvalidFilter'
E         - InvalidFilter
E         + InvalidServiceName
```

Our test environment is this:

```
platform linux -- Python 3.11.8, pytest-7.4.4, pluggy-1.4.0
rootdir: /build/moto-4.2.13
configfile: setup.cfg
plugins: order-1.2.0, xdist-3.5.0
```


Discussion:

Resolving patch:
diff --git a/moto/ec2/models/vpcs.py b/moto/ec2/models/vpcs.py
--- a/moto/ec2/models/vpcs.py
+++ b/moto/ec2/models/vpcs.py
@@ -1,5 +1,6 @@
 import ipaddress
 import json
+import threading
 import weakref
 from collections import defaultdict
 from operator import itemgetter
@@ -82,6 +83,7 @@
 ]
 MAX_NUMBER_OF_ENDPOINT_SERVICES_RESULTS = 1000
 DEFAULT_VPC_ENDPOINT_SERVICES: List[Dict[str, str]] = []
+ENDPOINT_SERVICE_COLLECTION_LOCK = threading.Lock()
 
 
 class VPCEndPoint(TaggedEC2Resource, CloudFormationModel):
@@ -773,35 +775,37 @@ def _collect_default_endpoint_services(
         account_id: str, region: str
     ) -> List[Dict[str, str]]:
         """Return list of default services using list of backends."""
-        if DEFAULT_VPC_ENDPOINT_SERVICES:
-            return DEFAULT_VPC_ENDPOINT_SERVICES
+        with ENDPOINT_SERVICE_COLLECTION_LOCK:
 
-        zones = [
-            zone.name
-            for zones in RegionsAndZonesBackend.zones.values()
-            for zone in zones
-            if zone.name.startswith(region)
-        ]
+            if DEFAULT_VPC_ENDPOINT_SERVICES:
+                return DEFAULT_VPC_ENDPOINT_SERVICES
 
-        from moto import backends  # pylint: disable=import-outside-toplevel
+            zones = [
+                zone.name
+                for zones in RegionsAndZonesBackend.zones.values()
+                for zone in zones
+                if zone.name.startswith(region)
+            ]
 
-        for implemented_service in IMPLEMENTED_ENDPOINT_SERVICES:
-            _backends = backends.get_backend(implemented_service)  # type: ignore[call-overload]
-            account_backend = _backends[account_id]
-            if region in account_backend:
-                service = account_backend[region].default_vpc_endpoint_service(
-                    region, zones
-                )
-                if service:
-                    DEFAULT_VPC_ENDPOINT_SERVICES.extend(service)
+            from moto import backends  # pylint: disable=import-outside-toplevel
 
-            if "global" in account_backend:
-                service = account_backend["global"].default_vpc_endpoint_service(
-                    region, zones
-                )
-                if service:
-                    DEFAULT_VPC_ENDPOINT_SERVICES.extend(service)
-        return DEFAULT_VPC_ENDPOINT_SERVICES
+            for implemented_service in IMPLEMENTED_ENDPOINT_SERVICES:
+                _backends = backends.get_backend(implemented_service)  # type: ignore[call-overload]
+                account_backend = _backends[account_id]
+                if region in account_backend:
+                    service = account_backend[region].default_vpc_endpoint_service(
+                        region, zones
+                    )
+                    if service:
+                        DEFAULT_VPC_ENDPOINT_SERVICES.extend(service)
+
+                if "global" in account_backend:
+                    service = account_backend["global"].default_vpc_endpoint_service(
+                        region, zones
+                    )
+                    if service:
+                        DEFAULT_VPC_ENDPOINT_SERVICES.extend(service)
+            return DEFAULT_VPC_ENDPOINT_SERVICES
 
     @staticmethod
     def _matches_service_by_tags(service: Dict[str, Any], filter_item: Dict[str, Any]) -> bool:  # type: ignore[misc]


Test patch: