From 6bcab282ffc16f4340c015f195a919bc31891704 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sun, 10 Dec 2023 08:22:26 -0100 Subject: [PATCH] Batch: Only join running Threads (#7105) --- moto/batch/models.py | 3 ++- tests/test_batch/test_batch_jobs.py | 38 +++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/moto/batch/models.py b/moto/batch/models.py index 0f6879ae9..105ff3ae9 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -1055,7 +1055,8 @@ class BatchBackend(BaseBackend): if job.status not in (JobStatus.FAILED, JobStatus.SUCCEEDED): job.stop = True # Try to join - job.join(0.2) + if job.is_alive(): + job.join(0.2) super().reset() diff --git a/tests/test_batch/test_batch_jobs.py b/tests/test_batch/test_batch_jobs.py index 428f94961..1174e59a3 100644 --- a/tests/test_batch/test_batch_jobs.py +++ b/tests/test_batch/test_batch_jobs.py @@ -1,15 +1,16 @@ import datetime import time +from unittest import SkipTest from uuid import uuid4 import botocore.exceptions import pytest -from moto import mock_batch, mock_ec2, mock_ecs, mock_iam, mock_logs +from moto import mock_batch, mock_ec2, mock_ecs, mock_iam, mock_logs, settings from tests import DEFAULT_ACCOUNT_ID from ..markers import requires_docker -from . import _get_clients, _setup +from . import DEFAULT_REGION, _get_clients, _setup @mock_logs @@ -137,6 +138,39 @@ def test_submit_job_array_size(): assert len(child_job_1["attempts"]) == 1 +@mock_ec2 +@mock_ecs +@mock_iam +@mock_batch +@pytest.mark.network +@requires_docker +def test_submit_job_array_size__reset_while_job_is_running(): + if settings.TEST_SERVER_MODE: + raise SkipTest("No point testing this in ServerMode") + + # Setup + job_definition_name = f"echo_{str(uuid4())[0:6]}" + ec2_client, iam_client, _, _, batch_client = _get_clients() + commands = ["echo", "hello"] + _, _, _, iam_arn = _setup(ec2_client, iam_client) + _, queue_arn = prepare_job(batch_client, commands, iam_arn, job_definition_name) + + # Execute + batch_client.submit_job( + jobName="test1", + jobQueue=queue_arn, + jobDefinition=job_definition_name, + arrayProperties={"size": 2}, + ) + + from moto.batch import batch_backends + + # This method will try to join on (wait for) any created JobThreads + # The parent of the ArrayJobs is created, but never started + # So we need to make sure that we don't join on any Threads that are never started in the first place + batch_backends[DEFAULT_ACCOUNT_ID][DEFAULT_REGION].reset() + + @mock_logs @mock_ec2 @mock_ecs