From ab1d3b1d41d0a7ee50af0defb21a73d886f3106a Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 29 May 2024 17:41:14 +0200 Subject: [PATCH] GH-41862: [C++][S3] Try out possible fix number 2 --- cpp/src/arrow/filesystem/s3fs.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/filesystem/s3fs.cc b/cpp/src/arrow/filesystem/s3fs.cc index c456be2d0d3cd..9862845ff8b1c 100644 --- a/cpp/src/arrow/filesystem/s3fs.cc +++ b/cpp/src/arrow/filesystem/s3fs.cc @@ -1606,6 +1606,10 @@ class ObjectOutputStream final : public io::OutputStream { io::internal::CloseFromDestructor(this); } + std::shared_ptr Self() { + return std::dynamic_pointer_cast(shared_from_this()); + } + Status Init() { ARROW_ASSIGN_OR_RAISE(auto client_lock, holder_->Lock()); @@ -1724,9 +1728,9 @@ class ObjectOutputStream final : public io::OutputStream { RETURN_NOT_OK(EnsureReadyToFlushFromClose()); - auto self = std::dynamic_pointer_cast(shared_from_this()); // Wait for in-progress uploads to finish (if async writes are enabled) - return FlushAsync().Then([self]() { return self->FinishPartUploadAfterFlush(); }); + return FlushAsync().Then( + [self = Self()]() { return self->FinishPartUploadAfterFlush(); }); } bool closed() const override { return closed_; } @@ -1892,6 +1896,9 @@ class ObjectOutputStream final : public io::OutputStream { } // Notify completion if (--state->parts_in_progress == 0) { + // GH-41862: avoid potential deadlock if the Future's callback is called + // with the mutex taken. + lock.unlock(); state->pending_parts_completed.MarkFinished(state->status); } }