This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new d255ad850a fix(core): abort failed writes and clear zero-byte 
copy_from state (#8934)
d255ad850a is described below

commit d255ad850ae00141a49218ff600097f3d7c7da8e
Author: Xuanwo <[email protected]>
AuthorDate: Tue Nov 24 03:55:35 2026 +0800

    fix(core): abort failed writes and clear zero-byte copy_from state (#8934)
---
 core/core/src/types/context/write.rs     | 227 ++++++++++++++++++++++++++++++-
 core/core/src/types/operator/operator.rs | 204 ++++++++++++++++++++++++++-
 2 files changed, 427 insertions(+), 4 deletions(-)

diff --git a/core/core/src/types/context/write.rs 
b/core/core/src/types/context/write.rs
index 660eb1fd2c..5a9b8814d7 100644
--- a/core/core/src/types/context/write.rs
+++ b/core/core/src/types/context/write.rs
@@ -123,6 +123,7 @@ struct PendingCopy {
     options: OpReader,
     range: BytesRange,
     limit: Option<u64>,
+    start: u64,
     end: u64,
     /// The next source offset to copy. It advances only after the destination
     /// accepts the corresponding bytes.
@@ -130,6 +131,10 @@ struct PendingCopy {
 }
 
 impl PendingCopy {
+    fn has_accepted_bytes(&self) -> bool {
+        self.offset > self.start
+    }
+
     fn matches(&self, path: &str, args: &OpRead, range: BytesRange, limit: 
Option<u64>) -> bool {
         self.path == path && self.range == range && self.limit == limit && 
self.args.same_read(args)
     }
@@ -215,8 +220,9 @@ impl WriteState {
         Ok(())
     }
 
-    fn check_writable(&self) -> Result<()> {
+    fn check_writable(&mut self) -> Result<()> {
         self.check_not_aborted()?;
+        self.pending_copy.take_if(|copy| !copy.has_accepted_bytes());
         if self.pending_copy.is_some() {
             return Err(Error::new(
                 ErrorKind::Unexpected,
@@ -349,6 +355,7 @@ impl WriteState {
         const MAX_COPY_FROM_SIZE: u64 = 5 * 1024 * 1024 * 1024;
 
         self.check_not_aborted()?;
+        self.pending_copy.take_if(|copy| !copy.has_accepted_bytes());
 
         if let Some(copy) = &self.pending_copy {
             // Only the identical logical call can continue after the writer
@@ -452,12 +459,17 @@ impl WriteState {
                 options,
                 range,
                 limit,
+                start: absolute.start,
                 end,
                 offset: absolute.start,
             }));
         }
 
-        self.drive_copy().await
+        let result = self.drive_copy().await;
+        if result.is_err() {
+            self.pending_copy.take_if(|copy| !copy.has_accepted_bytes());
+        }
+        result
     }
 
     async fn drive_copy(&mut self) -> Result<()> {
@@ -647,6 +659,7 @@ mod tests {
         fail_on_copy: usize,
         pending_on_copy: usize,
         can_copy_from: bool,
+        read_with_if_match: bool,
         /// Report this content length from `stat` instead of the real one.
         stat_content_length: Option<u64>,
     }
@@ -658,6 +671,7 @@ mod tests {
                 fail_on_copy: usize::MAX,
                 pending_on_copy: usize::MAX,
                 can_copy_from: true,
+                read_with_if_match: false,
                 stat_content_length: None,
             }
         }
@@ -688,6 +702,7 @@ mod tests {
                 write_can_multi: true,
                 write_multi_min_size: Some(10),
                 write_can_copy_from: self.faults.can_copy_from,
+                read_with_if_match: self.faults.read_with_if_match,
                 ..self.inner.capability()
             }
         }
@@ -795,6 +810,12 @@ mod tests {
                 self.copy_pending.store(true, Ordering::SeqCst);
                 std::future::pending::<()>().await;
             }
+            if args.if_match().is_some() {
+                return Err(Error::new(
+                    ErrorKind::ConditionNotMatch,
+                    "source etag mismatch",
+                ));
+            }
 
             let reader = self.source.read(&self.ctx, path, args)?;
             let (_, bs) = oio::Read::read(&reader, range).await?;
@@ -1581,6 +1602,208 @@ mod tests {
         Ok(())
     }
 
+    /// A bounded-range `copy_from` that fails before accepting any bytes must
+    /// leave the writer usable for subsequent writes, other copies, and close.
+    #[tokio::test]
+    async fn test_copy_from_zero_bytes_error_keeps_writer_usable() -> 
Result<()> {
+        // Streaming path failing on a missing source with a non-zero offset.
+        let op = Operator::new(services::Memory::default())?;
+        op.write("other", "0123456789").await?;
+        let mut writer = op.writer("target-stream").await?;
+        writer.write("pre:").await?;
+
+        let err = writer
+            .copy_from("missing", 5..15)
+            .await
+            .expect_err("missing source must fail");
+        assert_eq!(err.kind(), ErrorKind::NotFound);
+
+        writer.write("mid:").await?;
+        writer.copy_from("other", 0..10).await?;
+        writer.close().await?;
+        assert_eq!(
+            op.read("target-stream").await?.to_vec(),
+            b"pre:mid:0123456789"
+        );
+
+        // Native copy path failing on missing source and ConditionNotMatch.
+        let (fault_op, _, _) = with_write_faults(
+            &op,
+            WriteFaults {
+                read_with_if_match: true,
+                ..Default::default()
+            },
+        );
+        let mut writer = 
fault_op.writer_with("target-native").chunk(10).await?;
+
+        let err = writer
+            .copy_from("missing", 0..10)
+            .await
+            .expect_err("missing source must fail");
+        assert_eq!(err.kind(), ErrorKind::NotFound);
+
+        let err = writer
+            .copy_from_options(
+                "other",
+                crate::options::ReadOptions {
+                    range: (0..10).into(),
+                    if_match: Some("\"stale-etag\"".to_string()),
+                    ..Default::default()
+                },
+            )
+            .await
+            .expect_err("mismatched etag must fail");
+        assert_eq!(err.kind(), ErrorKind::ConditionNotMatch);
+
+        writer.write("abcdefghij").await?;
+        writer.copy_from("other", 0..10).await?;
+        writer.close().await?;
+        assert_eq!(
+            op.read("target-native").await?.to_vec(),
+            b"abcdefghij0123456789"
+        );
+        Ok(())
+    }
+
+    /// Dropping a `copy_from` future before any bytes are accepted must leave
+    /// the writer usable for subsequent writes, other copies, and close.
+    #[tokio::test]
+    async fn 
test_copy_from_cancelled_before_accepting_bytes_keeps_writer_usable() -> 
Result<()> {
+        let op = Operator::new(services::Memory::default())?;
+        op.write("source", "0123456789abcdefghij").await?;
+        op.write("other", "abcdefghij").await?;
+
+        // Case 1: write() and close() succeed after cancellation at 0 
accepted bytes.
+        let (fault_op, _, copy_pending) = with_write_faults(
+            &op,
+            WriteFaults {
+                pending_on_copy: 1,
+                ..Default::default()
+            },
+        );
+        let mut writer = fault_op.writer_with("target-write").chunk(10).await?;
+        {
+            let mut fut = std::pin::pin!(writer.copy_from("source", 0..20));
+            for _ in 0..100 {
+                let _ = futures::poll!(&mut fut);
+                if copy_pending.load(Ordering::SeqCst) {
+                    break;
+                }
+                tokio::task::yield_now().await;
+            }
+            assert!(
+                copy_pending.load(Ordering::SeqCst),
+                "first native copy part must be pending"
+            );
+        }
+
+        writer.write("fresh-data").await?;
+        writer.close().await?;
+        assert_eq!(op.read("target-write").await?.to_vec(), b"fresh-data");
+
+        // Case 2: copy_from("other") and close() succeed after cancellation 
at 0 accepted bytes.
+        let (fault_op, _, copy_pending) = with_write_faults(
+            &op,
+            WriteFaults {
+                pending_on_copy: 1,
+                ..Default::default()
+            },
+        );
+        let mut writer = fault_op.writer_with("target-other").chunk(10).await?;
+        {
+            let mut fut = std::pin::pin!(writer.copy_from("source", 0..20));
+            for _ in 0..100 {
+                let _ = futures::poll!(&mut fut);
+                if copy_pending.load(Ordering::SeqCst) {
+                    break;
+                }
+                tokio::task::yield_now().await;
+            }
+            assert!(
+                copy_pending.load(Ordering::SeqCst),
+                "first native copy part must be pending"
+            );
+        }
+
+        writer.copy_from("other", 0..10).await?;
+        writer.close().await?;
+        assert_eq!(op.read("target-other").await?.to_vec(), b"abcdefghij");
+
+        // Case 3: close() directly succeeds after cancellation at 0 accepted 
bytes.
+        let (fault_op, _, copy_pending) = with_write_faults(
+            &op,
+            WriteFaults {
+                pending_on_copy: 1,
+                ..Default::default()
+            },
+        );
+        let mut writer = fault_op.writer_with("target-close").chunk(10).await?;
+        writer.write("0123456789").await?;
+        {
+            let mut fut = std::pin::pin!(writer.copy_from("source", 0..20));
+            for _ in 0..100 {
+                let _ = futures::poll!(&mut fut);
+                if copy_pending.load(Ordering::SeqCst) {
+                    break;
+                }
+                tokio::task::yield_now().await;
+            }
+            assert!(
+                copy_pending.load(Ordering::SeqCst),
+                "first native copy part must be pending"
+            );
+        }
+
+        writer.close().await?;
+        assert_eq!(op.read("target-close").await?.to_vec(), b"0123456789");
+        Ok(())
+    }
+
+    /// Once at least one byte of a `copy_from` call has been accepted, the
+    /// writer must still reject unrelated writes, other `copy_from` calls, and
+    /// `close` until the same `copy_from` is resumed or `abort` runs.
+    #[tokio::test]
+    async fn test_copy_from_partial_accept_still_requires_resume_or_abort() -> 
Result<()> {
+        let op = Operator::new(services::Memory::default())?;
+        op.write("source", "0123456789abcdefghij").await?;
+        op.write("other", "abcdefghij").await?;
+        let (fault_op, _, _) = with_write_faults(
+            &op,
+            WriteFaults {
+                fail_on_copy: 2,
+                ..Default::default()
+            },
+        );
+
+        let mut writer = fault_op.writer_with("target-abort").chunk(10).await?;
+        let err = writer
+            .copy_from("source", 0..20)
+            .await
+            .expect_err("second native copy part fails after first 10 bytes 
are accepted");
+        assert_eq!(err.message(), "injected copy failure");
+
+        let err = writer
+            .write("rejected")
+            .await
+            .expect_err("unrelated write must fail after partial copy");
+        assert!(err.is_persistent());
+
+        let err = writer
+            .copy_from("other", 0..10)
+            .await
+            .expect_err("different copy_from must fail after partial copy");
+        assert!(err.is_persistent());
+
+        let err = writer
+            .close()
+            .await
+            .expect_err("close must fail after partial copy");
+        assert!(err.is_persistent());
+
+        writer.abort().await?;
+        Ok(())
+    }
+
     #[tokio::test]
     async fn test_retry_exact_write_after_first_flush_failure() -> Result<()> {
         for (prefix, fail_on_write, expected) in [
diff --git a/core/core/src/types/operator/operator.rs 
b/core/core/src/types/operator/operator.rs
index a3f308f15a..53fdcd76c8 100644
--- a/core/core/src/types/operator/operator.rs
+++ b/core/core/src/types/operator/operator.rs
@@ -1019,8 +1019,17 @@ impl Operator {
         }
 
         let mut w = Self::writer_inner(ctx, srv, path, opts).await?;
-        w.write(bs).await?;
-        w.close().await
+        if let Err(err) = w.write(bs).await {
+            let _ = w.abort().await;
+            return Err(err);
+        }
+        match w.close().await {
+            Ok(meta) => Ok(meta),
+            Err(err) => {
+                let _ = w.abort().await;
+                Err(err)
+            }
+        }
     }
 
     /// Create a writer for the specified path.
@@ -3063,4 +3072,195 @@ mod tests {
         assert_eq!(buf.to_bytes(), "ta");
         Ok(())
     }
+
+    #[derive(Debug)]
+    struct AbortRecordingService {
+        fail_on_write: Option<usize>,
+        fail_on_close: bool,
+        fail_on_abort: bool,
+        write_calls: Arc<std::sync::atomic::AtomicUsize>,
+        abort_calls: Arc<std::sync::atomic::AtomicUsize>,
+    }
+
+    struct AbortRecordingWriter {
+        fail_on_write: Option<usize>,
+        fail_on_close: bool,
+        fail_on_abort: bool,
+        write_calls: Arc<std::sync::atomic::AtomicUsize>,
+        abort_calls: Arc<std::sync::atomic::AtomicUsize>,
+    }
+
+    impl oio::Write for AbortRecordingWriter {
+        async fn write(&mut self, _: Buffer) -> Result<()> {
+            let attempt = self
+                .write_calls
+                .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
+                + 1;
+            if self.fail_on_write == Some(attempt) {
+                return Err(Error::new(ErrorKind::Unexpected, "injected write 
failure"));
+            }
+            Ok(())
+        }
+
+        async fn close(&mut self) -> Result<Metadata> {
+            if self.fail_on_close {
+                return Err(Error::new(
+                    ErrorKind::ConditionNotMatch,
+                    "injected close failure",
+                ));
+            }
+            Ok(MetadataBuilder::unknown().build())
+        }
+
+        async fn abort(&mut self) -> Result<()> {
+            self.abort_calls
+                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+            if self.fail_on_abort {
+                return Err(Error::new(ErrorKind::Unexpected, "injected abort 
failure"));
+            }
+            Ok(())
+        }
+    }
+
+    impl Service for AbortRecordingService {
+        type Reader = ();
+        type Writer = AbortRecordingWriter;
+        type Lister = ();
+        type Deleter = ();
+        type Copier = ();
+        type Composer = ();
+
+        fn info(&self) -> ServiceInfo {
+            ServiceInfo::new("mock", "/", "abort-recording")
+        }
+
+        fn capability(&self) -> Capability {
+            Capability {
+                write: true,
+                write_can_multi: true,
+                write_can_empty: true,
+                ..Default::default()
+            }
+        }
+
+        async fn create_dir(
+            &self,
+            _: &OperationContext,
+            _: &str,
+            _: OpCreateDir,
+        ) -> Result<RpCreateDir> {
+            unreachable!()
+        }
+
+        async fn stat(&self, _: &OperationContext, _: &str, _: OpStat) -> 
Result<RpStat> {
+            unreachable!()
+        }
+
+        fn read(&self, _: &OperationContext, _: &str, _: OpRead) -> 
Result<Self::Reader> {
+            unreachable!()
+        }
+
+        fn write(&self, _: &OperationContext, _: &str, _: OpWrite) -> 
Result<Self::Writer> {
+            Ok(AbortRecordingWriter {
+                fail_on_write: self.fail_on_write,
+                fail_on_close: self.fail_on_close,
+                fail_on_abort: self.fail_on_abort,
+                write_calls: self.write_calls.clone(),
+                abort_calls: self.abort_calls.clone(),
+            })
+        }
+
+        fn delete(&self, _: &OperationContext) -> Result<Self::Deleter> {
+            unreachable!()
+        }
+
+        fn list(&self, _: &OperationContext, _: &str, _: OpList) -> 
Result<Self::Lister> {
+            unreachable!()
+        }
+
+        fn copy(&self, _: &OperationContext, _: &str, _: &str, _: OpCopy) -> 
Result<Self::Copier> {
+            unreachable!()
+        }
+
+        async fn rename(
+            &self,
+            _: &OperationContext,
+            _: &str,
+            _: &str,
+            _: OpRename,
+        ) -> Result<RpRename> {
+            unreachable!()
+        }
+
+        async fn presign(&self, _: &OperationContext, _: &str, _: OpPresign) 
-> Result<RpPresign> {
+            unreachable!()
+        }
+    }
+
+    fn abort_recording_operator(
+        fail_on_write: Option<usize>,
+        fail_on_close: bool,
+        fail_on_abort: bool,
+    ) -> (
+        Operator,
+        Arc<std::sync::atomic::AtomicUsize>,
+        Arc<std::sync::atomic::AtomicUsize>,
+    ) {
+        let write_calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+        let abort_calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+        let op = Operator::from_parts(
+            OperationContext::new(),
+            Arc::new(AbortRecordingService {
+                fail_on_write,
+                fail_on_close,
+                fail_on_abort,
+                write_calls: write_calls.clone(),
+                abort_calls: abort_calls.clone(),
+            }),
+        );
+        (op, write_calls, abort_calls)
+    }
+
+    #[tokio::test]
+    async fn test_write_aborts_on_write_failure() {
+        for fail_on_abort in [false, true] {
+            let (op, write_calls, abort_calls) =
+                abort_recording_operator(Some(2), false, fail_on_abort);
+
+            let err = op
+                .write_options(
+                    "target",
+                    "abcdefghi",
+                    options::WriteOptions {
+                        chunk: Some(4),
+                        ..Default::default()
+                    },
+                )
+                .await
+                .expect_err("second chunk write must fail");
+
+            assert_eq!(err.kind(), ErrorKind::Unexpected);
+            assert_eq!(err.message(), "injected write failure");
+            assert_eq!(write_calls.load(std::sync::atomic::Ordering::SeqCst), 
2);
+            assert_eq!(abort_calls.load(std::sync::atomic::Ordering::SeqCst), 
1);
+        }
+    }
+
+    #[tokio::test]
+    async fn test_write_aborts_on_close_failure() {
+        for fail_on_abort in [false, true] {
+            let (op, write_calls, abort_calls) =
+                abort_recording_operator(None, true, fail_on_abort);
+
+            let err = op
+                .write_options("target", "hello", 
options::WriteOptions::default())
+                .await
+                .expect_err("close must fail");
+
+            assert_eq!(err.kind(), ErrorKind::ConditionNotMatch);
+            assert_eq!(err.message(), "injected close failure");
+            assert_eq!(write_calls.load(std::sync::atomic::Ordering::SeqCst), 
1);
+            assert_eq!(abort_calls.load(std::sync::atomic::Ordering::SeqCst), 
1);
+        }
+    }
 }

Reply via email to