On Mon, Jul 8, 2019 at 10:14 PM Daniel Eloff <dan.el...@gmail.com> wrote: > > If a select statement has multiple channels ready when it runs, then it will > choose one at a random. So if you fire something across a channel that holds > a resource, like an open file descriptor - you have no guarantees that the > other end of the channel receives it. The (possibly full) channel will get > garbage collected later and the resource will leak in that case. > > Some code that explains things better than my clumsy prose: > > Receiver: > // Wait on the channel, or for timeout > select { > case fd := <-channel: > return fd, nil > case <-time.After(queue.timeout): > return nil, ErrTimeoutElapsed > } > > Sender: > channel <- fd > > What happens when the timeout races with the channel send? I think it's > possible the select handles the timeout in that case and leaves the channel > containing a connection alone. > > Am I right that this is a problem? How might I fix this code?
Interesting problem. Maybe you shouldn't send file descriptors like this? One solution could be: case <- time.After(queue.timeout): close(channel) return nil, errTimeOutElapsed and recover from panic on the sending side. > > Thanks, > Dan > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/5cda1189-2761-4110-8e9e-1d7311fd97fe%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqoMh-4w%3DxuFD-1Rk_BOzdDgS%2BiHygNYDBw55me0Sg-eGQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.