Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Yamux] Fix sending of buffered messages after a window update #312

Merged
merged 14 commits into from
Sep 8, 2023

Conversation

StefanBratanov
Copy link
Collaborator

@StefanBratanov StefanBratanov commented Sep 6, 2023

Mainly changes and refactoring around SendBuffer class. Also using a single windowSizes map for receive and send window sizes since the MuxId is different.

Before logic was checking the total written bytes in the buffer against the sendWindow to determine if it fits within window. That is not entirely correct since the sendWindow is changed inside sendBlocks method. That could lead to incorrect behaviour.

Also there is a possibility that sendWindow is negative, so adding a check which will help with one of the exceptions captured in #311 java.lang.IllegalArgumentException: minimumReadableBytes : -52227 (expected: >= 0)

Also change to calculate totalBufferedWrites on the fly based on the existing SendBuffer values in the map.

Removed data.retain and data.release in SendBuffer.add() and SendBuffer.flush(). Use data.readRetainedSlice() for the partial buffer sending.

Copy link
Collaborator

@Nashatyrev Nashatyrev left a comment

Choose a reason for hiding this comment

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

LGTM

nit: TBH I would refactor this part

    private val receiveWindows = ConcurrentHashMap<MuxId, AtomicInteger>()
    private val sendWindows = ConcurrentHashMap<MuxId, AtomicInteger>()
    private val sendBuffers = ConcurrentHashMap<MuxId, SendBuffer>()
    private val totalBufferedWrites = AtomicInteger()
  1. Looks like we need just a single Map<MuxId, *>
  2. Probably it makes sense to make totalBufferedWrites calculatable on the fly. Seems much safer while probably not that bad with respect to performance

libp2p/src/main/kotlin/io/libp2p/mux/yamux/YamuxHandler.kt Outdated Show resolved Hide resolved

fun add(data: ByteBuf) {
buffered.add(data.retain())
bufferedData.add(data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Right! It was excessive pair of retain/release.
Just to note: the general rule of sending the handler closest to the wire is responsible for releasing the message buffer

@StefanBratanov StefanBratanov merged commit 41c17ae into libp2p:develop Sep 8, 2023
2 checks passed
@StefanBratanov StefanBratanov deleted the yamux_upstream_messages branch September 8, 2023 07:31
@ianopolous
Copy link
Contributor

I think this introduces a vulnerability by merging the send and receive windows. A remote could send a window update for their own receive buffer id, and trick us into setting our receive window for them to a large value?

@Nashatyrev
Copy link
Collaborator

@ianopolous thanks for that point 👍
@StefanBratanov what do you think on this?

@StefanBratanov
Copy link
Collaborator Author

@ianopolous thanks for that point 👍 @StefanBratanov what do you think on this?

I think it's a reasonable feedback. Could raise a PR for future discussion.

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.

3 participants