Skip to content

Conversation

@SuchitraSwain
Copy link

@SuchitraSwain SuchitraSwain commented Sep 13, 2025

PR #923 Review: Input Validation and Error Handling Improvements

Overview

This PR adds comprehensive input validation, exception handling, and test coverage across multiple libp2p modules. While it references Issue #194 (which is already resolved), the actual changes provide significant value for code quality and defensive programming.

Key Changes

Real Value Added

  1. Input Validation: Added checks for empty protocol IDs, null handlers, negative deadlines
  2. Exception Handling: Proper error messages and exception chaining
  3. Test Coverage: 500+ lines of new tests covering edge cases
  4. API Consistency: Unified error handling patterns across modules

Files Modified (25 files)

  • Core: abc.py, basic_host.py, autonat.py
  • Stream Muxers: mplex_stream.py, yamux.py
  • Transport: quic/stream.py, tcp/tcp.py
  • Tests: Comprehensive test coverage added

Technical Improvements

Before vs After

# Before: No validation, always returned True
def set_deadline(self, ttl: int) -> bool:
    self.read_deadline = ttl
    return True  # Even for negative values!

# After: Proper validation and exceptions
def set_deadline(self, ttl: int) -> None:
    try:
        if ttl < 0:
            raise ValueError("Deadline cannot be negative")
        self.read_deadline = ttl
    except Exception as e:
        raise MuxedStreamError(f"Failed to set deadline: {e}") from e

Test Coverage Added

New Test Files

  • test_identify_push_exception_handling.py (146 lines) - Exception handling tests
  • Enhanced test_basic_host.py (151 lines) - Input validation tests
  • Enhanced test_mplex_stream.py (96 lines) - Deadline validation tests

Test Quality

  • Input Validation: Tests empty strings, null values, negative numbers
  • Exception Handling: Tests proper error propagation and chaining
  • Edge Cases: Comprehensive coverage of failure scenarios

Assessment

Strengths

  • Defensive Programming: Prevents runtime errors with invalid inputs
  • Better UX: More descriptive error messages for debugging
  • Comprehensive Testing: Extensive test coverage for edge cases
  • Code Quality: Improves maintainability and reliability

⚠️ Issues

Recommendation

REFACTOR, DON'T DISCARD

Update PR Description: Remove Issue #194 references, focus on actual improvements:

  • Input validation and defensive programming
  • Better error handling and debugging
  • Comprehensive test coverage
  • Code quality improvements

Keep the Code: The changes provide real value for:

  • Preventing runtime errors
  • Improving debugging experience
  • Adding defensive programming
  • Enhancing code reliability

Final Assessment

Status: ✅ Approve with description update

The PR has significant value but needs reframing. The code improvements are excellent - just update the description to focus on the actual benefits rather than the already-resolved issue.

Review based on PR #923 - SuchitraSwain:194-try-catch branch

Cute Animal Picture

image

Suchitra Swain added 2 commits September 13, 2025 14:20
- Update multiaddr dependency from 3ea7f866fda9268ee92506edf9d8e975274bf941 to db8124e2321f316d3b7d2733c7df11d6ad9c03e6
- This fixes the build failure caused by missing py-cid branch 'acul71/new-setup'
- Resolves Read the Docs build failure in PR libp2p#923
Copy link
Contributor

@sumanjeet0012 sumanjeet0012 left a comment

Choose a reason for hiding this comment

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

@SuchitraSwain You can take a look at other methods which is still using the True / False return value and change it to responce / exception.

except HostException:
raise
except Exception as e:
raise HostException(f"Failed to set stream handler: {e}") from e
Copy link
Contributor

Choose a reason for hiding this comment

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

@SuchitraSwain Thank you for adding the try-catch block and input validation to this function. However, I believe this PR may not be addressing the core issue described in #194.
Issue #194 is specifically about "changing return True/False pattern to try/catch pattern," but the True/False pattern has already been removed from py-libp2p in previous commits.
For reference, the earlier implementation at this commit did use the True/False return pattern, but the current codebase has already migrated away from this pattern.

Copy link
Contributor

Choose a reason for hiding this comment

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

@SuchitraSwain
Change the PR title

@sumanjeet0012
Copy link
Contributor

