Skip to content

GH-36523: [C++] Fix TSan-detected lock ordering issues in S3 - #36536

Merged
pitrou merged 4 commits into
apache:mainfrom
pitrou:gh36523-tsan-s3
Jul 10, 2023
Merged

pitrou merged 4 commits into
apache:mainfrom
pitrou:gh36523-tsan-s3

Conversation

@pitrou

@pitrou pitrou commented Jul 7, 2023 •

Copy link
Copy Markdown
Member

It is counter-intuitive, but lock ordering issues can happen even with a shared mutex locked in shared mode.
The reason is that locking again in shared mode can block while there are threads waiting to take the lock in exclusive mode. For this reason, we must avoid to keep the S3ClientLock taken before is it taken again.

@pitrou
pitrou requested a review from westonpace July 7, 2023 09:04
@pitrou

pitrou commented Jul 7, 2023

Copy link
Copy Markdown
Member Author

Can you run this with your stress test @westonpace ?

@github-actions

github-actions Bot commented Jul 7, 2023

Copy link
Copy Markdown

⚠️ GitHub issue #36523 has been automatically assigned in GitHub to PR creator.

@github-actions

github-actions Bot commented Jul 7, 2023

Copy link
Copy Markdown

⚠️ GitHub issue #36523 has no components, please add labels for components.

Comment thread cpp/src/arrow/filesystem/s3fs.cc Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I'm not sure if calling std::move here is enough to destroy client_lock at the end of this statement. @bkietz Can you advise?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move will not destroy this lock (in general, it's useful to think of std::move as doing nothing except flagging a reference as legal to move construct from). What you want here is

Suggested change
auto outcome = std::move(client_lock)->HeadObject(req);
S3Model::HeadObjectOutcome outcome;
{
ARROW_ASSIGN_OR_RAISE(auto client_lock, holder_->Lock());
outcome = std::move(client_lock)->HeadObject(req);
} // client lock goes out of scope here

or maybe

Suggested change
auto outcome = std::move(client_lock)->HeadObject(req);
ARROW_ASSIGN_OR_RAISE(auto outcome, holder_->Lock().Map([&](auto client_lock) {
return client_lock->HeadObject(req);
}));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you might even prefer

template <auto MemberPtr, typename... Args>
auto S3ClientHolder::LockThenCall(Args&&... args) {
  return Lock().Map([&](S3ClientLock client_lock) {
    return (client_lock.get()->*MemberPtr)(std::forward<Args>(args)...);
  });
}

used like

Suggested change
auto outcome = std::move(client_lock)->HeadObject(req);
ARROW_ASSIGN_OR_RAISE(auto outcome, holder_->LockThenCall<&S3Client::HeadObject>(req));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bkietz !

@pitrou pitrou Jul 8, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I opted for a more manual approach because mixing arrow::Result and Aws::Outcome is not really convenient:
https://github.com/apache/arrow/pull/36536/files#diff-27b767cd06b879c40106ea98d21d3b12f13ec6e9f9aa49218d10583c5c1899c4R725-R726

  S3ClientLock Move() { return std::move(*this); }
      auto deferred = [owned_buffer, holder = holder_, req = std::move(req),
                       state = upload_state_,
                       part_number = part_number_]() mutable -> Status {
        ARROW_ASSIGN_OR_RAISE(auto client_lock, holder->Lock());
        auto outcome = client_lock.Move()->UploadPart(req);
        HandleUploadOutcome(state, part_number, req, outcome);
        return Status::OK();
      };

@pitrou

pitrou commented Jul 7, 2023

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g cpp

@github-actions

This comment was marked as outdated.

@pitrou

pitrou commented Jul 7, 2023

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g cpp

@github-actions

This comment was marked as outdated.

@pitrou

pitrou commented Jul 7, 2023

Copy link
Copy Markdown
Member Author

