Skip to content

Reading from PTY in OSX makes the CPU go to 100% #52

@kristoiv

Description

@kristoiv

The following go code running on OSX (version 10.12.6, MacBook Pro (Retina, 15-inch, Mid 2014)) will cause the CPU to go to 100% within 10 seconds, and stay there. It is due to the io.Copy(stdout, t) line. It doesn't happen on Ubuntu 16.04 (nor Android 5.1.1).

Anyone have any ideas as to why?

package main

import (
	"io"
	"log"
	"os"
	"os/exec"

	"golang.org/x/crypto/ssh/terminal"

	"github.com/kr/pty"
)

func main() {
	stdin := os.Stdin
	stdout := os.Stdout

	os.Stdin = nil
	os.Stdout = nil
	// os.Stderr = nil

	bash := exec.Command("/bin/bash")

	// Allocate a terminal for this channel
	t, err := pty.Start(bash)
	if err != nil {
		log.Fatalf("Could not start pty (%s)\n", err)
	}

	_, err = terminal.MakeRaw(int(stdin.Fd()))
	if err != nil {
		log.Fatalln(err)
	}

	go func() {
		if _, err := io.Copy(stdout, t); err != nil {
			log.Fatalln(err)
		}
	}()

	if _, err := io.Copy(t, stdin); err != nil {
		log.Fatalln(err)
	}
}

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