Skip to content

Conversation

selfbalance
Copy link
Contributor

@selfbalance selfbalance commented Sep 4, 2025

Summary

  • ensure TeeReader::read_to_end writes only newly read bytes to the tee'd writer

Testing

  • cargo test -p defuse-io-utils

https://chatgpt.com/codex/tasks/task_b_68b9fa7b5288832c8b15a178fb27d15c

Summary by CodeRabbit

  • Bug Fixes
    • Resolved an issue where the tee functionality could forward pre-existing buffer data instead of only newly read bytes, causing duplicated or incorrect output.
    • Now, only data appended during a full read is written to the paired writer, ensuring accurate and reliable output in downstream consumers.
    • Improves data integrity for scenarios involving reading streams to completion.
    • No changes to the public API.

Copy link

coderabbitai bot commented Sep 4, 2025

Walkthrough

Adjusted TeeReader’s Read::read_to_end implementation so it writes only the bytes appended during the read. It now records the initial buffer length, delegates to read_to_end, and writes the slice of newly added data to the internal writer. No public API changes.

Changes

Cohort / File(s) Summary
TeeReader read_to_end behavior
io-utils/src/lib.rs
Modified Read::read_to_end to capture initial buf length and write only buf[len..] to the tee writer instead of buf[..n], ensuring only newly appended bytes are forwarded. No API surface changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant C as Caller
  participant T as TeeReader
  participant R as Inner Reader
  participant W as Internal Writer

  C->>T: read_to_end(buf)
  Note over T: Record original_len = buf.len()
  T->>R: read_to_end(buf)
  R-->>T: n = bytes appended (or error)

  alt n > 0
    T->>W: write_all(buf[original_len..])
    W-->>T: ok
    T-->>C: return n
  else n == 0
    T-->>C: return 0
  end

  opt error from R
    T-->>C: propagate error
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I buffered a burrow of bytes today,
Nibbled the new, let the old stay away.
My whiskers twitch at slices clean—
Only fresh carrots in the stream! 🥕
Hop, tee, write—the bug’s at bay,
Byte by byte, I bound in gray.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/locate-small-bugs-43odon

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
io-utils/src/lib.rs (3)

43-44: Clarify the doc comment to match behavior.

Make it explicit that only newly appended bytes are written.

-    /// Reads all remaining data into a buffer and writes it to the internal writer.
+    /// Reads all remaining data into `buf` and writes only the newly appended bytes to the internal writer.

79-94: Add a test for pre-filled buffers to prevent regressions.

Covers the case where buf already has data and ensures only appended bytes are tee’d.

@@
     fn tee_read_to_end() {
@@
         assert_eq!(&written[..n], b"stream this");
     }
+
+    #[test]
+    fn tee_read_to_end_prefilled_buf_writes_only_appended() {
+        let input_data = b"stream this";
+        let input = Cursor::new(input_data);
+        let output = Cursor::new(Vec::new());
+
+        let mut tee = input.tee(output);
+        let mut buf = b"prefix-".to_vec();
+
+        let n = tee.read_to_end(&mut buf).unwrap();
+        assert_eq!(n, input_data.len());
+        assert_eq!(buf, b"prefix-stream this");
+
+        let (_input, output) = tee.into_inner();
+        let written = output.into_inner();
+        assert_eq!(written, b"stream this");
+    }

44-49: Optional: write partial data on error.

If read_to_end appends some bytes then errors, you currently return early and write nothing. If you want “best effort” teeing, write the appended slice before propagating the error.

-        let n = self.reader.read_to_end(buf)?;
-        self.writer.write_all(&buf[len..])?;
-        Ok(n)
+        match self.reader.read_to_end(buf) {
+            Ok(n) => {
+                self.writer.write_all(&buf[len..])?;
+                Ok(n)
+            }
+            Err(e) => {
+                if buf.len() > len {
+                    // Best-effort: ignore a secondary write error to preserve original read error.
+                    let _ = self.writer.write_all(&buf[len..]);
+                }
+                Err(e)
+            }
+        }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 11fe297 and 161f0e4.

📒 Files selected for processing (1)
  • io-utils/src/lib.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Check
  • GitHub Check: Shared / Static Analysis (intents-contract, contract, near, defuse, 1, ., 1.18, false, --filter-paths "nod...
  • GitHub Check: Shared / Static Analysis (intents-poa-factory, contract, near, poa-factory, 1, ., 1.18, false, --filter-pa...
  • GitHub Check: Shared / Static Analysis (intents-poa-token, contract, near, poa-token, 1, ., 1.18, false, --filter-paths ...
  • GitHub Check: Shared security analysis / Static Analysis (intents-poa-factory, contract, near, poa-factory, 1, ., 1.18, false, --filter-pa...
  • GitHub Check: Shared security analysis / Static Analysis (intents-poa-token, contract, near, poa-token, 1, ., 1.18, false, --filter-paths ...
  • GitHub Check: Shared security analysis / Static Analysis (intents-contract, contract, near, defuse, 1, ., 1.18, false, --filter-paths "nod...
  • GitHub Check: Security Audit - report
🔇 Additional comments (1)
io-utils/src/lib.rs (1)

45-49: Correct fix: write only newly appended bytes in read_to_end.

Recording the initial len and writing &buf[len..] matches Read::read_to_end semantics and fixes the previous bug. Return value n remains correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant