Skip to content

[C++] Make grpc::MessageBuilder::Release() hand over its slice - #9513

Open
aaronjmars wants to merge 1 commit into
google:masterfrom
aaronjmars:grpc-messagebuilder-release
Open

aaronjmars wants to merge 1 commit into
google:masterfrom
aaronjmars:grpc-messagebuilder-release

Conversation

@aaronjmars

Copy link
Copy Markdown

flatbuffers::grpc::MessageBuilder inherits FlatBufferBuilder::Release(). The DetachedBuffer it returns points into the grpc::Slice that is still held by the builder's SliceAllocator, so:

  • reusing the builder after Release() trips FLATBUFFERS_ASSERT(slice_.size() == 0) in SliceAllocator::allocate (or, with asserts off, replaces the slice the detached buffer is still reading from);
  • destroying or move-assigning the builder while the DetachedBuffer is alive frees the memory under it.

This adds MessageBuilder::Release(), which uses the existing three-argument ReleaseRaw() to move the slice into a new SliceAllocator and returns a DetachedBuffer that owns that allocator. The buffer then frees the slice itself when it goes away, and the builder can be reused like after ReleaseRaw() or ReleaseMessage().

grpc/tests/message_builder_test.cpp already had builder_reusable_after_release_test and builder_reusable_after_release_and_move_assign_test, disabled with FIXME notes describing exactly these asserts. This change enables them and removes the notes.

Testing: built message_builder_tests() from grpc/tests/message_builder_test.cpp with clang and AddressSanitizer against gRPC 1.85. With the change all tests pass and ASan is clean; without the header change the newly enabled tests fail with the slice_.size() == 0 assert.

MessageBuilder inherited FlatBufferBuilder::Release(), which returns a
DetachedBuffer that still points into the grpc::Slice held by the
builder's SliceAllocator. Reusing the builder then hit an assert in
SliceAllocator::allocate, and destroying or move-assigning it freed the
memory the DetachedBuffer was still using.

Add a Release() override that moves the slice into a new SliceAllocator
owned by the DetachedBuffer, and enable the two reuse-after-Release tests
that were disabled because of this.

This branch has not been deployed

No deployments
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.

1 participant