Skip to content

Commit

Permalink
test: fix data races in FakeStream. (envoyproxy#7929)
Browse files Browse the repository at this point in the history
Fixes envoyproxy#7927.

Risk Level: low (test only)

Signed-off-by: Piotr Sikora <[email protected]>
  • Loading branch information
PiotrSikora authored and lizan committed Aug 19, 2019
1 parent 6858df7 commit 869981f
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions test/integration/fake_upstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,30 @@ void FakeStream::decodeMetadata(Http::MetadataMapPtr&& metadata_map_ptr) {
}

void FakeStream::encode100ContinueHeaders(const Http::HeaderMapImpl& headers) {
std::shared_ptr<Http::HeaderMapImpl> headers_copy(
// TSan complains about thread-safety of std::shared_ptr when linked against libc++.
// See: https://github.com/envoyproxy/envoy/pull/7929
std::unique_ptr<Http::HeaderMapImpl> headers_copy(
new Http::HeaderMapImpl(static_cast<const Http::HeaderMap&>(headers)));
parent_.connection().dispatcher().post(
[this, headers_copy]() -> void { encoder_.encode100ContinueHeaders(*headers_copy); });
parent_.connection().dispatcher().post([this, headers = headers_copy.release()]() -> void {
encoder_.encode100ContinueHeaders(*headers);
delete headers;
});
}

void FakeStream::encodeHeaders(const Http::HeaderMapImpl& headers, bool end_stream) {
std::shared_ptr<Http::HeaderMapImpl> headers_copy(
// TSan complains about thread-safety of std::shared_ptr when linked against libc++.
// See: https://github.com/envoyproxy/envoy/pull/7929
std::unique_ptr<Http::HeaderMapImpl> headers_copy(
new Http::HeaderMapImpl(static_cast<const Http::HeaderMap&>(headers)));
if (add_served_by_header_) {
headers_copy->addCopy(Http::LowerCaseString("x-served-by"),
parent_.connection().localAddress()->asString());
}
parent_.connection().dispatcher().post([this, headers_copy, end_stream]() -> void {
encoder_.encodeHeaders(*headers_copy, end_stream);
});
parent_.connection().dispatcher().post(
[this, headers = headers_copy.release(), end_stream]() -> void {
encoder_.encodeHeaders(*headers, end_stream);
delete headers;
});
}

void FakeStream::encodeData(absl::string_view data, bool end_stream) {
Expand All @@ -106,16 +114,24 @@ void FakeStream::encodeData(uint64_t size, bool end_stream) {
}

void FakeStream::encodeData(Buffer::Instance& data, bool end_stream) {
std::shared_ptr<Buffer::Instance> data_copy(new Buffer::OwnedImpl(data));
parent_.connection().dispatcher().post(
[this, data_copy, end_stream]() -> void { encoder_.encodeData(*data_copy, end_stream); });
// TSan complains about thread-safety of std::shared_ptr when linked against libc++.
// See: https://github.com/envoyproxy/envoy/pull/7929
std::unique_ptr<Buffer::Instance> data_copy(new Buffer::OwnedImpl(data));
parent_.connection().dispatcher().post([this, data = data_copy.release(), end_stream]() -> void {
encoder_.encodeData(*data, end_stream);
delete data;
});
}

void FakeStream::encodeTrailers(const Http::HeaderMapImpl& trailers) {
std::shared_ptr<Http::HeaderMapImpl> trailers_copy(
// TSan complains about thread-safety of std::shared_ptr when linked against libc++.
// See: https://github.com/envoyproxy/envoy/pull/7929
std::unique_ptr<Http::HeaderMapImpl> trailers_copy(
new Http::HeaderMapImpl(static_cast<const Http::HeaderMap&>(trailers)));
parent_.connection().dispatcher().post(
[this, trailers_copy]() -> void { encoder_.encodeTrailers(*trailers_copy); });
parent_.connection().dispatcher().post([this, trailers = trailers_copy.release()]() -> void {
encoder_.encodeTrailers(*trailers);
delete trailers;
});
}

void FakeStream::encodeResetStream() {
Expand Down

0 comments on commit 869981f

Please sign in to comment.