Skip to content

Prevent download_artifact from overwriting late-created destinations - #6904

Merged
c-bata merged 2 commits into
optuna:masterfrom
HuaTNA:fix-exclusive-artifact-download
Nov 9, 2026
Merged

c-bata merged 2 commits into
optuna:masterfrom
HuaTNA:fix-exclusive-artifact-download

Conversation

@HuaTNA

@HuaTNA HuaTNA commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Closes #6903. download_artifact checks 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 real FileSystemArtifactStore.

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:

  • A local deterministic reproduction fails on master 988b115a8 and 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.
  • Configured pre-commit Ruff checks and git diff --check pass. The configured mypy 1.16.0 hook reports an unrelated dict-item error at optuna/visualization/_intermediate_values.py:95; the identical command on an untouched clone of 988b115a8 reports the same error. Current development-environment mypy 2.4.0 passes for the changed source file.
  • Standalone two-thread reproduction now raises FileExistsError and 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

  • I used an LLM while working on this PR.

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.

Comment on lines +49 to +73


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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@gen740 gen740 self-assigned this Oct 16, 2026

@gen740 gen740 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@c-bata c-bata self-assigned this Oct 23, 2026
@c-bata c-bata added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Oct 23, 2026
@c-bata c-bata added this to the v5.1.0 milestone Oct 23, 2026
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@c-bata c-bata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@c-bata
c-bata merged commit 45a45a7 into optuna:master Nov 9, 2026
11 checks passed
@c-bata c-bata removed the stale Exempt from stale bot labeling. label Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

download_artifact can overwrite a destination created while opening its reader

3 participants