@raulcd Ideally we would have this PR in the release, because it fixes a late regression (we're not sure the regression can happen concretely, though).

@pitrou

pitrou commented Jul 7, 2023

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g python -g wheel

@github-actions

This comment was marked as outdated.

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll wait for this to merge and then update #35440 . The approach there will need to change as it currently holds the client lock for the duration of the request and since DeleteDirContents calls GetFileInfo this will be a problem. However, it should be easy enough to change it back to locking on the spot when needed.

@pitrou
pitrou force-pushed the gh36523-tsan-s3 branch from 084180f to 85a0144 Compare July 8, 2023 14:31
@pitrou
pitrou marked this pull request as ready for review July 8, 2023 14:31
@pitrou

pitrou commented Jul 8, 2023

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g python -g wheel -g cpp

@github-actions

github-actions Bot commented Jul 8, 2023

Copy link
Copy Markdown

Revision: 85a0144

Submitted crossbow builds: ursacomputing/crossbow @ actions-1735f0c98a

Task Status
test-alpine-linux-cpp Github Actions
test-build-cpp-fuzz Github Actions
test-conda-cpp Github Actions
test-conda-cpp-valgrind Azure
test-conda-python-3.10 Github Actions
test-conda-python-3.10-hdfs-2.9.2 Github Actions
test-conda-python-3.10-hdfs-3.2.1 Github Actions
test-conda-python-3.10-pandas-latest Github Actions
test-conda-python-3.10-pandas-nightly Github Actions
test-conda-python-3.10-spark-master Github Actions
test-conda-python-3.10-substrait Github Actions
test-conda-python-3.11 Github Actions
test-conda-python-3.11-dask-latest Github Actions
test-conda-python-3.11-dask-upstream_devel Github Actions
test-conda-python-3.11-hypothesis Github Actions
test-conda-python-3.11-pandas-upstream_devel Github Actions
test-conda-python-3.8 Github Actions
test-conda-python-3.8-pandas-1.0 Github Actions
test-conda-python-3.8-spark-v3.1.2 Github Actions
test-conda-python-3.9 Github Actions
test-conda-python-3.9-pandas-latest Github Actions
test-conda-python-3.9-spark-v3.2.0 Github Actions
test-cuda-cpp Github Actions
test-cuda-python Github Actions
test-debian-11-cpp-amd64 Github Actions
test-debian-11-cpp-i386 Github Actions
test-debian-11-python-3 Azure
test-fedora-35-cpp Github Actions
test-fedora-35-python-3 Azure
test-ubuntu-20.04-cpp Github Actions
test-ubuntu-20.04-cpp-bundled Github Actions
test-ubuntu-20.04-cpp-minimal-with-formats Github Actions
test-ubuntu-20.04-cpp-thread-sanitizer Github Actions
test-ubuntu-20.04-python-3 Azure
test-ubuntu-22.04-cpp Github Actions
test-ubuntu-22.04-cpp-20 Github Actions
test-ubuntu-22.04-python-3 Github Actions
wheel-clean Github Actions
wheel-macos-big-sur-cp310-arm64 Github Actions
wheel-macos-big-sur-cp311-arm64 Github Actions
wheel-macos-big-sur-cp38-arm64 Github Actions
wheel-macos-big-sur-cp39-arm64 Github Actions
wheel-macos-mojave-cp310-amd64 Github Actions
wheel-macos-mojave-cp311-amd64 Github Actions
wheel-macos-mojave-cp38-amd64 Github Actions
wheel-macos-mojave-cp39-amd64 Github Actions
wheel-manylinux-2-28-cp310-amd64 Github Actions
wheel-manylinux-2-28-cp310-arm64 Github Actions
wheel-manylinux-2-28-cp311-amd64 Github Actions
wheel-manylinux-2-28-cp311-arm64 Github Actions
wheel-manylinux-2-28-cp38-amd64 Github Actions
wheel-manylinux-2-28-cp38-arm64 Github Actions
wheel-manylinux-2-28-cp39-amd64 Github Actions
wheel-manylinux-2-28-cp39-arm64 Github Actions
wheel-manylinux-2014-cp310-amd64 Github Actions
wheel-manylinux-2014-cp310-arm64 Github Actions
wheel-manylinux-2014-cp311-amd64 Github Actions
wheel-manylinux-2014-cp311-arm64 Github Actions
wheel-manylinux-2014-cp38-amd64 Github Actions
wheel-manylinux-2014-cp38-arm64 Github Actions
wheel-manylinux-2014-cp39-amd64 Github Actions
wheel-manylinux-2014-cp39-arm64 Github Actions
wheel-windows-cp310-amd64 Github Actions
wheel-windows-cp311-amd64 Github Actions
wheel-windows-cp38-amd64 Github Actions
wheel-windows-cp39-amd64 Github Actions

@github-actions

github-actions Bot commented Jul 8, 2023

Copy link
Copy Markdown

⚠️ GitHub issue #36523 has no components, please add labels for components.

@pitrou

pitrou commented Jul 8, 2023

Copy link
Copy Markdown
Member Author

@westonpace Are you available quickly for review?

@kou kou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

(I don't know why S3ClinetLock::Move() works but LGTM if it works as we expect.)

@pitrou
pitrou merged commit 8eacd8c into apache:main Jul 10, 2023
@pitrou
pitrou deleted the gh36523-tsan-s3 branch July 10, 2023 07:42
@westonpace

westonpace commented Jul 10, 2023 •

Copy link
Copy Markdown
Member

I'll rebase this into #35440 now and run my stress tests

@westonpace

Copy link
Copy Markdown
Member

+1, after running my test multiple times with and without CPU stress I no longer see any TSAN errors. I've included one minor comment tweak in #35440

@pitrou

pitrou commented Jul 10, 2023

Copy link
Copy Markdown
Member Author

Great, thank you @westonpace :-)

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 8eacd8c.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about possible false positives for unstable benchmarks that are known to sometimes produce them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] TSAN error in s3 (maybe false positive?)

4 participants