tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

TinyGo Program blocks forever with os.Pipe, go proc, and a sleep; same program returns as expected with go

Open sparques opened this issue 1 year ago • 0 comments

Versions:

tinygo version 0.31.2 linux/amd64 (using go version go1.22.2 and LLVM version 17.0.1)

Code to replicate issue:

package main

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

func main() {
	rd, wr, err := os.Pipe()
	if err != nil {
		panic(err)
	}
	defer wr.Close()
	defer rd.Close()

	go io.Copy(os.Stdout, rd)

	os.Stdout = wr

	fmt.Printf("testing\n")
	fmt.Printf("testing part 2")

	time.Sleep(2 * time.Second)
}

When ran with go (go run test.go), the two Printf statements show, then it exits as expected.

When ran with TinyGo (tinygo run test.go), it prints the statements then hangs forever. Checking with the debugger, it seems it's waiting for additional input from the go io.Copy(). Commenting out either the sleep or the go io.Copy() will let the program return normally.

This appears to be related to the use of os.Pipe(), because the same program written using io.Pipe() returns as expected.

Returns as expected:

package main

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

func main() {
	rd, wr := io.Pipe()
	defer wr.Close()
	defer rd.Close()

	go io.Copy(os.Stdout, rd)

	fmt.Fprintf(wr, "testing\n")
	fmt.Fprintf(wr, "testing part 2")

	time.Sleep(2 * time.Second)
}

sparques avatar Apr 19 '24 15:04 sparques