fix: correct buffers concatenation in the socket manager (Deluge) #787
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
There currently is an issue with the concatenation of buffers in the
clientRequestManager.ts
of the Deluge service, more specifically at L129. If a client has a lot of torrents (in the current case, 588 torrents crash the software - though this is not the minimal amount to reproduce the issue), Flood will crash at boot up when it receives the packets from Deluge. Below is an explanation of the issue more specifically:I have debugged the line to see what goes wrong. If you take a look at the first screenshot, you can see that
rpcBuffer.length = 16379
,chunk.length = 16384
andrpcBufferSize = 50328
.rpcBuffer
is the content of the buffer currently in memory (captured from a previous packet received),chunk
is the new buffer received by Flood, andrpcBufferSize
is the expected total size of the buffer.Based on those values, we should expect the new size of
rpcBuffer
to be16379 + 16384 = 32763
. There's other packets to obtain before being able to process the whole buffer at L140, which checks that the current length ofrpcBuffer
is equal of greater thanrpcBufferSize
.The issue resides in the call made to
Buffer.concat
, specifically the second parameter. Taking a look at the second screenshot, you can see that the size ofrpcBuffer
is now50328
, when it should've been32763
.Taking a look at the documentation for the function Buffer.concat(list[, totalLength]), it mentions:
totalLength <integer>: Total length of the Buffer instances in list when concatenated.
. We can observe that behavior by taking a look at the end of the bufferchunk
andrpcBuffer
afterBuffer.concat
is called (50 last elements for both):This means that by adding
rpcBufferSize
as the total length of the buffer when callingBuffer.concat(list[, totalLength])
, it will add 0s at the end of the new buffer until its length is the expected value. This is not the expected behavior here, as we will receive subsequent packets after the current one that we will have to append to the buffer. Hence, I have modified the code to specifyrpcBufferSize
for the total length only ifrpcBuffer.length + chunk.length
is bigger (or equal to) thanrpcBufferSize
. We could also completely remove the second parameter, but it wouldn't be ideal.Related Issue
See #788 for a tracking of the issue.
Screenshots
FIRST SCREENSHOT
SECOND SCREENSHOT
Types of changes
This is a bug fix and should not break the typical behavior of the project. It should allow Flood to handle a more subsequent load of torrents at the same time.