Skip to content

GC does not collect cycles with finalizers #2368

@dvyukov

Description

@dvyukov
> What steps will reproduce the problem?

package main

import "runtime"

func main() {
    c := make(chan bool)
    func() {
        type X struct {
            next *X
        }
        fin := func(_ *X) {
            c <- true
        }

        x := new(X)
        x.next = x
        runtime.SetFinalizer(x, fin)

        x1 := new(X)
        x2 := new(X)
        x1.next = x2
        x2.next = x1
        runtime.SetFinalizer(x1, fin)
        runtime.SetFinalizer(x2, fin)
    }()

    go func() {
        for {
            runtime.GC()
            runtime.Gosched()
        }
    }()

    <-c
    <-c
    <-c
}

> What is the expected output? What do you see instead?

The program must terminate. The program hangs.

> Please use labels and text to provide additional information.

gc/linux/amd64
10044:c587bcb93290 tip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions