doufei4923 2019-08-07 23:44
浏览 40
已采纳

去所有goroutine都陷入僵局

Can't seem to figure out why I'm getting the error message: fatal error: all goroutines are asleep - deadlock!.

I suspected a race condition was occurring in my block below which should only execute after the channel is closed.

I thought adding a sync WaitGroup would help, but it's only given me this deadlock. What I have looks close to the samples I've seen online, so I'm just not sure what's wrong here.

func S3UploadFolder(instance *confighelper.Instance, sess *session.Session, 
    srcFolder string, bucketName string) (err error) {

    log.Println("S3UploadFolder", srcFolder, bucketName)

    wg := &sync.WaitGroup{}

    // find files recursively
    walker := make(fileWalk)
    wg.Add(1)

    go func() {

        // Gather the files to upload by walking the path recursively
        if err := filepath.Walk(srcFolder, walker.Walk); err != nil {
            log.Fatalln("Walk failed:", err)
        }
        wg.Done()
        close(walker)

    }()
    wg.Wait()

    for path := range walker {
    // THE GO routine above needs to have finished by the time this for loop 
       // ranges over the channel
         fmt.Println(path)

 }



return
}


type fileWalk chan string

func (f fileWalk) Walk(path string, info os.FileInfo, err error) error {
    if err != nil {
        return err
    }
    if !info.IsDir() {
        f <- path
    }
    return nil
}
  • 写回答

2条回答 默认 最新

  • dongwen1909 2019-08-08 00:36
    关注

    The walker channel is unbuffered. Communication on an unbuffered channel does not proceed until until a sender and receiver are ready.

    The deadlock is this: The main goroutine waits on the walker goroutine to complete by calling wg.Done(). The walker goroutine waits on the main goroutine to receive on the channel.

    Fix the program by removing all code related to the wait group. The wait group is not needed. Range over the channel in the main goroutine does not complete until the channel is closed by the walker goroutine. The walker goroutine does not close the channel until the walk is complete. No other coordination is required.

    You can also fix the code by removing the goroutines and channels:

    err := filepath.Walk(srcFolder, func(path string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }
        if info.IsDir() {
            return nil
        }
    
        // Insert body of for path := range walker here ... 
        fmt.Println(path)
    
        return nil
    })
    if err != nil {
        log.Fatal(err)
    }
    

    Another option is to create a buffered channel with capacity greater than the number of files that will be walked, but this requires knowing the number of files in advance and provides no benefit over collecting the file names in a slice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真