Skip to content

Conversation

unniznd
Copy link

@unniznd unniznd commented Aug 12, 2025

What was wrong?

The multiselect type was inconsistent

How was it fixed?

Set the return type to None and handled the places where the negotiate function is used.

Reopening the PR #814 due to an error in the rebase.
Summary of approach.

To-Do

  • Clean up commit history
  • Add or update documentation related to these changes
  • Add entry to the release notes

Cute Animal Picture

put a cute animal picture link inside the parentheses

@acul71
Copy link
Contributor

acul71 commented Aug 14, 2025

Hello @unniznd
I think the Run tox / tox (3.10, docs) fails because of file name:
837.fix.rst in check https://github.com/libp2p/py-libp2p/blob/main/newsfragments/validate_files.py

ALLOWED_EXTENSIONS = {
    ".breaking.rst",
    ".bugfix.rst",
    ".deprecation.rst",
    ".docs.rst",
    ".feature.rst",
    ".internal.rst",
    ".misc.rst",
    ".performance.rst",
    ".removal.rst",
}

@unniznd
Copy link
Author

unniznd commented Aug 14, 2025

@acul71 I have fixed it.

@acul71
Copy link
Contributor

acul71 commented Aug 14, 2025

@acul71 I have fixed it.

Great.
If you run make pr and make docs # or make linux-docs everything pass ?

@unniznd
Copy link
Author

unniznd commented Aug 14, 2025

Yes, everything passes.

@seetadev
Copy link
Contributor

@unniznd : HI Aanand. Thank you for submitting the PR. Appreciate it.

Please resolve the merge conflicts. CCing @acul71 and @sumanjeet0012, who will help you arrive at a good conclusion on the issue.

@unniznd
Copy link
Author

unniznd commented Aug 25, 2025

@seetadev I have resolved the conflicts.

@seetadev
Copy link
Contributor

@unniznd : Sorry, 4 CI/CD issues still remains. No worries, take your time :) Appreciate the efforts.

@unniznd
Copy link
Author

unniznd commented Aug 25, 2025

@seetadev Fixed the CI/CD issue. Can you review the PR and give some feedback?

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.

@@ -288,6 +288,9 @@ async def _swarm_stream_handler(self, net_stream: INetStream) -> None:
protocol, handler = await self.multiselect.negotiate(
MultiselectCommunicator(net_stream), self.negotiate_timeout
)
if protocol is None:
await net_stream.reset()
raise StreamFailure("No protocol selected")
Copy link
Contributor

Choose a reason for hiding this comment

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

For error messages, can you be more verbose, something like:
raise StreamFailure("Failed to negotiate protocol: no protocol selected")

@unniznd
Copy link
Author

unniznd commented Aug 27, 2025

@acul71 I have added a testcase for this. Can you review the code and give some feedback? Should I update all the error messages since other messages were done by someone else in a different PR? I closed the other PR since I unfortunately rebased the branch, and other commits made in the main branch were applied to it. So I thought to close and create a new PR that will be cleaner

@acul71
Copy link
Contributor

acul71 commented Aug 28, 2025

Can you review the code and give some feedback? Should I update all the error messages since other messages were done by someone else in a different PR

Yes please. I think to remember I've see another couple of error messages to fix.

@acul71
Copy link
Contributor

acul71 commented Aug 28, 2025

Can you review the code and give some feedback

Thanks @unniznd the PR is well done.
If you can fix the other error messages I'll approve it and ask for merging.

@seetadev
Copy link
Contributor

@unniznd : Wonderful contribution. We are pretty near to having the PR fully ready for final review.

@acul71 : Thank you so much Luca. Appreciate your great support and guidance on this PR.

@unniznd
Copy link
Author

unniznd commented Aug 29, 2025

@acul71 @seetadev I have improved the error messages. Appreciate the feedback.

@acul71
Copy link
Contributor

acul71 commented Aug 29, 2025

@seetadev @pacrob
Thanks, @unniznd , I've reviewed and is ready to be merged.

Quick Overview

Fixed type consistency in Multiselect.negotiate() method by updating return type from tuple[TProtocol, ...] to tuple[TProtocol | None, ...] to properly reflect that it can return None.

Key Changes

  • Type Annotation: -> tuple[TProtocol | None, StreamHandlerFn | None]
  • Error Handling: Added proper StreamFailure handling in basic_host.py
  • Tests: Added 5 new test cases covering None protocol scenarios

Impact

  • ✅ No breaking changes
  • ✅ All existing code continues to work
  • ✅ Improved type safety
  • ✅ Better error messages

Files Changed

  • libp2p/protocol_muxer/multiselect.py - Core type fix
  • libp2p/host/basic_host.py - Error handling
  • tests/core/protocol_muxer/test_protocol_muxer.py - 4 new tests
  • tests/core/host/test_basic_host.py - 1 new test

Recommendation

APPROVE - Clean, safe, well-tested fix that improves type consistency.

@@ -118,6 +118,8 @@ async def select_transport(
# Select protocol if non-initiator
protocol, _ = await self.multiselect.negotiate(communicator)
if protocol is None:
raise MultiselectError("fail to negotiate a security protocol")
raise MultiselectError(
"fail to negotiate a security protocol: no protocl selected"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"fail to negotiate a security protocol: no protocl selected"
"Failed to negotiate a security protocol: no protocol selected"

@@ -85,7 +85,9 @@ async def select_transport(self, conn: IRawConnection) -> TMuxerClass:
else:
protocol, _ = await self.multiselect.negotiate(communicator)
if protocol is None:
raise MultiselectError("fail to negotiate a stream muxer protocol")
raise MultiselectError(
"fail to negotiate a stream muxer protocol: no protocol selected"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"fail to negotiate a stream muxer protocol: no protocol selected"
"Failed to negotiate a stream muxer protocol: no protocol selected"

@pacrob
Copy link
Member

pacrob commented Aug 29, 2025

Overall good, just a couple places to update to keep messaging consistent.

@seetadev
Copy link
Contributor

@unniznd : Great work, Aanand. Wish if you could address the feedback points shared by @pacrob.

Overall the PR is in good shape. Kindly share a detailed blog (you can open up a discussion page for the same) on how you did this PR once you resolve the above issues.

Since this was your initial PRs in py-libp2p, which is getting merged, we would like you to share your experiences from a technical perspective in detail. This would also help the new contributors to take up projects in py-libp2p and arrive at a good conclusion on them. Keep up the wonderful progress.

@unniznd
Copy link
Author

unniznd commented Sep 1, 2025

@pacrob, I have made the changes.

@seetadev
Copy link
Contributor

seetadev commented Sep 1, 2025

@unniznd : Great work, Aanand. This PR is indeed ready for final review + merge.

Congratulations on the wonderful progress. Keep it up :) Looking forward to sharing more detailed projects with you this week.

@acul71 and @pacrob : Thank you so much for your great support and mentorship. Appreciate it.

@seetadev
Copy link
Contributor

seetadev commented Sep 1, 2025

@acul71 : Looks like an unrelated test case ( FAILED tests/core/pubsub/test_gossipsub_px_and_backoff.py) is failing the CI/CD checks. We can fix this in another issue.

@unniznd can make the requisite changes.

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.

4 participants