Skip to content

THRIFT-6138: Give Ruby memory buffers private ownership - #3702

Open
kpumuk wants to merge 1 commit into
apache:masterfrom
kpumuk:rb-memory-buffer-ownership
Open

THRIFT-6138: Give Ruby memory buffers private ownership#3702
kpumuk wants to merge 1 commit into
apache:masterfrom
kpumuk:rb-memory-buffer-ownership

Conversation

@kpumuk

@kpumuk kpumuk commented Aug 5, 2026

Copy link
Copy Markdown
Member

Ruby MemoryBufferTransport currently retains a caller-supplied string as its internal storage. Mutating either object can therefore unexpectedly alter the other, frozen input can fail during later writes or resets, and construction can change the caller's string encoding.

This change duplicates constructor input before normalizing it to binary encoding. The transport consequently owns a private mutable buffer: caller mutations no longer change unread bytes, transport operations no longer modify the caller's string, and frozen input remains usable.

This deliberately removes the historical shared-buffer behavior. Callers that intentionally treated the original string as a live view of the transport will instead need to read from the transport.

  • Did you create an Apache Jira ticket? THRIFT-6138
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

Copilot AI lite review requested due to automatic review settings August 5, 2026 11:36
@mergeable mergeable Bot added the ruby Pull requests that update Ruby code label Aug 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Updates Ruby MemoryBufferTransport to privately own its internal buffer by duplicating constructor input before normalizing encoding, preventing unexpected caller/transport side effects and allowing frozen inputs.

Changes:

  • Duplicate constructor-provided buffers before forcing binary encoding.
  • Update specs to validate private ownership behavior and frozen-input usability.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/rb/lib/thrift/transport/memory_buffer_transport.rb Make constructor duplicate input string before binary-encoding to ensure private mutable storage.
lib/rb/spec/base_transport_spec.rb Replace shared-buffer spec with new expectations for private ownership and frozen-input behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/rb/lib/thrift/transport/memory_buffer_transport.rb
Comment thread lib/rb/lib/thrift/transport/memory_buffer_transport.rb
Client: rb

Co-Authored-By: OpenAI Codex (GPT-5.6) <codex@openai.com>
Copilot AI review requested due to automatic review settings August 5, 2026 12:21
@kpumuk
kpumuk force-pushed the rb-memory-buffer-ownership branch from 75ff595 to f75f4f0 Compare August 5, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

lib/rb/spec/base_transport_spec.rb:317

  • The expectation at line 317 uses .b on the expected value, but eq only checks ==, which does not reliably assert the returned string’s encoding (ASCII-8BIT vs UTF-8 can still compare equal for compatible byte sequences). If the intent is to verify the transport returns binary-encoded data, add an explicit assertion on the returned value’s .encoding (and keep the byte/content assertion separately).
      expect(source.encoding).to eq(Encoding::UTF_8)
      source.replace("caller changed")
      @buffer.write("!")

      expect(source).to eq("caller changed")
      expect(@buffer.read(@buffer.available)).to eq("this is a test!".b)

lib/rb/lib/thrift/transport/memory_buffer_transport.rb:28

  • Calling dup unconditionally on any non-nil buffer changes the failure mode for non-String inputs (e.g., TypeError: can't dup Integer) and may reject objects that are string-like via to_str if Bytes.force_binary_encoding previously handled them. If this constructor is intended to accept string-like objects, consider coercing via to_str (or String(...) if appropriate) and raising a consistent ArgumentError for invalid types before duplicating.
    def initialize(buffer = nil)
      @buf = buffer ? Bytes.force_binary_encoding(buffer.dup) : Bytes.empty_byte_buffer

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

Labels

ruby Pull requests that update Ruby code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants