Skip to content

Decoder.More() doesn't read end of socket stream properly #264

Closed
@Xaenalt

Description

@Xaenalt

The issue for Decoder.More() has been resolved, thank you! Unfortunately, now when reading from socket with the code in #261 the end of the stream starts to throw

ReadMapCB: expect { or n, but found , error found in #10 byte of ...|"1000000"}
|..., bigger context ...|"999998"}
{"count": "999999"}
{"count": "1000000"}
|...

Upon adding some more debugging code, it seems that Decoder.More() is returning true at the end of the stream

Updated code:

# for i in {0..1000000}; do echo "{\"count\": \"$i\"}" >> 1mjson; done
# nc localhost 1234 < 1mjson
package main

import (
	"fmt"
	"net"

	"github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func main() {
	addr := ":1234"
	tcpAddr, _ := net.ResolveTCPAddr("tcp4", addr)
	listener, _ := net.ListenTCP("tcp", tcpAddr)

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}

		go func(conn net.Conn) {
			defer conn.Close()
			jsonReader := json.NewDecoder(conn)

			for jsonReader.More() {
				var jsonMsg map[string]interface{}
				err := jsonReader.Decode(&jsonMsg)
				if err != nil {
					fmt.Printf("Decoder.More(): %v\n", jsonReader.More())
					fmt.Println(err)
				}
				fmt.Println(jsonMsg)

			}
			fmt.Println("Deferred close of connection executing")
		}(conn)

	}

}

Updated output:

Decoder.More(): true
ReadMapCB: expect { or n, but found , error found in #10 byte of ...|"1000000"}
|..., bigger context ...|"999998"}
{"count": "999999"}
{"count": "1000000"}
|...

I've worked around this temporarily by just returning on err != nil, but it would be awesome to have this bug squashed too

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions