Skip to content
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

why deadlock? #32422

Closed
nnsgmsone opened this issue Jun 4, 2019 · 3 comments
Closed

why deadlock? #32422

nnsgmsone opened this issue Jun 4, 2019 · 3 comments

Comments

@nnsgmsone
Copy link

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

$ go version

go version go1.11.4 linux/amd64

Does this issue reproduce with the latest release?

yes

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

ubuntu18.04 amd64

go env Output
$ go env


GOARCH="amd64"
GOBIN="/home/robin/go/bin"
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/go"
GOPROXY=""
GORACE=""
GOROOT="/home/robin/go"
GOTMPDIR=""
GOTOOLDIR="/home/robin/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build746074640=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I run the following code:

package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {
	go func() {
		for {
		}
	}()
	go func() {
		cnt := 1
		for {
			time.Sleep(100 * time.Millisecond)
			cnt = cnt + 1
			fmt.Println(cnt)
			if cnt == 10 {
				runtime.GC()
			}
		}
	}()
	ch := make(chan struct{})
	<-ch
}

What did you expect to see?

I hope to see the number of non-stop output increases.

What did you see instead?

When cnt is 10, the program will be deadlock

my confusion

Why do infinite loops and gc lead to deadlocks, and can't the dead loop be seized?

@nnsgmsone
Copy link
Author

The following code will still be deadlock:

package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {
	go func() {
		cnt := 1
		for {
			cnt++
		}
	}()
	go func() {
		cnt := 1
		for {
			time.Sleep(100 * time.Millisecond)
			cnt = cnt + 1
			fmt.Println(cnt)
			if cnt == 10 {
				runtime.GC()
			}
		}
	}()
	ch := make(chan struct{})
	<-ch
}

The following code will not:

package main

import (
	"encoding/base64"
	"fmt"
	"runtime"
	"time"
)

func main() {
	go func() {
		for {
			base64.StdEncoding.EncodeToString([]byte("a"))
		}
	}()
	go func() {
		cnt := 1
		for {
			time.Sleep(100 * time.Millisecond)
			cnt = cnt + 1
			fmt.Println(cnt)
			if cnt == 10 {
				runtime.GC()
			}
		}
	}()
	ch := make(chan struct{})
	<-ch
}

According to the phenomenon, the goroutine that call other packages will be preempted. why? Is it a deliberate design or a bug?

@ianlancetaylor
Copy link
Member

See the discussion in #10958.

@nnsgmsone
Copy link
Author

@ianlancetaylor thank you

@golang golang locked and limited conversation to collaborators Jun 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants