Repository navigation
Make the write session public and share the file drivers (GH-1455) - #1627
laughingman7743 wants to merge 8 commits into
Conversation
| taken = self._state is S3WriteState.COMMITTING and self._reservation is state | ||
| if not taken: | ||
| self._refuse_unless(state, operation="commit") | ||
| if body and self._multipart: |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. FINDINGS, repaired: commit(body) raised ValueError for a non-empty body before it read the state. A multipart write that had committed did not return its recorded result, and a failed or discarded one did not raise its state error. The body is now checked once the state allows the commit. test_commit_rejects_a_body_of_a_multipart_upload covers both and fails with the old order.
| if target.append_data is not None: | ||
| self.write(target.append_data) | ||
| # Not kept twice while the file is open: the target is. | ||
| target.append_data = None |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. FINDINGS, repaired: a write-mode file now keeps its S3FileTarget, which held the bytes of a small appended object (up to the minimum part size) after the file had written them into its buffer. The target drops them there, here and in s3_async.py:2671.
| handler = UploadIdLogged() | ||
| logger = logging.getLogger("pyathena.filesystem.s3") | ||
| # The logger of the session, whose abort_or_log() logs the upload ID. | ||
| logger = logging.getLogger("pyathena.filesystem.s3_writer") |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. FINDINGS, repaired: this test listened on pyathena.filesystem.s3, while abort_or_log() logs on pyathena.filesystem.s3_writer. Its finally released the part either way, so it no longer checked that the upload ID is logged. It now listens on the session's logger and asserts the record, and it fails with the interrupt log removed.
| max_workers=4, | ||
| ) | ||
| try: | ||
| if size >= 5 * 2**20: |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. FINDINGS, repaired: the example started a multipart upload for any stream, so an empty stream sent a completion without parts. It now writes a short stream with PutObject, as the S3WriteSession docstring example does.
| state = S3WriteState.ACTIVE if self._multipart else S3WriteState.IDLE | ||
| self._refuse_unless(state, operation="commit") | ||
| # Set first, so that a COMPLETING state that this sets always has | ||
| # Set first, so that a COMMITTING state that this sets always has |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. FINDINGS, repaired: this comment and the one in withdraw_commit() named the removed COMPLETING state.
| return dict(self._prepared_params.get(method) or {}) | ||
|
|
||
| def _request_core(self) -> S3Core: | ||
| def _request_core(self, method: str) -> S3Core: |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. Rejected: a reviewer pass suggested merging prepared parameters with the session's request_kwargs instead of replacing them. Decision 4 on #1455 says a prepared entry replaces the session's defaults for that operation. The core's own request_kwargs are still added, and no caller passes both. The multipart copy passes prepared parameters with its plan's defaults on the core, and the files pass request_kwargs only. test_prepared_params_replace_request_kwargs pins the rule. /simplify also suggested one _send() helper for the request sites; skipped, since the creation and the parts take their parameters when they are submitted, and the rule lives in this method already.
| if_match_from_lookup: bool = False | ||
| # The commit requests of the write session counted when the cache of the | ||
| # path was last invalidated after a commit (see commit()). | ||
| _invalidated_commit_attempts: int = field(default=0, init=False, repr=False, compare=False) |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. Rejected: a reviewer pass objected to the per-write counter living on S3FileTarget. A target is resolved for one file and builds that file's one session; write_session() now says so. No second session is built on a target.
| fs._invalidate(path) | ||
| self._invalidated_commit_attempts = session.commit_attempts | ||
|
|
||
| def abort(self, fs: S3FileSystem, session: S3WriteSession, abort: Callable[[], None]) -> None: |
There was a problem hiding this comment.
Self-review round one (implementation behavior; /code-review high and /simplify), base 06d48025, reviewed head e0e50e61, repairs in 54c5e715. Rejected, as decided before implementation: the callable form and the private _logging_abort_failure() keep the sync file's eviction on a failed abort (NoSuchBucket) and keep aio without it. The differential check's part1_abort_nobucket rows show the sync eviction unchanged. Adding the eviction to aio would change aio behavior, so it belongs in a separate change.
Covered in round one: the session state machine (commit, reservation, withdrawal, abort, waiters without settled()), the shared target methods, both files' write, commit, discard and finalizer paths, the sync copy, the tests, and the docs. /simplify (reuse, simplification, efficiency, altitude) applied a docstring clarification and matching example names. It skipped getvalue() or None (it keeps the mock-level put_object(path, None) calls), the fs parameter of iter_append_copies (tests override the part limits per core), and the name-valued enum (decided). The differential check was rerun after the repairs: unchanged.
| raise | ||
| ``` | ||
|
|
||
| `start()` creates the multipart upload, which needs at least one part before `commit()`; without it, `commit(body)` writes `body` with PutObject, and `None` or empty bytes write an empty object. |
There was a problem hiding this comment.
Self-review round two (claims, compatibility, operations), base 06d48025, head 54c5e715. FINDINGS, repaired: this said the parts must hold at least one byte. A completion needs at least one part, so it now says that.
| S3MultipartStateError: If no upload is active, or the write failed | ||
| earlier or has been discarded. | ||
| raised yet, or the failure of the commit request. | ||
| S3WriteStateError: If the multipart upload is being created, the |
There was a problem hiding this comment.
Self-review round two (claims, compatibility, operations), base 06d48025, head 54c5e715. FINDINGS, repaired: the Raises entry omitted a commit refused while another call commits the write.
Claims checked:
- the error messages read as before: the differential check compares them (only the type name changes, as an alias)
settled()had no source caller- no caller passes both prepared parameters and
request_kwargs - the range read and splitter were already shared
- the docs'
abort()andcommit_attemptsdescriptions
The PR body's WHY said the abort-failure logs were all worded differently; the two file sites used the same text, so the body was corrected.
Compatibility: S3MultipartStateError is the same class, so except and isinstance keep working. _S3WriteSession was private. The S3FileTarget field is init=False and compare=False. Operations: no request changes in any of the 2966 rows. This is a static and offline review; the AWS jobs have not run yet.
Publish S3WriteSession and S3WriteState in s3_writer.py. The states take plain names (COMMITTING and COMMITTED replace COMPLETING and COMPLETED), and the error messages describe them separately. One commit(body=None) sends PutObject or completes the multipart upload, chosen by whether the write started one, and rejects a non-empty body for a multipart upload; put() and complete() become private. begin_complete()/withdraw_complete() become reserve_commit()/withdraw_commit(). settled(), which no caller uses, is removed. abort_or_log() logs a failed or interrupted abort with the upload ID and replaces the abort-failure log sites of both files and of the multipart copy. Prepared parameters of an operation replace the session's request_kwargs for that operation. S3MultipartStateError becomes S3WriteStateError; the former name refers to the same class. S3File and AioS3StreamedFile keep their S3FileTarget, which builds the session and holds the shared append copies, commit, and abort; the blocking and awaited waits, the buffering, and the finalizers stay on each file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1455) commit(body) on a multipart write returned ValueError before it looked at the state, so a commit after a success did not return the recorded result, and a failed or discarded write did not raise its state error. The body is now checked once the state allows the commit. A write-mode file keeps its S3FileTarget, which held the data of a small appended object after the file had written it into its buffer; the target drops it there. The interrupted-copy test listens on the session's logger, which now logs the upload ID, and checks the record. The documentation example writes a short stream with PutObject, the commit docstring names the refusal of a concurrent commit, and stale comments name the COMMITTING state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…H-1455) The shared commit of S3FileTarget invalidated the cache of the path before the file aborted the failed upload, and an interrupt of the invalidation after a successful commit closed a sync file without its buffer. The file's cleanup now runs inside the commit, before the invalidation, as before the drivers were shared. The session takes a reserved commit before it checks the body, so that a rejected body fails the write and abort() aborts the upload, and it resolves the waits of ready() when the write starts to be committed. write_session() keeps the version of the target, which the session refuses, and starts the invalidation record over; the append plan yields no copy for an object that the file rewrites from its buffer. The docstrings and docs name the fields that S3Core sets itself and when an interrupted abort is logged; the log tests check the level and the upload ID instead of the wording. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
54c5e71 to
6971bc2
Compare
| session.commit(body) | ||
| except BaseException: | ||
| if on_failure is not None: | ||
| on_failure() |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
1. Valid, a regression of this PR; repaired. The shared commit invalidated the cache of the path before the file aborted the failed upload. An entry cached while the abort ran could then survive a completion that took effect despite its failure. An interrupt of the invalidation after a successful flush(force=True) also closed a sync file without its buffer. The file's cleanup (on_failure) now runs inside the commit, before the invalidation, as on master. test_failed_commit_invalidates_after_the_abort (sync) and test_failed_completion_invalidates_after_the_abort (aio) fail with the cleanup moved after the invalidation.
| state = S3WriteState.ACTIVE if self._multipart else S3WriteState.IDLE | ||
| waiters: list[Future[None]] = [] | ||
| if self._state is S3WriteState.COMMITTING and self._reservation is state: | ||
| # Reserved by reserve_commit(), and taken before the body |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
2. Valid; repaired. After reserve_commit(), commit(b"extra") raised ValueError with the reservation still held. The write stayed COMMITTING, so abort() left the upload open. The commit now takes the reservation before it checks the body, so the rejected body fails the write and abort() aborts it (test_rejected_body_of_a_reserved_commit_fails_the_write).
| # abort, rather than being committed. | ||
| reserved = True | ||
| self._state = S3WriteState.COMMITTING | ||
| # The waits of ready() end, as no part can be submitted. |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
3. Valid, pre-existing on master (begin_complete/_commit); fixed here since ready() is now public. Entering COMMITTING did not resolve pending ready() futures until another change published. Both reserve_commit() (line 803) and a direct commit() now publish (test_commit_ends_the_waits_of_ready, both forms fail without it).
| self._invalidated_commit_attempts = 0 | ||
| return S3WriteSession( | ||
| fs.core, | ||
| S3Path(self.bucket, self.key, self.version_id), |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
4. Valid for the public S3FileTarget; repaired. write_session() dropped the target's version. It now passes it, so the session refuses the write before any request (test_write_session_refuses_a_version). The files only build sessions for write modes, which have no version.
5. Repaired. A second session on one target reused the first one's invalidation record, so its first commit skipped the invalidation (also raised in round one). write_session() now starts the record over (line 5918).
| which the file rewrites from its buffer (see ``append_data``). | ||
| """ | ||
| size = self.appended.get("size", 0) if self.appended is not None else 0 | ||
| if size < fs.core.MULTIPART_UPLOAD_MIN_PART_SIZE: |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
6. Valid for the public planner; repaired. iter_append_copies() yielded a whole-object copy for an appended object that the file rewrites from its buffer. It now yields nothing below the minimum part size (test_iter_append_copies). The files call it only for larger objects, so their behavior is unchanged.
| ``create_multipart_upload``, which are sent as given for that | ||
| operation, so that botocore rejects one that the operation | ||
| does not accept, under the fields that the core sets itself, | ||
| such as the checksum algorithm of the upload. They replace |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
7. Valid as documentation; repaired. The docstrings and docs/filesystem.md now say that prepared parameters yield to the fields that S3Core sets itself, such as the upload's checksum algorithm. They also say that None or empty bytes send no body, as S3Core.put_object() does, which writes a Body of request_kwargs in that case (line 704). The session test keeps checking the body that the session passes to the core. S3Core.put_object has its own tests for the request.
| failure is logged with the ID of the upload, which is kept so that a | ||
| later :meth:`abort` can retry. An interrupt, such as | ||
| ``KeyboardInterrupt``, is raised, and logged with the ID of the upload | ||
| if the interrupted abort keeps it; an interrupt of the wait for the |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
8. Valid; repaired. abort_or_log() now says that an interrupt is logged only if the interrupted abort keeps the upload. An interrupt of the wait for the creation is raised once the upload is aborted.
| assert session.state is S3WriteState.COMMITTED | ||
| assert requests.completed == [[1]] | ||
|
|
||
| def test_withdrawal_leaves_a_running_commit(self): |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
9. Valid; repaired. The reservation test now withdraws while the completion request is held, checks that the state stays COMMITTING, and checks that the commit succeeds after the release. Withdrawing a taken reservation makes it fail.
| session.abort_or_log() | ||
| level = logging.WARNING if interrupted else logging.ERROR | ||
| assert [(r.levelno, "uploadid" in r.getMessage()) for r in caplog.records] == [ | ||
| (level, True) |
There was a problem hiding this comment.
Independent review (Codex), relayed. Reviewer: Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d1a-b868-7f63-8023-c9ed9ddf27f0. Static review of a detached snapshot, base 06d48025, head 54c5e715; no PR text, commit message, or prior findings were given. Result: FINDINGS (10). Repairs in 5b5370de, rebased onto e0ab4875 as 6971bc25.
10. Valid; repaired. This test and the multipart-copy interrupt test (test_s3.py:9863) check the level and the upload ID instead of the wording. The copy test keeps its record assertion, since its handler-based check passes even without the log (its finally releases the part).
After the repairs:
just test offline: 4960 passed, 6 skipped, on6971bc25.- The differential check, rerun on the repaired branch, shows only the three intended differences.
- Each new test fails with its behavior removed.
The shared commit read the buffer before the block whose failure runs the file's cleanup, and evicted stale entries before that cleanup. The body is now read inside that block, and the cleanup runs inside the eviction, so that an entry cached while the upload is aborted is evicted too, as the async file did before the drivers were shared. The sync file passes no buffer to a multipart commit, which would keep it in the error's traceback. A reserved commit raises a failed part before it rejects its body. The docs say which fields prepared parameters must not give and that a Body of request_kwargs is sent when the commit has no body of its own. The order tests observe the cache of a filesystem instead of the calls, and a test covers an interrupt of the invalidation after a successful commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| body = None | ||
| if buffer is not None and not session.multipart: | ||
| # None without data, which writes an empty object as well. | ||
| body = buffer.getvalue() or None |
There was a problem hiding this comment.
Independent follow-up review (Codex), relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d4b-8874-7f71-bcaa-87fac24d0c81. Static review of a detached snapshot of 6971bc25 (merge base e0ab4875). It covered the repair commit, the unchanged range-diff of the earlier commits, and the upstream filesystem commits since 06d48025. Result: FINDINGS (9). Repairs in 3d20335a.
2. Valid; repaired. buffer.getvalue() ran before the block whose failure runs the cleanup, so an interrupt there left a sync file with its buffer and an IDLE session. The body is now read inside that block.
3. Valid; repaired. The eviction of the commit's error ran before the cleanup (the abort), so an entry cached while the abort ran could survive. The cleanup now runs inside _evicting_stale, as the async driver did on master.
The tests test_failed_commit_invalidates_after_the_abort (sync, test_s3.py:13631) and its async counterpart now cache the object during the mocked abort and check the cache afterwards on an offline filesystem (finding 8). Both fail with the cleanup moved after the invalidation.
| session, | ||
| # A multipart upload has its data in its parts; the buffer, which | ||
| # the error of a failed commit would keep, is not passed. | ||
| None if session.multipart else self.buffer, |
There was a problem hiding this comment.
Independent follow-up review (Codex), relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d4b-8874-7f71-bcaa-87fac24d0c81. Static review of a detached snapshot of 6971bc25 (merge base e0ab4875). It covered the repair commit, the unchanged range-diff of the earlier commits, and the upstream filesystem commits since 06d48025. Result: FINDINGS (9). Repairs in 3d20335a.
6. Valid; repaired. The sync driver passed its buffer to a multipart commit, whose failure would keep it in the traceback. It now passes None; the data is in the parts.
8 (second part). Valid; repaired. test_interrupted_invalidation_keeps_a_committed_file_open (test_s3.py:13657) interrupts the invalidation after a successful commit of a force-flushed file. It checks that the file stays open and that a retry sends no second PutObject. It fails with the cleanup wrapped around the shared commit.
| self._refuse_unless(state, operation="commit") | ||
| if body and self._multipart: | ||
| if reserved: | ||
| # The first failure of a request, if any, before the |
There was a problem hiding this comment.
Independent follow-up review (Codex), relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d4b-8874-7f71-bcaa-87fac24d0c81. Static review of a detached snapshot of 6971bc25 (merge base e0ab4875). It covered the repair commit, the unchanged range-diff of the earlier commits, and the upstream filesystem commits since 06d48025. Result: FINDINGS (9). Repairs in 3d20335a.
4. Valid; repaired. A reserved commit that rejected its body hid a part failure recorded after the reservation. It now raises that failure first, as the completion would (test_rejected_body_of_a_reserved_commit_raises_a_failed_part, which fails without the check).
| botocore rejects one that the operation does not accept. | ||
| ``create_multipart_upload``, which are sent as given for that | ||
| operation, so that botocore rejects one that the operation | ||
| does not accept. They must not give the fields that the core |
There was a problem hiding this comment.
Independent follow-up review (Codex), relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d4b-8874-7f71-bcaa-87fac24d0c81. Static review of a detached snapshot of 6971bc25 (merge base e0ab4875). It covered the repair commit, the unchanged range-diff of the earlier commits, and the upstream filesystem commits since 06d48025. Result: FINDINGS (9). Repairs in 3d20335a.
7. Valid as documentation; repaired. This docstring and docs/filesystem.md now say that prepared parameters must not give the fields the core sets from the path, the upload, and the part (Bucket, Key, UploadId, PartNumber). They yield to the upload's checksum algorithm, and a Body of request_kwargs is sent when the commit has no body of its own. Not adopted: checking the request at the botocore boundary in the session test. The session passes the body through, and S3Core.put_object has its own request tests.
| # abort, rather than being committed. | ||
| reserved = True | ||
| self._state = S3WriteState.COMMITTING | ||
| # The waits of ready() end, as no part can be submitted. |
There was a problem hiding this comment.
Independent follow-up review (Codex), relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d4b-8874-7f71-bcaa-87fac24d0c81. Static review of a detached snapshot of 6971bc25 (merge base e0ab4875). It covered the repair commit, the unchanged range-diff of the earlier commits, and the upstream filesystem commits since 06d48025. Result: FINDINGS (9). Repairs in 3d20335a.
5. Rejected. Waiters are taken under the lock and resolved after it is released at every state change of the session. That is how master publishes too: _on_part_done, withdraw_complete, and abort all do it. An interrupt between the two steps can strand a waiter at any of them. This PR adds publishing to the two transitions into COMMITTING, which did not publish at all, so it narrows the case. A sync wait_ready() also rechecks every second. A change to how all transitions resolve waiters is outside this refactor.
| ValueError: If the target names a version, which a write cannot | ||
| replace, or the block size is invalid. | ||
| """ | ||
| self._invalidated_commit_attempts = 0 |
There was a problem hiding this comment.
Independent follow-up review (Codex), relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d4b-8874-7f71-bcaa-87fac24d0c81. Static review of a detached snapshot of 6971bc25 (merge base e0ab4875). It covered the repair commit, the unchanged range-diff of the earlier commits, and the upstream filesystem commits since 06d48025. Result: FINDINGS (9). Repairs in 3d20335a.
1. Rejected. It needs two sessions created on one target before either commits. A target is resolved for one file and builds that file's session: write_session() says so, and the files build one session each. write_session() restarts the invalidation record for a session built after another.
9. Partly adopted. The log tests filter the session logger's records by upload ID and check the level. The ValueError checks keep a short match, which the repository's lint (ruff PT011) requires for pytest.raises(ValueError).
After the repairs:
just test offline: 4962 passed, 6 skipped, on3d20335a.- The new tests were seen to fail with their behavior removed.
- The differential check on
3d20335a(basee0ab4875, two identical base runs) shows only the three intended differences.
A failed commit evicts the entries that its error shows to be stale before the cleanup, which an interrupt may end, and again after it, for an entry cached while the upload was aborted. A commit that fails before the session takes its reservation, such as on a closed buffer, withdraws the reservation. A reserved commit consumes finished part requests before it rejects a body, so that a part failure whose callback has not run is raised first. S3FileTarget.write_session() and iter_append_copies() take the core of the file's filesystem, so that the async file sends through the core of AioS3FileSystem, as before the drivers were shared. The docs name the payload parameters that only PutObject inherits and the Body that a commit without a body sends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| # Evicts again after the cleanup, for an entry cached meanwhile. | ||
| with fs._evicting_stale(path): | ||
| try: | ||
| # Evicts before the cleanup, which an interrupt may end. |
There was a problem hiding this comment.
Independent follow-up review (Codex), second round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d60-bc63-7a91-8861-a7fffe210bb7. Static review of a detached snapshot of 3d20335a (merge base e0ab4875), covering the repair commit and the full change. Result: FINDINGS (8). Repairs in 8695cc58.
1. Valid, introduced by the previous repair; repaired. Moving the cleanup inside the eviction lost the sync eviction of a missing bucket when an interrupt ends the abort. The commit now evicts the error's stale entries before the cleanup (this block) and again after it (the outer block).
test_missing_bucket_of_a_failed_commit_is_evicted uses a sibling entry and NoSuchBucket:
- cached before the commit, with the abort interrupted: fails without the inner eviction;
- cached during the abort: fails without the outer eviction.
| body = buffer.getvalue() or None | ||
| session.commit(body) | ||
| except BaseException: | ||
| session.withdraw_commit() |
There was a problem hiding this comment.
Independent follow-up review (Codex), second round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d60-bc63-7a91-8861-a7fffe210bb7. Static review of a detached snapshot of 3d20335a (merge base e0ab4875), covering the repair commit and the full change. Result: FINDINGS (8). Repairs in 8695cc58.
4. Valid; repaired. A commit that failed before the session took its reservation, such as getvalue() on a closed buffer, left a reserved PutObject in COMMITTING, which abort() leaves alone. The failure now withdraws an untaken reservation. A reservation that the commit took makes this a no-op. test_commit_withdraws_a_reservation_that_it_did_not_take fails without it.
| max_workers=self.max_workers, | ||
| request_kwargs=self.s3_additional_kwargs, | ||
| self._session = target.write_session( | ||
| fs.core, self._requests, block_size=block_size, max_workers=self.max_workers |
There was a problem hiding this comment.
Independent follow-up review (Codex), second round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d60-bc63-7a91-8861-a7fffe210bb7. Static review of a detached snapshot of 3d20335a (merge base e0ab4875), covering the repair commit and the full change. Result: FINDINGS (8). Repairs in 8695cc58.
5. Valid, a difference this PR introduced; repaired. The async file built its session on fs._sync_fs.core instead of fs.core, so a subclass whose core adds parameters would lose them. write_session() and iter_append_copies() now take the core (s3.py:5920), and each file passes fs.core, as on master. No test was added: master already used fs.core, and the change restores it.
| # The first failure of a request, if any, also of one | ||
| # whose callback has not run yet, before the error of | ||
| # the caller. | ||
| self._consume_finished() |
There was a problem hiding this comment.
Independent follow-up review (Codex), second round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d60-bc63-7a91-8861-a7fffe210bb7. Static review of a detached snapshot of 3d20335a (merge base e0ab4875), covering the repair commit and the full change. Result: FINDINGS (8). Repairs in 8695cc58.
3. Valid; repaired. A part failure whose done callback had not run yet lost to the body's ValueError. The reserved commit now consumes finished requests first, as the completion does. test_rejected_body_of_a_reserved_commit_raises_a_failed_part defers the callback and fails without the consumption.
| self._reservation = state | ||
| self._state = _S3WriteState.COMPLETING | ||
| self._state = S3WriteState.COMMITTING | ||
| # The waits of ready() end, as no part can be submitted. |
There was a problem hiding this comment.
Independent follow-up review (Codex), second round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d60-bc63-7a91-8861-a7fffe210bb7. Static review of a detached snapshot of 3d20335a (merge base e0ab4875), covering the repair commit and the full change. Result: FINDINGS (8). Repairs in 8695cc58.
2. Rejected. On master, a reservation left the waiters for a later transition. Every later transition (part completion, withdrawal, abort, completion) takes its waiters under the lock and resolves them after releasing it, with the same window for an interrupt. Publishing at the reservation resolves the waiters at once in the normal case, instead of after the part completes. The window between taking and notifying is the same one that every publishing transition on master has. The file drivers never wait on ready() across a reservation: the async final flush waits for the flush in progress before it reserves, and the sync wait_ready() rechecks every second. A change to how all transitions resolve waiters is outside this refactor.
6. Rejected. Concurrent commit() calls on one file race on the invalidation record as they did on master's file-local counter. One file is not used from several threads at once (threadsafety 2).
| (see :meth:`S3Core.with_request_kwargs`). The mapping is read | ||
| (see :meth:`S3Core.with_request_kwargs`), except those that | ||
| describe the payload of the object, such as ``ContentMD5``, | ||
| which it sends with PutObject only (see :meth:`S3Core.call`). |
There was a problem hiding this comment.
Independent follow-up review (Codex), second round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d60-bc63-7a91-8861-a7fffe210bb7. Static review of a detached snapshot of 3d20335a (merge base e0ab4875), covering the repair commit and the full change. Result: FINDINGS (8). Repairs in 8695cc58.
7. Valid as documentation; repaired. This docstring and docs/filesystem.md now say that payload parameters such as ContentMD5 reach PutObject only. They also say that a commit without a body sends a Body from request_kwargs or from the prepared put_object parameters.
| assert isinstance(result, S3CompleteMultipartUpload if multipart else S3PutObject) | ||
| assert session.state is S3WriteState.COMMITTED | ||
| assert session.commit_attempts == 1 | ||
| # Empty bytes and None send the same request. |
There was a problem hiding this comment.
Independent follow-up review (Codex), second round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d60-bc63-7a91-8861-a7fffe210bb7. Static review of a detached snapshot of 3d20335a (merge base e0ab4875), covering the repair commit and the full change. Result: FINDINGS (8). Repairs in 8695cc58.
8. Partly adopted. The commit test no longer distinguishes b"" from None handed to the core. Kept: the matches on the session's state messages (has been discarded, was withdrawn). These are user-facing messages, and the suite matches on them elsewhere too (for example test_s3.py and test_s3_async.py). The ValueError checks keep a short match for ruff PT011.
After the repairs:
just test offline: 4965 passed, 6 skipped, on8695cc58.- The new tests were seen to fail with their behavior removed.
- The differential check on
8695cc58(basee0ab4875) shows only the three intended differences.
iter_append_copies() decided with the limits of the core it was given whether an appended object is rewritten from the buffer, which resolve() had decided with the limits of the filesystem; with different limits, an appended object could be neither copied nor rewritten. It now plans the copies of the appended object, for one that resolve() did not read into append_data, as the files call it. Any commit, reserved or not, consumes finished part requests and raises a failed part before it rejects a body. The docs name the checksums that the completion of a FULL_OBJECT upload inherits and the parameters from which a PutObject without a body takes its Body. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| ) -> Iterator[tuple[int, tuple[int, int] | None]]: | ||
| """Plan the part copies of the appended object, which lead the multipart upload. | ||
|
|
||
| For an appended object that the file copies, which :meth:`resolve` |
There was a problem hiding this comment.
Independent follow-up review (Codex), third round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d77-7211-76e1-bba0-5e12a1dc3a06. Static review of a detached snapshot of 8695cc58 (merge base e0ab4875), limited to that repair and what it affects. Result: FINDINGS (6). Repairs in 91dd6bd9.
1. Valid, introduced by an earlier repair; repaired. The planner skipped an appended object below the minimum part size of the core it was given. resolve() decides copy or rewrite with the filesystem's limits, so with different limits an object could be neither copied nor rewritten. The planner now plans the copies of the appended object, for one that resolve() did not read into append_data, which is how both files call it. The planner test that covered the early return was removed. What remains is covered by the files' append tests.
| # The first failure of a request, if any, also of one | ||
| # whose callback has not run yet, before the error of the | ||
| # caller. | ||
| self._consume_finished() |
There was a problem hiding this comment.
Independent follow-up review (Codex), third round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d77-7211-76e1-bba0-5e12a1dc3a06. Static review of a detached snapshot of 8695cc58 (merge base e0ab4875), limited to that repair and what it affects. Result: FINDINGS (6). Repairs in 91dd6bd9.
4. Valid; repaired. A direct commit without a reservation still let the body's ValueError win over a failed part whose callback had not run. Every commit now consumes finished requests and raises the failure before it rejects a body. test_rejected_body_raises_a_failed_part runs with and without a reservation, and both fail without the consumption.
| PutObject request of ``body``; None or empty bytes send no body of | ||
| their own, as with :meth:`S3Core.put_object`, which writes an empty | ||
| object unless the parameters of the PutObject request give ``Body``: | ||
| the prepared parameters of ``put_object``, if any, or else |
There was a problem hiding this comment.
Independent follow-up review (Codex), third round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d77-7211-76e1-bba0-5e12a1dc3a06. Static review of a detached snapshot of 8695cc58 (merge base e0ab4875), limited to that repair and what it affects. Result: FINDINGS (6). Repairs in 91dd6bd9.
5, 6. Valid as documentation; repaired. The docs and this docstring now say:
- the checksums of a
FULL_OBJECTupload reach its completion; - a PutObject without a body takes
Bodyfrom its effective parameters: the preparedput_objectparameters if any, else the session'srequest_kwargs, then the core's.
| """ | ||
| path = S3Path(self.bucket, self.key) | ||
| try: | ||
| # Evicts again after the cleanup, for an entry cached meanwhile. |
There was a problem hiding this comment.
Independent follow-up review (Codex), third round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d77-7211-76e1-bba0-5e12a1dc3a06. Static review of a detached snapshot of 8695cc58 (merge base e0ab4875), limited to that repair and what it affects. Result: FINDINGS (6). Repairs in 91dd6bd9.
2. Rejected. It needs a lookup that repopulates a sibling during the abort and an interrupt of that abort, two events in one failed commit. The interrupt guarantee covers one interrupt per operation. Without the interrupt, the outer eviction covers the concurrent lookup; without the concurrent lookup, the inner eviction covers the interrupt (test_missing_bucket_of_a_failed_commit_is_evicted).
3. Rejected. It needs an interrupt between the reservation and the handler of target.commit(). Only the async file reserves a commit, and its _complete_upload runs in a thread of the filesystem's pool, which interrupts do not reach (they reach the main thread only).
After the repairs:
just test offline: 4963 passed, 6 skipped, on91dd6bd9.- The parametrized test was seen to fail with its behavior removed.
- The differential check on
91dd6bd9(basee0ab4875) shows only the three intended differences.
iter_append_copies() took the core whose maximum part size decided whether the appended object is copied whole, apart from the session that plans the ranges; with different limits, every range was copied whole. It now takes only the session, and copies the object whole when the session plans it as one part. A test covers the boundary at the maximum part size. The docs say that prepared parameters of put_object replace the session's request_kwargs also as an empty mapping. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| size = self.appended.get("size", 0) if self.appended is not None else 0 | ||
| parts = list(session.iter_copy_parts(size)) | ||
| # An object planned as one part is copied whole. | ||
| whole_source = len(parts) == 1 |
There was a problem hiding this comment.
Independent follow-up review (Codex), fourth round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d89-d76a-7ef3-85e0-e09fa499af8f. Static review of a detached snapshot of 91dd6bd9 (merge base e0ab4875), limited to that repair and what it affects. Result: FINDINGS (4). Repairs in ba0c127e.
1. Valid for the public planner; repaired. iter_append_copies() took a core whose maximum part size decided whole copies, apart from the session that plans the ranges. With different limits, every range was copied whole. It now takes only the session and copies the object whole when the session plans one part, which is the master rule (size <= MULTIPART_UPLOAD_MAX_PART_SIZE) for the files' shared core.
3. Valid; repaired. test_iter_append_copies covers the boundary again: at the minimum and at exactly the maximum part size, the object is copied whole; one byte more, it is copied in contiguous ranges that cover it. It fails with the boundary moved.
| their own, as with :meth:`S3Core.put_object`, which writes an empty | ||
| object unless the parameters of the PutObject request give ``Body``: | ||
| the prepared parameters of ``put_object``, which, if the session has | ||
| them, also as an empty mapping, replace ``request_kwargs``, and the |
There was a problem hiding this comment.
Independent follow-up review (Codex), fourth round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d89-d76a-7ef3-85e0-e09fa499af8f. Static review of a detached snapshot of 91dd6bd9 (merge base e0ab4875), limited to that repair and what it affects. Result: FINDINGS (4). Repairs in ba0c127e.
4. Valid as documentation; repaired. This docstring and the guide say that the session's put_object entry replaces request_kwargs even as an empty mapping, while the core's request_kwargs still apply.
| # The first failure of a request, if any, also of one | ||
| # whose callback has not run yet, before the error of the | ||
| # caller. | ||
| self._consume_finished() |
There was a problem hiding this comment.
Independent follow-up review (Codex), fourth round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d89-d76a-7ef3-85e0-e09fa499af8f. Static review of a detached snapshot of 91dd6bd9 (merge base e0ab4875), limited to that repair and what it affects. Result: FINDINGS (4). Repairs in ba0c127e.
2. Rejected. It needs an interrupted part callback, after which a commit raises the consumed failure without resolving pending ready() waiters. Master's _submit() and check() raise a consumed failure without publishing in the same way, and a later operation that publishes resolves them. The sync wait_ready() also rechecks every second. This is the pre-existing pattern for an interrupted callback, not a change of this PR.
After the repairs: just test offline gave 4966 passed and 6 skipped on ba0c127e. The differential check on ba0c127e (base e0ab4875) shows only the three intended differences.
The async append test already requires a whole copy at the minimum part size; the target test keeps the boundary at the maximum. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| max_workers=1, | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize(("size", "whole"), [(8, True), (9, False)]) |
There was a problem hiding this comment.
Independent follow-up review (Codex), fifth round, relayed. Codex CLI codex exec -s read-only, model gpt-6.2-astra, session 01a22d9a-98a0-7c00-bf69-71b9cab18754. Static review of a detached snapshot of ba0c127e (merge base e0ab4875), limited to that repair. It found no runtime defect in the repairs; the sync and async append paths, part numbering, and the commit documentation were checked. Result: FINDINGS (1, low).
1. Valid; repaired in c8ca53f2. The (4, True) case duplicated the async append test, which already requires a whole copy at the minimum part size. It is dropped; the cases at exactly the maximum and one byte above it remain. This test-only change applies the reviewer's own suggestion, so it gets no further review round.
WHAT
Publishes the write session and has
S3FileandAioS3StreamedFileshare their write logic throughS3FileTarget, as decided on #1455 (Decided (2026-11-29) and decision 2 of 08:44).Public API in
pyathena.filesystem.s3_writerS3WriteSession, the former_S3WriteSession. Its public methods arestate,multipart,upload,completed_parts,commit_attempts,start,iter_parts,iter_copy_parts,ready,wait_ready,check,submit_part,submit_copy,commit,reserve_commit,withdraw_commit,begin_abort,abort, andabort_or_log.S3WriteState, the former_S3WriteState. Its values are now plain names.COMMITTINGandCOMMITTEDreplaceCOMPLETINGandCOMPLETED. The error messages describe the states separately and read as before.S3WriteStateError.S3MultipartStateErrorrefers to the same class under its former name.Session changes
commit(body=None)is now the only commit entry point. It sends PutObject if the write started no multipart upload, and otherwise completes the upload. It chooses from the session's state, not frombody is None. It rejects a non-emptybodyfor a multipart upload withValueError, after the state checks and after raising a failed part. A repeated commit after a success returns the recorded result.COMMITTING, byreserve_commit()orcommit(), now resolves pendingready()futures, which master left until a later change.put()andcomplete()are now private (_put_object,_complete_parts).begin_complete()andwithdraw_complete()are renamed toreserve_commit()andwithdraw_commit().settled()is removed. No caller in the source used it, so it went under the condition of the decision.abort_or_log()aborts and logs a failed abort with the upload ID instead of raising it. On an interrupt, it logs the upload ID and re-raises. It replaces the abort-failure log sites ofS3File,AioS3StreamedFile, and the sync multipart copy.request_kwargsfor that operation. No current caller passes both.Shared file drivers
S3FileTargetis now kept on the file in both modes. It builds the session on the file's core (write_session) and holds:iter_append_copies)commit). It takes the file's cleanup for a failed commit, evicts the error's stale entries before and after that cleanup, and invalidates the path last, as both files did on master.abort)The blocking and awaited waits, the fsspec buffering, the failure handling of each file, and the finalizers stay on each class. There is no base class or mixin. The stop-checked range read and the range splitter were already shared (
read_range,read_ranges).Decided during implementation (consulted with Codex only, because Fable 5.2 was at its weekly limit until 12/5):
settled(). Its one test waits onready()withmax_workers=1instead.commit_attempts, so that a custom driver can invalidate a cache after a commit whose response was lost._logging_abort_failure()context manager, shared withabort_or_log(), wraps the file's eviction. Aio stays without that eviction.The final flush stays with the file (#1518). The cache invalidation after a possibly sent commit (#1523, #1536, #1562, #1579), the upload-listing invalidation (#1527), and the final-flush ownership of #1546 (#1593) are unchanged.
Not in this PR: the recheck-interval constant and the executor rename from the earlier comments, which the 14:55 decision does not include.
Closes #1455
WHY
#1340 proposal 6: the session that both files and the sync multipart copy use was private, and its commit was split across
put()andcomplete(). The two files repeated the commit dispatch, the append copy plan, and the abort, and each of them and the copy logged an abort failure itself.TEST
Tested at
c8ca53f2, based one0ab4875.just lint: passed (ruff, mypy, license headers).just test offline: 4966 passed, 6 skipped, atba0c127e. The last commit removes one test parameter; that test was rerun.just docs lint: passed.sphinx-build docson the working tree raises no warning for the changed pages.just docs buildbuilds the git refs, not the working tree.test_s3_writer.py:commit()follows the state (PutObject withNone, empty, or data; completion), and a repeat returns the same result.commit()rejects a body for a multipart upload, but only once the state allows the commit: a committed write returns its result, and a discarded one raises its state error.abort_or_log()with a failure and with an interrupt.request_kwargs, checked with a botocoreStubber. This test fails with the change reverted.Differential check (offline; not in the repository): a fake S3 answers at botocore's
before-callevent, and sockets are blocked. Each case has a fresh filesystem, the same seed, and an empty request log. It records the result, the exception, the requests (bodies as size and digest), the pyathena log records, the objects and uploads left, and a cache probe after the write (the requests thatexists()andls()send).The matrix has 2966 cases:
S3Fileand aioAioS3StreamedFile.wb,xb, andab, with no object, a small object rewritten from the buffer, an object copied whole, or an object copied in ranges.close,commitwhile open,flush(force=True)thencommit, repeatedcommit,discard,withexit on an error, andcommitaftercloseclose,flush(force=True)thenclose, repeatedclose, andasync withexit on an errordiscard/commitafter each failureRace-prone cases (an abort while a part may run) use
max_workers=1and wait forready()first. Failing copies use one worker. Part request runs are compared in part-number order.Two runs on each base were identical:
4f320bf0,06d48025,251a43ff, ande0ab4875. The branch was compared on each base and again after every review repair, last atba0c127eone0ab4875. Each comparison gave the same result:S3MultipartStateError→S3WriteStateError(same class, same message)pyathena.filesystem.s3/s3_asyncloggers topyathena.filesystem.s3_writer(same text)abort_or_log()Interrupted the abort of multipart upload <id> to <uri>.abort_or_log()logs an interrupt with the upload ID, as the copy did🤖 Generated with Claude Code