Is there any way to produce etcd watch chan be closed

108 views
Skip to first unread message

袁成若

unread,
Mar 25, 2022, 2:41:07 PM3/25/22
to golang-nuts
I met a problem about etcd watch channel. seems that be closed,  but i can not reproduce it.

like this: 

```
for {
     ach := etcdClientV3.Watch(context.Background(), "/test", clientv3.WithPrefix())
     for {
          select {
             case wch := <- ach {
                   fmt.Println("recv chan")    
             }
          }
     }
}
```
the program print recv chan all the time. but I cannot reproduce  it , Is there any way to reproduce it

Ian Lance Taylor

unread,
Mar 25, 2022, 2:45:48 PM3/25/22
to 袁成若, golang-nuts
It sounds like you are describing a problem with the
https://github.com/etcd-io/etcd/ package. I suggest that you ask
there.

Ian

Sam Hughes

unread,
Mar 25, 2022, 5:04:48 PM3/25/22
to golang-nuts
 As written above, if that's the main thread, it is guaranteed to freeze in a deadlock every time. I don't see a goroutine kicked off, so I'm assuming you're trying to run that on the main thread, and you will be 100%, always, forever stuck on the `case wch := <- ach {` line.

Channel reads are blocking, if there's nothing in the buffer. You might have omitted it for brevity, but so I read that as if it's on the main thread for the program. You want goroutines to block while awaiting a receivable channel, typically. If you're receiving on the main thread, you need to add a default case that will not block.. This is a good explanation of the issue: https://golangbyexample.com/select-default-case-go/

袁成若

unread,
Mar 27, 2022, 10:51:03 PM3/27/22
to Ian Lance Taylor, golang-nuts
Ok, thanks. 

Ian Lance Taylor <ia...@golang.org> 于2022年3月26日周六 02:45写道:
Reply all
Reply to author
Forward
0 new messages