Skip to content

os/exec: incorrect "fatal error: all goroutines are asleep - deadlock!" #41271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
martinrode opened this issue Sep 8, 2020 · 4 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@martinrode
Copy link

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

$ go version
go version go1.15 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/Users/martin/go/bin"
GOCACHE="/Users/martin/Library/Caches/go-build"
GOENV="/Users/martin/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/martin/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/martin/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/martin/go/src/github.com/programmfabrik/fylr/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4f/l31kb4n521d6g89qxrwcx69h0000gp/T/go-build343164864=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Playground

package main

import (
	"context"
	"io"
	"os/exec"
	"time"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()
	cmd := exec.CommandContext(ctx, "sleep", "2")
	cmd.Stdin, _ = io.Pipe() // This prevents the "Timeout" to work correctly
	cmd.Run()                // Wrongly gets killed with "deadlock" detected
}

What did you expect to see?

I expect my program to not deadlock, but instead kill "sleep" and return from "Run" with an error. The unclosed STDIN should not prevent the program from returning.

Related or DUP?

Issue #23019 might be related to this?

What did you see instead?

The program deadlocks which is not what I expect.

@fatelei
Copy link

fatelei commented Sep 8, 2020

In golang, the deadlock is detected when there is only main goroutine running

@martinrode
Copy link
Author

In golang, the deadlock is detected when there is only main goroutine running

Nope, comment the cmd.Stdin line, and see how the program works.

@andybons
Copy link
Member

andybons commented Sep 8, 2020

@bradfitz @ianlancetaylor

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 8, 2020
@andybons andybons added this to the Unplanned milestone Sep 8, 2020
@ianlancetaylor ianlancetaylor changed the title os/exec: Timeout (and unclosed Stdin) not working correctly runtime: incorrect "fatal error: all goroutines are asleep - deadlock!" Sep 8, 2020
@ianlancetaylor ianlancetaylor modified the milestones: Unplanned, Go1.16 Sep 8, 2020
@ianlancetaylor ianlancetaylor changed the title runtime: incorrect "fatal error: all goroutines are asleep - deadlock!" os/exec: incorrect "fatal error: all goroutines are asleep - deadlock!" Sep 8, 2020
@ianlancetaylor
Copy link
Member

This is an interesting case. It's related to #23019, certainly. Because you used io.Pipe rather than, say, os.Pipe, the Go runtime knows for sure that nothing is ever going to write to that pipe. So the os/exec package is waiting for something to write to the pipe, and the runtime knows that that will never happen. So the runtime is correctly reporting a deadlock.

I'm going to close this as a dup of #23019, because if we fix that problem, we will fix this one. And if we don't fix that problem, then I don't think there is anything we can do about this one.

@golang golang locked and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants