简体   繁体   English

此功能可能导致goroutine泄漏吗

[英]Is this func possible to cause goroutine leak

func startTimer(ctx context.Context, intervalTime int) {
    intervalChan := make(chan bool) 
    go func() {
        for {
            select {
            case <-ctx.Done():
                return
            case <-time.After(time.Second * time.Duration(intervalTime)):
                intervalChan <- true
            }
        }
    }()


    for {
        select {
        case <-ctx.Done():
            return
        case <-intervalChan:
            doSomething()
    }
}

Hi,I write a func as above and want to know is it possible to cause goroutine leak. 嗨,我如上所述编写了一个func,想知道是否有可能导致goroutine泄漏。

For example, the first select statement sends a true to intervalChan, then the second select statement receives Done flag from ctx.Done() and return. 例如,第一个选择语句将true发送给intervalChan,然后第二个选择语句从ctx.Done()接收Done标志并返回。 Will the goroutine be block forever? goroutine会永远被封锁吗?

I cannot replicate this behaviour every time but could be some leak. 我无法每次都复制此行为,但可能会出现一些泄漏。 If doSomething do some heavy computation, meanwhile goroutine is blocked on intervalChan <- true since it cannot push into the channel. 如果doSomething做一些繁重的计算,则goroutine在intervalChan <- true -true上被阻塞,因为它无法推送到通道中。 After doSomething finish execution and context was cancelled, startTimer exists before goroutine and this will lead into blocked goroutine, because there isn't any consumer of intervalChan . doSomething完成执行并且取消了上下文之后,startTimer在goroutine之前存在,这将导致阻塞的goroutine,因为没有intervalChan使用者。

go version go1.8.3 darwin/amd64

package main

import (
    "context"
    "fmt"
    "time"
)

func startTimer(ctx context.Context, intervalTime int) {
    intervalChan := make(chan bool)
    go func() {
        for {
            select {
            case <-ctx.Done():
                fmt.Println("Done from inside of goroutine.")
                return
            case <-time.After(time.Second * time.Duration(intervalTime)):
                fmt.Println("Interval reached.")
                intervalChan <- true
            }
        }
    }()

    for {
        select {
        case <-ctx.Done():
            fmt.Println("Done from startTimer.")
            return
        case <-intervalChan:
            time.Sleep(10 * time.Second)
            fmt.Println("Done")
        }
    }
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    startTimer(ctx, 2)
}

The only place your first goroutine could be blocked indefinitely is in intervalChan <- true . 您的第一个goroutine唯一可以无限期阻止的地方是intervalChan <- true Put it in another select block to be able to cancel that send: 将其放在另一个选择块中可以取消该发送:

go func() {
    for {
        select {
        case <-ctx.Done():
            return
        case <-time.After(time.Second * time.Duration(intervalTime)):
            select {
            case <-ctx.Done():
                return
            case intervalChan <- true:
            }
        }
    }
}()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM