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 6db8b691d6 fix(core): guard failed writer abort and BlockWriter
close/abort (#9011)
6db8b691d6 is described below
commit 6db8b691d6bdb816843b4427af53a16432543d07
Author: Xuanwo <[email protected]>
AuthorDate: Wed Nov 25 14:13:23 2026 +0800
fix(core): guard failed writer abort and BlockWriter close/abort (#9011)
* fix(core): enforce explicit lifecycle states in WriteState and oio writers
* test(core,hf3fs): update hf3fs abort-close expectation and add
Closing->Aborting tests
* fix(core): limit writer lifecycle guards to failed cleanup and block
finalization
---
core/core/src/raw/oio/write/block_write.rs | 77 ++++++++++++++++++++++++++---
core/core/src/types/context/write.rs | 79 +++++++++++++++++++++++++++++-
2 files changed, 148 insertions(+), 8 deletions(-)
diff --git a/core/core/src/raw/oio/write/block_write.rs
b/core/core/src/raw/oio/write/block_write.rs
index 66c019a6da..a4a9f00c55 100644
--- a/core/core/src/raw/oio/write/block_write.rs
+++ b/core/core/src/raw/oio/write/block_write.rs
@@ -113,6 +113,7 @@ pub struct BlockWriter<W: BlockWrite> {
/// be retried with the complete list.
scheduled_block_ids: Vec<Uuid>,
cache: Option<Buffer>,
+ abort_pending: bool,
tasks: ConcurrentTasks<WriteInput<W>, Uuid>,
}
@@ -126,6 +127,7 @@ impl<W: BlockWrite> BlockWriter<W> {
executor: executor.clone(),
scheduled_block_ids: Vec::new(),
cache: None,
+ abort_pending: false,
tasks: ConcurrentTasks::new(executor, concurrent, 0, |input| {
Box::pin(async move {
@@ -175,12 +177,17 @@ where
W: BlockWrite,
{
async fn write(&mut self, bs: Buffer) -> Result<()> {
- if self.scheduled_block_ids.is_empty() && self.cache.is_none() {
- self.fill_cache(bs);
- return Ok(());
- }
-
- let bytes = self.cache.clone().expect("pending write must exist");
+ let Some(bytes) = self.cache.clone() else {
+ if self.scheduled_block_ids.is_empty() {
+ self.fill_cache(bs);
+ return Ok(());
+ }
+ // The final block was scheduled or abort discarded the tail.
+ return Err(Error::new(
+ ErrorKind::Unexpected,
+ "block writer cannot accept more data while closing or
aborting",
+ ));
+ };
let block_id = Uuid::new_v4();
self.tasks
.execute(WriteInput {
@@ -199,6 +206,12 @@ where
}
async fn close(&mut self) -> Result<Metadata> {
+ if self.abort_pending {
+ return Err(Error::new(
+ ErrorKind::Unexpected,
+ "block writer cannot close while cleanup is pending",
+ ));
+ }
if self.scheduled_block_ids.is_empty() {
let (size, body) = match self.cache.clone() {
Some(cache) => (cache.len(), cache),
@@ -239,8 +252,10 @@ where
self.cache = None;
// Keep the IDs until the abort succeeds so a temporary failure can be
retried.
+ self.abort_pending = true;
self.w.abort_block(self.scheduled_block_ids.clone()).await?;
self.scheduled_block_ids.clear();
+ self.abort_pending = false;
Ok(())
}
}
@@ -410,6 +425,8 @@ mod tests {
aborted: Vec<Uuid>,
abort_calls: Vec<Vec<Uuid>>,
abort_failures: usize,
+ complete_calls: Vec<Vec<Uuid>>,
+ fail_first_complete: bool,
fail_write_on: Option<&'static [u8]>,
}
@@ -450,7 +467,14 @@ mod tests {
Ok(())
}
- async fn complete_block(&self, _: Vec<Uuid>) -> Result<Metadata> {
+ async fn complete_block(&self, block_ids: Vec<Uuid>) ->
Result<Metadata> {
+ let mut state = self.state.lock().expect("test state mutex
poisoned");
+ state.complete_calls.push(block_ids);
+ if std::mem::take(&mut state.fail_first_complete) {
+ return Err(
+ Error::new(ErrorKind::Unexpected, "injected complete
failure").set_temporary(),
+ );
+ }
Ok(MetadataBuilder::unknown().build())
}
@@ -576,6 +600,34 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn
test_block_writer_close_can_retry_after_temporary_commit_failure() ->
Result<()> {
+ let (inner, state) = RecordingWrite::new();
+ state.lock().unwrap().fail_first_complete = true;
+ let mut w = BlockWriter::new(crate::test_executor(), inner, 1);
+
+ w.write(Buffer::from("a")).await?;
+ w.write(Buffer::from("b")).await?;
+ let err = w.close().await.expect_err("first commit fails");
+ assert_eq!(err.message(), "injected complete failure");
+ assert!(err.is_temporary());
+ let err = w
+ .write(Buffer::from("extra"))
+ .await
+ .expect_err("final block was already scheduled");
+ assert_eq!(err.kind(), ErrorKind::Unexpected);
+ assert!(err.is_permanent());
+ w.close().await?;
+
+ let state = state.lock().unwrap();
+ assert_eq!(state.written.len(), 2);
+ assert_eq!(
+ state.complete_calls,
+ vec![state.written.clone(), state.written.clone()]
+ );
+ Ok(())
+ }
+
#[tokio::test]
async fn test_block_writer_abort_can_retry_after_temporary_abort_failure()
-> Result<()> {
let (inner, state) = RecordingWrite::new();
@@ -598,11 +650,22 @@ mod tests {
.abort_failures = 1;
let err = w.abort().await.expect_err("first abort should fail");
+ assert_eq!(err.message(), "injected abort block failure");
assert!(err.is_temporary());
+ let err = w
+ .write(Buffer::from("extra"))
+ .await
+ .expect_err("cleanup is still pending");
+ assert_eq!(err.kind(), ErrorKind::Unexpected);
+ assert!(err.is_permanent());
+ let err = w.close().await.expect_err("cleanup is still pending");
+ assert_eq!(err.kind(), ErrorKind::Unexpected);
+ assert!(err.is_permanent());
w.abort().await?;
let state = state.lock().expect("test state mutex poisoned");
+ assert!(state.complete_calls.is_empty());
assert_eq!(state.abort_calls, vec![written.clone(), written.clone()]);
assert_eq!(state.aborted, [written.clone(), written].concat());
Ok(())
diff --git a/core/core/src/types/context/write.rs
b/core/core/src/types/context/write.rs
index 38c3b3816b..7b9d1e232f 100644
--- a/core/core/src/types/context/write.rs
+++ b/core/core/src/types/context/write.rs
@@ -159,6 +159,8 @@ pub struct WriteState {
can_copy_from: bool,
/// Whether a logical write stopped after the inner writer accepted part
of its input.
requires_abort: bool,
+ /// Whether abort discarded local data but the underlying cleanup is still
pending.
+ abort_pending: bool,
/// A source copy that stopped after accepting part of its range.
pending_copy: Option<Box<PendingCopy>>,
buffer: oio::QueueBuf,
@@ -178,6 +180,7 @@ impl WriteState {
exact,
can_copy_from,
requires_abort: false,
+ abort_pending: false,
pending_copy: None,
buffer: oio::QueueBuf::new(),
})
@@ -193,6 +196,7 @@ impl WriteState {
exact,
can_copy_from: false,
requires_abort: false,
+ abort_pending: false,
pending_copy: None,
buffer: oio::QueueBuf::new(),
}
@@ -233,6 +237,12 @@ impl WriteState {
}
fn check_not_aborted(&self) -> Result<()> {
+ if self.abort_pending {
+ return Err(Error::new(
+ ErrorKind::Unexpected,
+ "writer cannot accept data or close while abort is pending",
+ ));
+ }
if self.requires_abort {
return Err(Error::new(
ErrorKind::Unexpected,
@@ -583,9 +593,13 @@ impl WriteState {
/// Abort the write process.
pub async fn abort(&mut self) -> Result<()> {
+ // A failed or canceled abort cannot restore the buffered bytes or
copy offset.
+ self.abort_pending = true;
self.buffer.clear();
self.pending_copy = None;
- self.w.abort().await
+ self.w.abort().await?;
+ self.abort_pending = false;
+ Ok(())
}
}
@@ -653,6 +667,7 @@ mod tests {
fail_on_write: usize,
fail_on_copy: usize,
pending_on_copy: usize,
+ fail_first_abort: bool,
can_copy_from: bool,
read_with_if_match: bool,
/// Report this content length from `stat` instead of the real one.
@@ -665,6 +680,7 @@ mod tests {
fail_on_write: usize::MAX,
fail_on_copy: usize::MAX,
pending_on_copy: usize::MAX,
+ fail_first_abort: false,
can_copy_from: true,
read_with_if_match: false,
stat_content_length: None,
@@ -822,6 +838,11 @@ mod tests {
}
async fn abort(&mut self) -> Result<()> {
+ if std::mem::take(&mut self.faults.fail_first_abort) {
+ return Err(
+ Error::new(ErrorKind::Unexpected, "injected abort
failure").set_temporary()
+ );
+ }
self.writer.abort().await
}
}
@@ -1049,6 +1070,62 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn test_writer_does_not_commit_partial_copy_after_failed_abort() ->
Result<()> {
+ let op = Operator::new(services::Memory::default())?;
+ op.write("source", "0123456789abcdefghijABCDEFGHIJ").await?;
+ let (fault_op, _, _) = with_write_faults(
+ &op,
+ WriteFaults {
+ fail_on_write: 2,
+ fail_first_abort: true,
+ can_copy_from: false,
+ ..Default::default()
+ },
+ );
+ let mut writer = fault_op.writer_with("target").chunk(10).await?;
+ let options = crate::options::ReadOptions {
+ range: (0..30).into(),
+ chunk: Some(5),
+ ..Default::default()
+ };
+
+ let err = writer
+ .copy_from_options("source", options.clone())
+ .await
+ .expect_err("second streamed chunk fails");
+ assert_eq!(err.message(), "injected flush failure");
+ assert!(
+ writer
+ .close()
+ .await
+ .expect_err("copy is incomplete")
+ .is_persistent()
+ );
+ let err = writer.abort().await.expect_err("first abort fails");
+ assert_eq!(err.message(), "injected abort failure");
+ assert!(err.is_temporary());
+
+ let close = writer.close().await;
+ let stored = op.read("target").await;
+ assert_eq!(
+ stored
+ .expect_err("close after failed abort committed a partial
copy")
+ .kind(),
+ ErrorKind::NotFound
+ );
+ assert_eq!(
+ close.expect_err("cleanup is still pending").kind(),
+ ErrorKind::Unexpected
+ );
+ assert!(writer.write("extra").await.is_err());
+ assert!(writer.copy_from_options("source", options).await.is_err());
+
+ writer.abort().await?;
+ assert!(!op.exists("target").await?);
+ Ok(())
+ }
+
/// A limited forward bounded range may end past EOF, so it streams instead
/// of requesting exact native copy parts.
#[tokio::test]