@SuchitraSwain Have you tried running all the tests locally? Are they all passing?
Since these changes may impact other modules, please ensure that all tests pass locally first. After that, we can proceed with running the CI/CD pipeline.

@SuchitraSwain
Copy link
Author

@SuchitraSwain Have you tried running all the tests locally? Are they all passing? Since these changes may impact other modules, please ensure that all tests pass locally first. After that, we can proceed with running the CI/CD pipeline.

YES I have tested it locally, everything passes

@sumanjeet0012
Copy link
Contributor

@SuchitraSwain resolve the merge conflicts.

@sumanjeet0012
Copy link
Contributor

@seetadev kindly run the CI/CD pipeline.

@seetadev
Copy link
Contributor

@sumanjeet0012 : Ran the CI/Cd pipeline. Waiting for test results.

@seetadev
Copy link
Contributor

@SuchitraSwain : 2 test cases are failing. Please collaborate with @sumanjeet0012 and @acul71 to get them fixed and arrive at a good conclusion on this PR.

@SuchitraSwain
Copy link
Author

@seetadev could you please re-run the ci/cd again

@SuchitraSwain
Copy link
Author

SuchitraSwain commented Sep 15, 2025

@seetadev The PR is ready

@seetadev
Copy link
Contributor

@SuchitraSwain : Thank you so much Suchitra for considering our feedback points. Appreciate it.

Will do a final review today. Please add a test suite and a newsfragment file too.

CCing @sumanjeet0012, who will help you complete these aspects in the PR.

Copy link
Contributor

@sumanjeet0012 sumanjeet0012 left a comment

Choose a reason for hiding this comment

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

@SuchitraSwain The PR looks good to me and is ready to go from my side. I have just left a minor piece of feedback for your consideration.
Once it is addressed, we will run the CI/CD pipeline and proceed with merging this PR.

@seetadev
Copy link
Contributor

Great work team 👏

@SuchitraSwain — thank you for taking the time to carefully rework the return pattern into a proper try/catch exception handling flow, add test coverage, and follow through on the review feedback. This brings much-needed consistency across the codebase and improves clarity for future maintainers.

@sumanjeet0012 — thanks for the thorough review, guidance, and ensuring the changes aligned with the original issue intent. The back-and-forth here really helped refine the PR into a solid contribution.

With the added tests and newsfragment in place, this looks ready to land 🚀

@sumanjeet0012 : Will like you do a final review and add a thumbs up to this comment. Will merge right away.

Appreciate the collaboration from both sides!

@seetadev
Copy link
Contributor

@SuchitraSwain : Re-ran the CI/Cd pipeline. Some test cases are failing. Please check and arrive at a good conclusion on the issues.

@acul71
Copy link
Contributor

acul71 commented Sep 22, 2025

@SuchitraSwain
I've seen some CI/CD tests failing
ping me if you need help ;-)

@SuchitraSwain
Copy link
Author

@seetadev please re-ran the pipeline

@SuchitraSwain
Copy link
Author

Thank you @seetadev

Copy link
Contributor

@acul71 acul71 left a comment

Choose a reason for hiding this comment

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

PR #923 Review: Input Validation and Error Handling Improvements

Overview

This PR adds comprehensive input validation, exception handling, and test coverage across multiple libp2p modules. While it references Issue #194 (which is already resolved), the actual changes provide significant value for code quality and defensive programming.

Key Changes

Real Value Added

  1. Input Validation: Added checks for empty protocol IDs, null handlers, negative deadlines
  2. Exception Handling: Proper error messages and exception chaining
  3. Test Coverage: 500+ lines of new tests covering edge cases
  4. API Consistency: Unified error handling patterns across modules

Files Modified (25 files)

  • Core: abc.py, basic_host.py, autonat.py
  • Stream Muxers: mplex_stream.py, yamux.py
  • Transport: quic/stream.py, tcp/tcp.py
  • Tests: Comprehensive test coverage added

Technical Improvements

Before vs After

# Before: No validation, always returned True
def set_deadline(self, ttl: int) -> bool:
    self.read_deadline = ttl
    return True  # Even for negative values!

# After: Proper validation and exceptions
def set_deadline(self, ttl: int) -> None:
    try:
        if ttl < 0:
            raise ValueError("Deadline cannot be negative")
        self.read_deadline = ttl
    except Exception as e:
        raise MuxedStreamError(f"Failed to set deadline: {e}") from e

