Fix file handle leaks in azure-ai-evaluation when exceptions occur during evaluation #42480
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The azure-ai-evaluation package had potential file handle leaks when the
_evaluate
function completed with errors. Specifically, temporary JSONL files created for ProxyClient usage were not being cleaned up if exceptions occurred at certain points during evaluation.Affected Scenarios
Exception during
_begin_aoai_evaluation
: When using a target function with ProxyClient, a temporary file is created in_preprocess_data
but may not be cleaned up if_begin_aoai_evaluation
fails before reaching_run_callable_evaluators
.OAI-only evaluation failures: When only graders are used (no local evaluators), the cleanup logic in
_run_callable_evaluators
is never reached if OAI evaluation fails.Early evaluation failures: Any exception occurring after temp file creation but before the cleanup logic in
_run_callable_evaluators
would leave files on disk.Root Cause
The cleanup logic was only present in
_run_callable_evaluators
, creating a gap where temporary files could be leaked if exceptions occurred elsewhere in the evaluation flow.Solution
Implemented centralized temporary file cleanup using a try-finally pattern at the
_evaluate
function level to ensure all temporary files are cleaned up regardless of where exceptions occur.Changes Made
Added tracking infrastructure: Extended
__ValidatedData
TypedDict to includetemp_files_to_cleanup: List[str]
Track temp file creation: Modified
_preprocess_data
to maintain a list of created temporary files and include it in the returned dataCentralized cleanup: Added try-finally block in
_evaluate
that guarantees cleanup of all tracked temporary files, with graceful error handling for edge casesRemoved duplicate logic: Simplified
_run_callable_evaluators
by removing the now-redundant cleanup codeCode Example
Before (problematic):
After (fixed):
Benefits
This fix ensures that temporary files created during evaluation are always cleaned up, preventing file handle leaks and potential disk space issues in long-running applications.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
pypi.org
/home/REDACTED/work/azure-sdk-for-python/azure-sdk-for-python/.venv/bin/python /home/REDACTED/work/azure-sdk-for-python/azure-sdk-for-python/.venv/lib/python3.9/site-packages/pip/__pip-REDACTED__.py install --ignore-installed --no-user --prefix /tmp/pip-build-env-sc3lg2hs/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i REDACTED -- setuptools>=40.8.0
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.