fix: Clean up temp CSV in AthenaQuery.as_dataframe (v2) - #6345
lucasjia-aws merged 1 commit into
Conversation
AthenaQuery.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
|
Unit tests failing, can you check. |
mohamedzeidan2021
left a comment
There was a problem hiding this comment.
Approving — the better-evidenced of the pair. Reproduced the leak and its absence the same way as #6344, with the adversarial cases (exception path, lazy reader) also clean.
This one has real end-to-end proof: the integ job ran the full suite and six tests that actually execute as_dataframe() passed against live Athena/S3 — test_create_feature_store, the three test_create_feature_store_ingest_with_*_target_stores variants, and both test_get_feature_group_with_* via get_feature_group_as_dataframe. test_create_feature_store calls it inside a while df.shape[0] < 11: retry loop, so repeated real invocations after deletion are covered.
The fix also matches an existing v2 convention — dataset_builder.py already does read-then-os.remove — and improves on it by being exception-safe. 2 new tests fail on reverted source; 56 passed in the module.
Three non-blocking notes:
test_athena_query_as_dataframe_cleanup_failure_does_not_raisepasses on unfixedmaster-v2, same as its v3 sibling.- The pre-existing unit test patches
tempfile.gettempdirto"tmp", so the fix now calls_remove_temp_file("tmp/query_id.csv")— a relative path. It's a no-op in CI, but it would delete a real./tmp/query_id.csvif a developer happened to have one in the repo root. Purely theoretical. @patch("pandas.read_csv", Mock(side_effect=ValueError("bad csv")))shares one decorator-time mock across invocations; harmless here, but neighbouring tests usepatch(..., side_effect=...)for a fresh mock per call.
dataset_builder.to_dataframe() downloads into ./ and os.removes without try/finally — same leak class, worth a follow-up.
Issue
#5100
Problem
sagemaker.feature_store.feature_group.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
as_dataframereturnspd.read_csv(filepath_or_buffer=output_filename, ...)directly with no cleanup.DatasetBuilder.to_dataframein the same package already removes its downloaded file after reading;as_dataframedid 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
tests/unit/sagemaker/feature_store/test_feature_group.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 tests/unit/sagemaker/feature_store/test_feature_group.py: 56 passed.black --checkandflake8pass on the changed files.V3 counterpart: #6344