Skip to content

Commit 1b4fa7c

Browse files
committed
fix(coding_agent): add context managers and narrow exception handling
1 parent d558893 commit 1b4fa7c

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

contributing/samples/coding_agent/agent.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import binascii
5050
from datetime import datetime
5151
import os
52+
import socket
5253
import urllib.error
5354
import urllib.request
5455

@@ -127,19 +128,19 @@ def fetch_url(url: str) -> dict:
127128
"url": url,
128129
"success": True,
129130
}
130-
except urllib.error.URLError as e:
131+
except (urllib.error.URLError, socket.timeout) as e:
131132
return {
132133
"content": "",
133134
"url": url,
134135
"success": False,
135136
"error": f"Failed to fetch URL: {str(e)}",
136137
}
137-
except Exception as e:
138+
except OSError as e:
138139
return {
139140
"content": "",
140141
"url": url,
141142
"success": False,
142-
"error": f"Unexpected error: {str(e)}",
143+
"error": f"Failed to decode response: {str(e)}",
143144
}
144145

145146

@@ -256,11 +257,6 @@ def save_chart(image_data: str, filename: str) -> dict:
256257
"success": False,
257258
"error": f"Failed to save file: {str(e)}",
258259
}
259-
except Exception as e:
260-
return {
261-
"success": False,
262-
"error": f"Unexpected error: {str(e)}",
263-
}
264260

265261

266262
def list_saved_charts() -> dict:
@@ -293,7 +289,7 @@ def list_saved_charts() -> dict:
293289
"directory": HOST_CHARTS_DIR,
294290
"count": len(charts),
295291
}
296-
except Exception as e:
292+
except OSError as e:
297293
return {
298294
"success": False,
299295
"error": f"Failed to list charts: {str(e)}",

src/google/adk/agents/coding_agent.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,11 @@ def cleanup(self) -> None:
650650
self._coding_executor = None
651651
self._resolved_tools = None
652652

653-
def __del__(self):
654-
"""Destructor to clean up resources."""
655-
try:
656-
self.cleanup()
657-
except Exception:
658-
pass
653+
def __enter__(self) -> "CodingAgent":
654+
"""Enter context manager and return self."""
655+
return self
656+
657+
def __exit__(self, exc_type, exc, traceback) -> bool:
658+
"""Exit context manager and clean up resources."""
659+
self.cleanup()
660+
return False

src/google/adk/code_executors/coding_agent_code_executor.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ def cleanup(self) -> None:
505505
self._stop_tool_server()
506506
self._execution_history.clear()
507507

508-
def __del__(self):
509-
"""Destructor to clean up resources."""
508+
def __enter__(self) -> CodingAgentCodeExecutor:
509+
"""Enter context manager and return self."""
510+
return self
511+
512+
def __exit__(self, exc_type, exc, traceback) -> bool:
513+
"""Exit context manager and clean up resources."""
510514
self.cleanup()
515+
return False

0 commit comments

Comments
 (0)