Repository navigation
Prevent download_artifact from overwriting late-created destinations - #6904
Conversation
|
|
||
|
|
||
| def test_download_artifact_preserves_destination_created_during_open_reader( | ||
| tmp_path: pathlib.Path, artifact_store: ArtifactStore | ||
| ) -> None: | ||
| source = tmp_path / "source.txt" | ||
| source.write_bytes(b"artifact contents") | ||
| study = optuna.create_study() | ||
| artifact_id = upload_artifact( | ||
| study_or_trial=study, file_path=str(source), artifact_store=artifact_store | ||
| ) | ||
| destination = tmp_path / "destination.txt" | ||
| original_open_reader = artifact_store.open_reader | ||
|
|
||
| def open_reader(artifact_id: str) -> BinaryIO: | ||
| destination.write_bytes(b"other task's contents") | ||
| return original_open_reader(artifact_id) | ||
|
|
||
| with patch.object(artifact_store, "open_reader", side_effect=open_reader): | ||
| with pytest.raises(FileExistsError): | ||
| download_artifact( | ||
| artifact_store=artifact_store, artifact_id=artifact_id, file_path=str(destination) | ||
| ) | ||
|
|
||
| assert destination.read_bytes() == b"other task's contents" |
There was a problem hiding this comment.
Thanks for the fix! The change looks reasonable to me.
However, I think the additional test might be unnecessary. I'd prefer to keep the tests focused on successful downloads rather than explicitly specifying the behavior under concurrent writes.
Could you remove the new test?
There was a problem hiding this comment.
Removed the added test and its unused imports in 8892768. The PR now only changes the destination open mode from "wb" to "xb". Re-ran the artifact test suite: 26 passed. The configured Ruff checks also pass; the previously documented local pinned-mypy baseline error is unchanged. I updated the description to match the reduced diff. Thanks for the feedback!
|
This pull request has not seen any recent activity. |
Motivation
Closes #6903.
download_artifactchecks whether its destination exists before opening the artifact reader, but then opens the destination with"wb". If another task creates the file during reader setup, the download silently truncates that task's file. This was found through code inspection and reproduced on current master with two synchronized threads and a realFileSystemArtifactStore.Description of the changes
Use
"xb"to enforce the existing no-overwrite behavior at file creation. Keep the early existence check so already-present files still fail before contacting the store.Validation on Python 3.13.13 / macOS arm64:
988b115a8and passes with this change. The proposed regression test was removed at the reviewer's request; no tests are added in the final diff.python -m pytest tests/artifacts_tests -q: 26 passed.git diff --checkpass. The configured mypy 1.16.0 hook reports an unrelateddict-itemerror atoptuna/visualization/_intermediate_values.py:95; the identical command on an untouched clone of988b115a8reports the same error. Current development-environment mypy 2.4.0 passes for the changed source file.FileExistsErrorand preserves the other task's bytes. Additional local checks cover a fresh download, existing-file fast failure, missing artifact, dangling destination symlink and reader cleanup.The full Optuna test suite and live cloud artifact services were not run. This does not make an incomplete download atomic or change recovery after a stream-copy failure.
Checklist
Codex assisted with code investigation, the minimal implementation, regression coverage and local test execution. The discovery and validation above are from those local runs, not a production incident.