tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
14.73k stars 859 forks source link

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

Open sparques opened 2 months ago

sparques commented 2 months ago

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)
}