Skip to content

io.Copy blocked after closing #88

@eudore

Description

@eudore

After ptmx is closed, io.Copy will not read and return an error.

example:

package main

import (
	"os/exec"
	"io"
	"fmt"
	"time"
	"github.com/creack/pty"
)

type w struct{}

func main() {
	cmd := exec.Command("bash")
	ptmx, err := pty.Start(cmd)
	if err != nil {
		panic(err)
	}
	go func() {
		time.Sleep(1* time.Second)
		fmt.Println("timeout")
		ptmx.Close()
	}()
	fmt.Println(io.Copy(&w{}, ptmx))
	fmt.Println("stop")
}

func (*w) Write(p []byte) (int,error) {
	return len(p), nil
}

example2:

func main() {
	cmd := exec.Command("bash")
	ptmx, err := pty.Start(cmd)
	if err != nil {
		panic(err)
	}
	go func() {
		time.Sleep(1 * time.Second)
		fmt.Println("timeout")
		ptmx.Close()
	}()
	body := make([]byte, 2048)
	for {
		n, err := ptmx.Read(body)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Println(body[0:n], err)
	}
	fmt.Println("stop")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions