Skip to content

Commit f92e9df

Browse files
authored
fix: prevent watch process crash on invalid template and improve sync test reliability (#8652)
- watch_manager: wrap _add_template_triggers in try/except within the infra sync error handler to prevent the watch process from crashing when the template is unparseable during error recovery - testing_utils: add descriptive message to bare ValueError in read_until, and catch ValueError in read_until_string to include expected output context - test_sync_adl: increase layer sync timeouts from 60s to 120s - test_sync_watch: increase nested stack infra sync timeout to 900s
1 parent 841aeb9 commit f92e9df

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

samcli/lib/sync/watch_manager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,13 @@ def _execute_infra_sync(self, first_sync: bool = False) -> None:
277277
)
278278
# Unschedule all triggers and only add back the template one as infra sync is incorrect.
279279
self._observer.unschedule_all()
280-
self._add_template_triggers()
280+
try:
281+
self._add_template_triggers()
282+
except Exception as trigger_err:
283+
LOG.warning(
284+
"Failed to re-add template triggers, will retry on next change: %s",
285+
trigger_err,
286+
)
281287
else:
282288
# Update stacks and repopulate triggers
283289
# Trigger are not removed until infra sync is finished as there

tests/integration/sync/test_sync_adl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def test_sync_watch_code(self):
129129
read_until_string(
130130
self.watch_process,
131131
"Finished syncing Layer HelloWorldFunction",
132-
timeout=60,
132+
timeout=120,
133133
)
134134
lambda_response = json.loads(self._get_lambda_response(lambda_functions[0]))
135135
self.assertEqual(lambda_response.get("message"), "hello mars")
@@ -143,7 +143,7 @@ def test_sync_watch_code(self):
143143
read_until_string(
144144
self.watch_process,
145145
"Finished syncing Layer HelloWorldFunction",
146-
timeout=60,
146+
timeout=120,
147147
)
148148
self._confirm_lambda_error(lambda_functions[0])
149149

@@ -155,7 +155,7 @@ def test_sync_watch_code(self):
155155
read_until_string(
156156
self.watch_process,
157157
"Finished syncing Function Layer Reference Sync HelloWorldFunction.\x1b[0m\n",
158-
timeout=60,
158+
timeout=120,
159159
)
160160

161161
def _verify_lambda_response(_lambda_response):

tests/integration/sync/test_sync_watch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def test_sync_watch_infra_nested_stack(self):
301301
self.test_data_path.joinpath("infra", "template-python-before.yaml"),
302302
)
303303

304-
read_until_string(self.watch_process, "\x1b[32mInfra sync completed.\x1b[0m\n", timeout=600)
304+
read_until_string(self.watch_process, "\x1b[32mInfra sync completed.\x1b[0m\n", timeout=900)
305305

306306
# Updated Infra Validation
307307
self.stack_resources = self._get_stacks(self.stack_name)

tests/testing_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ def _compare_output(output, _: List[str]) -> bool:
243243
raise TimeoutError(
244244
f"Did not get expected output after {timeout} seconds. Expected output: {expected_output_bytes!r}"
245245
) from ex
246+
except ValueError as ex:
247+
expected_output_bytes = expected_output.encode("utf-8")
248+
raise ValueError(
249+
f"Process ended before expected output was found. Expected output: {expected_output_bytes!r}"
250+
) from ex
246251

247252

248253
def read_until(process: Popen, callback: Callable[[str, List[str]], bool], timeout: int = 5):
@@ -285,7 +290,7 @@ def _read_output():
285290
if isinstance(result, Exception):
286291
raise result
287292
else:
288-
raise ValueError()
293+
raise ValueError("Process ended before expected output was found.")
289294

290295

291296
class FileCreator(object):

0 commit comments

Comments
 (0)