Skip to content

Commit

Permalink
fix corruption due to data race when concurrently calling
Browse files Browse the repository at this point in the history
  • Loading branch information
TangSiyang2001 committed Feb 18, 2024
1 parent 3486dec commit d353240
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cpp/src/arrow/filesystem/s3fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2898,7 +2898,8 @@ struct AwsInstance {
if (is_finalized_.load()) {
return Status::Invalid("Attempt to initialize S3 after it has been finalized");
}
if (!is_initialized_.exchange(true)) {
bool expected = false;
if (!is_initialized_.compare_exchange_weak(expected, true)) {
// Not already initialized
DoInitialize(options);
return true;
Expand Down
24 changes: 24 additions & 0 deletions cpp/src/arrow/filesystem/s3fs_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,30 @@ TEST_F(TestS3FS, FileSystemFromUri) {
AssertFileInfo(fs.get(), path, FileType::File, 8);
}

TEST_F(TestS3FS, ConcurrentFSInit) {
auto test_thread = [this]() {
std::stringstream ss;
ss << "s3://" << minio_->access_key() << ":" << minio_->secret_key()
<< "@bucket/somedir/subdir/subfile"
<< "?scheme=http&endpoint_override=" << UriEscape(minio_->connect_string());

std::string path;
ASSERT_OK(FileSystemFromUri(ss.str(), &path));
};

const int num_threads = 4;
auto threads = std::vector<std::thread>();
for (int i = 0; i < num_threads; i++) {
threads.emplace_back(test_thread);
}

for (auto& thread : threads) {
thread.join();
}

static_cast<void>(FinalizeS3());
}

TEST_F(TestS3FS, NoCreateDeleteBucket) {
// Create a bucket to try deleting
ASSERT_OK(fs_->CreateDir("test-no-delete"));
Expand Down

0 comments on commit d353240

Please sign in to comment.