go: net: io.CopyN fails to copy from file to net.Conn on Windows

What version of Go are you using (go version)?

go version go1.10.2 windows/amd64

Does this issue reproduce with the latest release?

That is the latest (stable) release.

What operating system and processor architecture are you using (go env)?

windows/amd64

What did you do?

  • Be on Windows (I tried this on Linux and couldn’t reproduce the bug.)
  • Build this program:
package main

import (
	"fmt"
	"io"
	"net"
	"os"
	"time"
)

func main() {
	l, err := net.Listen("tcp", ":1234")
	fmt.Println(err)
	for {
		conn, err := l.Accept()
		fmt.Println(err)
		f, err := os.Open("alphabet.txt")
		fmt.Println(err)

		for {
			n, err := io.CopyN(conn, f, 3)
			fmt.Println(n, err)
			if err != nil {
				break
			}
			time.Sleep(1 * time.Second)
		}
		conn.Close()
	}
}
  • Create a file alphabet.txt:
abcdefghijklmnopqrstuvwxyz
  • Run the program
  • Connect to port 1234 with PuTTY or netcat.

What did you expect to see?

abc <pause> def <pause> ghi <pause> jkl ...

What did you see instead?

abc <pause> def <pause> def <pause> def ...

Notes

I could only reproduce this when copying from a file to a network connection. If I for example replace the network connection with os.Stdout or the file with strings.NewReader() the issue doesn’t happen.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 40 (23 by maintainers)

Commits related to this issue

Most upvoted comments

I also got fail on Windows 10 64bit.

--- FAIL: TestSendfileParts (0.00s)
    sendfile_test.go:145: unexpected server reply "Producduc", want "Produced "
FAIL
FAIL    net     7.730s

@alexbrainman I think your change doesn’t trigger the bug. At least the part about advancing file position. Simple test would be to call CopyN(conn, f, 3) three times in a row and compare the result on the other end with "Produced ". You don’t want to seek. This bug will cause the string to be “Producduc” instead.