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

runtime: bad "deadlock" fatal error when there are pending finalizers #41362

Open
mdempsky opened this issue Sep 13, 2020 · 0 comments
Open

runtime: bad "deadlock" fatal error when there are pending finalizers #41362

mdempsky opened this issue Sep 13, 2020 · 0 comments

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Sep 13, 2020

This program usually succeeds, but sometimes it fails with "fatal error: all goroutines are asleep - deadlock!":

package main

import "runtime"

func main() {
	ch := make(chan bool)

	x := [10]int{4}
	var f func(*int)
	f = func(p *int) {
		if *p == 0 {
			close(ch)
			return
		}
		*p--
		runtime.SetFinalizer(p, f)
	}
	runtime.SetFinalizer(&x[0], f)
	
	runtime.GC()
	runtime.GC()
	runtime.GC()
	runtime.GC()
	runtime.GC()

	<-ch
}

Adding an extra runtime.GC() call makes it reliably* succeed. Removing any makes it reliably* fail. (* Based on 1000 runs.)

I think the program should reliably succeed, even if all runtime.GC() calls are removed. That is, when the runtime scheduler notices there aren't any runnable goroutines, it should keep running GC cycles until a cycle finishes without running any user finalizers; and only then should it report deadlock if there still aren't any runnable goroutines.

I don't know of any real-world programs this affects, so maybe this isn't a high priority if it's difficult to address completely. But it would make writing robust compiler test cases somewhat easier (e.g., see golang.org/cl/254401).

/cc @aclements @ianlancetaylor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.