Xuanwo commented on code in PR #9059:
URL: https://github.com/apache/opendal/pull/9059#discussion_r4696148026


##########
integrations/object_store/src/store.rs:
##########
@@ -1261,37 +1310,83 @@ impl MultipartUpload for OpendalMultipartUpload {
     fn put_part(&mut self, data: PutPayload) -> UploadPart {
         let writer = self.writer.clone();
         let location = self.location.clone();
-
-        // Generate next notify which will be notified only after the current
-        // part is written successfully.
-        let (tx, rx) = oneshot::channel();
-        // Fetch the notify for current part to wait for it to be written.
-        let last_rx = std::mem::replace(&mut self.next_notify, rx);
-        let mut guard = PutPartGuard::register(self.state.clone(), tx);
+        let state = self.state.clone();
+        let (mut guard, outcome) = match 
PutPartGuard::register(self.state.clone(), data) {
+            Ok(registered) => registered,
+            // The upload already failed: there is no part to drive, so the
+            // returned future reports the rejection at once.
+            Err(err) => return async move { Err::<(), _>(err) 
}.into_send().boxed(),
+        };
 
         async move {
-            // Wait for the previous part to be written. A predecessor that
-            // failed or was dropped disconnects the channel.
-            if last_rx.await.is_err() {
-                return Err(format_multipart_error(
-                    &location,
-                    "previous multipart part failed or was cancelled",
-                ));
+            // Return this part's recorded outcome before the poison check:
+            // a part whose own write failed must report its original error,
+            // not the generic poison error.
+            if let Some(outcome) = outcome.lock().unwrap().take() {
+                if outcome.is_ok() {
+                    guard.succeed();
+                }
+                return outcome;
             }
+
             guard.ensure_active(&location)?;

Review Comment:
   Checking `outcome` and `MultipartUploadState.failed` under separate mutexes 
leaves a TOCTOU window where a part can miss its recorded outcome and return 
the generic `"multipart upload has failed"` error instead.
   
   If another task drains the queue between taking `outcome` (seeing `None`) 
and calling `guard.ensure_active`, it can finish part A (`Ok(())`), fail part B 
with its real write error, and poison `state`. Part A then returns the generic 
upload failure instead of `Ok(())`, and part B loses its original write error.
   
   To close this race cleanly, completed part outcomes could be stored inside 
`MultipartUploadState` (for example via a monotonic part ID and `outcomes: 
HashMap<usize, object_store::Result<()>>`) instead of per-part 
`Arc<Mutex<...>>`. A single `state`-locked helper checking 
`outcomes.remove(&id)` before `state.failed`—used before `writer.lock().await` 
and at the top of the drain loop—together with recording 
`outcomes.insert(entry.id, Err(err))` and calling `state.poison()` under the 
same lock acquisition, would ensure `failed == true` is never observed before a 
completed or failed part's outcome is visible. (`poison()` would then only need 
to set `failed = true` and clear `queued` to drop buffered payloads.)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to