Coder Social home page Coder Social logo

Comments (7)

linde12 avatar linde12 commented on July 26, 2024

I wrote a test for this. And if you add my modification(see code block above) to stream.go you will see that there is still data in the internal buffer to be read.

I replace everything in session.go with this temporarily, and then i ran go test -test.v -run TestRace many times. Sometimes it passes, sometimes it fails.

Here is the code:

package smux

import (
	"io"
	"log"
	"math/rand"
	"net"
	"net/http"
	"testing"
)

func init() {
	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	ln, err := net.Listen("tcp", "127.0.0.1:19999")
	if err != nil {
		// handle error
		panic(err)
	}
	go func() {
		for {
			conn, err := ln.Accept()
			if err != nil {
				// handle error
			}
			go handleConnection(conn)
		}
	}()
}

func handleConnection(conn net.Conn) {
	session, _ := Server(conn, nil)
	for {
		if stream, err := session.AcceptStream(); err == nil {
			go func(s io.ReadWriteCloser) {
				buf := make([]byte, 50000)
				read := 0
				for read != len(buf) {
					n, err := s.Read(buf[read:])
					if err != nil {
						log.Fatalf("server read err: %v\n", err)
					}
					read += n
				}
				s.Write(buf)
				// Server closes stream properly
				s.Close()
				// And immedately after closes the session
				// which still causes the race to happen
				session.Close()
			}(stream)
		} else {
			return
		}
	}
}

func TestRace(t *testing.T) {
	cli, err := net.Dial("tcp", "127.0.0.1:19999")
	if err != nil {
		t.Fatal(err)
	}
	session, _ := Client(cli, nil)
	stream, _ := session.OpenStream()
	data := make([]byte, 50000)
	for i := range data {
		data[i] = byte(rand.Int())
	}
	if _, err = stream.Write(data); err != nil {
		t.Fatalf("write to stream failed: %v\n", err)
	}

	buf := make([]byte, len(data))
	read := 0
	for read != len(buf) {
		if n, err := stream.Read(buf[read:]); err == nil {
			read += n
		} else {
			log.Fatalf("Read error: %v\n", err)
		}
	}

	if string(buf) != string(data) {
		t.Fatal("Buf and data are not eq\n")
	}
}

from smux.

xtaci avatar xtaci commented on July 26, 2024

https://github.com/xtaci/smux/blob/master/stream.go#L55

are your referring to this ?

AFAIK,the behavior of reading after close is not specified in net.Conn.

also , it's completly ok to remove this select.

from smux.

linde12 avatar linde12 commented on July 26, 2024

Yes, exactly. I would expect that the internal buffer of stream would have to be completely read(via stream.Read) by a consumer before it says that the pipe is broken. On the server session.Close() was run after stream.Write(...) so i would expect to receive all data from the write operation before receiving the "broken pipe" message.

With a normal net.Conn, if you do this

buf := make([]byte, LEN)
conn.Write(buf)
conn.Close()

the client will first receive 50.000 bytes and then the connection will be closed.

But with smux it becomes a race because of the multiplexing. The client might receive some data, and then maybe a signal saying the session was closed before the rest of the data has been read/consumed.

from smux.

xtaci avatar xtaci commented on July 26, 2024

try the commit pls

from smux.

linde12 avatar linde12 commented on July 26, 2024

Works like a charm for me!

from smux.

xtaci avatar xtaci commented on July 26, 2024

happy to hear

from smux.

linde12 avatar linde12 commented on July 26, 2024

Thanks for your effort, closing! 🥇

from smux.

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.