Test Coverage Added

New Test Files

  • test_identify_push_exception_handling.py (146 lines) - Exception handling tests
  • Enhanced test_basic_host.py (151 lines) - Input validation tests
  • Enhanced test_mplex_stream.py (96 lines) - Deadline validation tests

Test Quality

  • Input Validation: Tests empty strings, null values, negative numbers
  • Exception Handling: Tests proper error propagation and chaining
  • Edge Cases: Comprehensive coverage of failure scenarios

Assessment

Strengths

  • Defensive Programming: Prevents runtime errors with invalid inputs
  • Better UX: More descriptive error messages for debugging
  • Comprehensive Testing: Extensive test coverage for edge cases
  • Code Quality: Improves maintainability and reliability

⚠️ Issues

  • Misleading Description: Claims to fix Issue #194 (already resolved)
  • Wrong Premise: The True/False pattern was already eliminated years ago

Recommendation

REFACTOR, DON'T DISCARD

Update PR Description: Remove Issue #194 references, focus on actual improvements:

  • Input validation and defensive programming
  • Better error handling and debugging
  • Comprehensive test coverage
  • Code quality improvements

Keep the Code: The changes provide real value for:

  • Preventing runtime errors
  • Improving debugging experience
  • Adding defensive programming
  • Enhancing code reliability

Final Assessment

Status: ✅ Approve with description update

The PR has significant value but needs reframing. The code improvements are excellent - just update the description to focus on the actual benefits rather than the already-resolved issue.


Review based on PR #923 - SuchitraSwain:194-try-catch branch

@seetadev
Copy link
Contributor

@acul71 : Thank you so much for your review and feedback. Appreciate your great support.

@SuchitraSwain , @bomanaps and @yashksaini-coder : Wish if you could address the valuable feedback points shared by @acul71 and make the requisite changes. Once done, kindly ping me. I'll do a final review and then merge.

@SuchitraSwain
Copy link
Author

I will address the comment by today

@seetadev
Copy link
Contributor

@yashksaini-coder , @SuchitraSwain : Re-ran the CI/CD pipeline. We will see the test results soon.

@seetadev
Copy link
Contributor

@SuchitraSwain , @yashksaini-coder : This looks good for final review and merge.

Please change the PR description as shared by @acul71

@SuchitraSwain
Copy link
Author

@seetadev it's done, please merge the PR. Thank you for the great support

@seetadev
Copy link
Contributor

@SuchitraSwain : Wish to have @acul71 and @pacrob 's review on this PR. Also, did you mistakenly close the PR request?

@SuchitraSwain
Copy link
Author

@SuchitraSwain : Wish to have @acul71 and @pacrob 's review on this PR. Also, did you mistakenly close the PR request?

yes it was by mistake.

- Fix TCPListener.listen return type to match IListener interface
- Fix BasicHost.set_stream_handler boolean context issue
- Update demo code to handle new push_identify_to_peer signature
- Fix test type errors with TProtocol casting and mock objects
- Resolve all linting and formatting issues
- All tests now pass with make pr command
@acul71
Copy link
Contributor

acul71 commented Sep 30, 2025

Hi @SuchitraSwain . I've been fixing some lint and typecheck errors and fix identify (thanks @pacrob )
Please review.
In some way (change the title or make sure to document what this issue is really about), so to not create confusion about things, like observed by @sumanjeet0012 I believe, some mods where already there but, this PR is anyway an improvement.

@seetadev
Copy link
Contributor

seetadev commented Oct 6, 2025

@yashksaini-coder and @SuchitraSwain : Thank you for updating the PR. Wish to check if we have covered all the feedback points as shared by @pacrob and me. We will do a final review once we receive a thumbs up from both of you.

CCing @acul71 for his feedback and review too.

@SuchitraSwain
Copy link
Author

@seetadev yes all the comments are addressed

@seetadev
Copy link
Contributor

seetadev commented Oct 8, 2025

@seetadev yes all the comments are addressed

@SuchitraSwain : Great to hear. I'll wait to hear for a review from @acul71. Will do a final review + merge after discussion in the maintainer's call.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants