Giter VIP home page Giter VIP logo

Comments (8)

xenoscopic avatar xenoscopic commented on July 28, 2024

It's not entirely true that data will stay at the beginning of the buffer until the stream is closed, is it? If you reach a point where the bytes.Buffer is completely emptied, its internal position tracker will be reset to point to the beginning of its buffer and its capacity will be reused (https://golang.org/src/bytes/buffer.go?s=8509:8559#L248).

But I agree that it does seem like even if you are constantly Reading (e.g. doing an io.Copy call into a file) you might never actually reach that condition unless you happen to do just the right read with just the right buffer size, and thus the buffer would continue to grow. Is that what you see in practice? I ask as someone considering using this library.

from yamux.

Matthias247 avatar Matthias247 commented on July 28, 2024

@havoc-io Yes, Buffer has this property. However if you look at https://github.com/hashicorp/yamux/blob/master/stream.go#L107-L112 you will see that this branch will never be taken as Read() will only be called if there is something in the buffer. But even if it would apply, as we can't guarantee that the reader is faster than the writer and that the buffer will ever be empty it would not be the best solution.

You can see the problem happens in the unit tests if you output the capacity of the buffer after TestSendData_Large with fmt.Printf("Recv buf cap: %d\n", stream.recvBuf.Cap())

I would fix it by replacing the Buffer with a simple slice with a behavior similar to here: https://github.com/bradfitz/http2/blob/master/buffer.go

from yamux.

xenoscopic avatar xenoscopic commented on July 28, 2024

Ah, that's true. It's seems like a weird design decision on the part of bytes.Buffer to have truncation triggered on the beginning of Read rather than the end. Triggering at the end would at least potentially truncate in this case, though of course something like a ring buffer would make more sense. Thanks for clarifying!

from yamux.

stuartcarnie avatar stuartcarnie commented on July 28, 2024

The problem is the window update logic is broken. A Stream#Read which does not empty the recvBuf buffer in a single call will cause unbounded memory growth for large streams.

from yamux.

xenoscopic avatar xenoscopic commented on July 28, 2024

I believe this issue was solved by #53, so I think it can be closed. Using the following test (which mismatches write and read speeds) on darwin/amd64, I see memory growing extremely rapidly pre-#53 and stopping at around 5.0 MB post-#53. There's a bit of a slow asymptotic growth towards 5.0 MB upon starting the executable, perhaps due to the Go runtime/GC behavior, but it does seem to stop eventually.

package main

import (
	"crypto/rand"
	"io"
	"io/ioutil"
	"net"
	"time"

	"github.com/hashicorp/yamux"
)

type sleepReader struct {
	reader io.Reader
}

func (r *sleepReader) Read(buffer []byte) (int, error) {
	time.Sleep(10 * time.Millisecond)
	return r.reader.Read(buffer)
}

func main() {
	clientConn, serverConn := net.Pipe()
	clientSession, _ := yamux.Client(clientConn, yamux.DefaultConfig())
	serverSession, _ := yamux.Server(serverConn, yamux.DefaultConfig())

	go func() {
		c, _ := serverSession.Accept()
		reader := &sleepReader{c}
		io.Copy(ioutil.Discard, reader)
	}()

	c, _ := clientSession.Open()
	io.Copy(c, rand.Reader)
}

from yamux.

pbedat avatar pbedat commented on July 28, 2024

I have a similar problem under production conditions like OP.
My memory is not growing 1Mb per 1Mb streamed data, but it is growing over time.

Here is a pprof dump (linux, amd64), that pointed me to this issue:

Showing nodes accounting for 9243.28kB, 100% of 9243.28kB total
Showing top 10 nodes out of 14
      flat  flat%   sum%        cum   cum%
 8731.23kB 94.46% 94.46%  8731.23kB 94.46%  bytes.makeSlice
  512.05kB  5.54%   100%   512.05kB  5.54%  github.com/hashicorp/yamux.newStream
         0     0%   100%  8731.23kB 94.46%  bytes.(*Buffer).ReadFrom
         0     0%   100%  8731.23kB 94.46%  bytes.(*Buffer).grow
         0     0%   100%   512.05kB  5.54%  github.com/hashicorp/yamux.(*Session).Open
         0     0%   100%   512.05kB  5.54%  github.com/hashicorp/yamux.(*Session).OpenStream
         0     0%   100%  8731.23kB 94.46%  github.com/hashicorp/yamux.(*Session).handleStreamMessage
         0     0%   100%  8731.23kB 94.46%  github.com/hashicorp/yamux.(*Session).recv
         0     0%   100%  8731.23kB 94.46%  github.com/hashicorp/yamux.(*Session).recvLoop
         0     0%   100%  8731.23kB 94.46%  github.com/hashicorp/yamux.(*Stream).readData

The biggest jumps occur, when a new stream (OpenStream) begins to stream. I was wondering, if the problem is on my side, that the code retains open streams, but I'm pretty sure it doesn't.

Is there anything else I could provide, apart from a test case, that reproduces the issue?

from yamux.

evanphx avatar evanphx commented on July 28, 2024

@pbedat What version of yamux are you using, could you retest with the latest. Any repro would be great as well!

from yamux.

pbedat avatar pbedat commented on July 28, 2024

@evanphx I was using github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce (Go 1.15)

I switched to smux right after that post, which served me well. I could try to reproduce this, but I don't think it would be worth it. A lot has changed in 1.5 years 😉
If you really really need a repro I will try my best.

from yamux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.