fix: Clean up temp CSV in AthenaQuery.as_dataframe - #6344
Conversation
as_dataframe() downloaded the Athena query result to <tempdir>/<execution_id>.csv and never removed it, so every query left a file behind and repeated queries could fill the local disk. Wrap the download and read_csv in try/finally and remove the temporary file afterwards, including when the download or parsing fails. Cleanup is best-effort: an OSError is logged as a warning and does not affect the returned DataFrame. Fixes aws#5100
|
Claude finished @lucasjia-aws's task in 52s —— View job Code Review — Clean up temp CSV in
|
Issue
#5100
Problem
AthenaQuery.as_dataframe()downloads the Athena query result to<tempdir>/<execution_id>.csv, loads it withpd.read_csv, and never deletes the file. Each query leaves a CSV behind, so running many queries (or queries with large results) can fill up local disk space.Root cause
sagemaker-mlops/src/sagemaker/mlops/feature_store/athena_query.pyreturnspd.read_csv(output_file, ...)directly with no cleanup. The other download paths in the same module (feature_utils.download_csv_from_s3) already remove their local file after reading;as_dataframewas the only one that did not.Fix
read_csvintry/finallyand remove the temporary file infinally, so it is deleted after a successful read and also when the download or parsing raises._remove_temp_filehelper: it only removes the file if it exists, and anOSError(for example on Windows when achunksize/iteratorreader still holds the file open) is logged as a warning instead of failing the call.Testing
sagemaker-mlops/tests/unit/sagemaker/mlops/feature_store/test_athena_query.py: the temp file is removed after a successful load (using a real temp directory), it is removed whenread_csvraises, and a failingos.removedoes not break the call.pytest sagemaker-mlops/tests/unit/sagemaker/mlops/feature_store(excludingfeature_processor, which needs pyspark): 389 passed.black --checkandflake8pass on the changed files.V2 counterpart: #6345