Fix Race Condition in Parallel AWS Lambda Server Mode Tests (#5643)

This commit is contained in:
Brian Pandola 2022-11-07 12:03:07 -08:00 committed by GitHub
parent d560ff002d
commit 3c26da6f64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 8 deletions

View File

@ -1134,3 +1134,39 @@ def test_multiple_qualifiers():
fn["FunctionArn"].should.equal( fn["FunctionArn"].should.equal(
f"arn:aws:lambda:us-east-1:{ACCOUNT_ID}:function:{fn_name}:6" f"arn:aws:lambda:us-east-1:{ACCOUNT_ID}:function:{fn_name}:6"
) )
def test_get_role_name_utility_race_condition():
# Play with these variables as needed to reproduce the error.
max_workers, num_threads = 3, 15
errors = []
roles = []
def thread_function(_):
while True:
# noinspection PyBroadException
try:
role = get_role_name()
except ClientError as e:
errors.append(str(e))
break
except Exception:
# boto3 and our own IAMBackend are not thread-safe,
# and occasionally throw weird errors, so we just
# pass and retry.
# https://github.com/boto/boto3/issues/1592
pass
else:
roles.append(role)
break
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
executor.map(thread_function, range(num_threads))
# Check all threads are accounted for, all roles are the same entity,
# and there are no client errors.
assert len(errors) + len(roles) == num_threads
assert roles.count(roles[0]) == len(roles)
assert len(errors) == 0

View File

@ -166,14 +166,18 @@ def create_invalid_lambda(role):
def get_role_name(): def get_role_name():
with mock_iam(): with mock_iam():
iam = boto3.client("iam", region_name=_lambda_region) iam = boto3.client("iam", region_name=_lambda_region)
try: while True:
return iam.get_role(RoleName="my-role")["Role"]["Arn"] try:
except ClientError: return iam.get_role(RoleName="my-role")["Role"]["Arn"]
return iam.create_role( except ClientError:
RoleName="my-role", try:
AssumeRolePolicyDocument="some policy", return iam.create_role(
Path="/my-path/", RoleName="my-role",
)["Role"]["Arn"] AssumeRolePolicyDocument="some policy",
Path="/my-path/",
)["Role"]["Arn"]
except ClientError:
pass
def wait_for_log_msg(expected_msg, log_group, wait_time=30): def wait_for_log_msg(expected_msg, log_group, wait_time=30):