Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Burak Serdar
On Wed, Jul 10, 2019 at 3:24 PM Roman Kuprov wrote: > > Would the Context package not work for this issue? > (kinda new gopher here) It is similar to using a cancel channel. Context communicates the cancellation by closing a channel returned by Done(). > > On Wednesday, July 10, 2019 at 1:08:38

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Roman Kuprov
Would the Context package not work for this issue? (kinda new gopher here) On Wednesday, July 10, 2019 at 1:08:38 PM UTC-6, Dan Eloff wrote: > > Yeah, agreed. I've been deep into concurrent programming for a long time > now, and into lock-free programming as well which is the most fraught kind

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Dan Eloff
Yeah, agreed. I've been deep into concurrent programming for a long time now, and into lock-free programming as well which is the most fraught kind of programming I've ever done. Parallel is the future, it has been that way for a long time, but it's only getting more and more obvious. I think in

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Jesper Louis Andersen
On Wed, Jul 10, 2019 at 6:30 PM wrote: > > The best thing to do is to not use unmanaged resources. > Would be the Erlang solution. You cannot change the owner of an open file, and if the process owning it terminates, the file is closed. You can change ownership of a TCP socket, and then

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Jesper Louis Andersen
On Wed, Jul 10, 2019 at 6:45 PM Dan Eloff wrote: > On Wed, Jul 10, 2019 at 7:54 AM Michael Jones > wrote: > >> unbuffered means nothing is sent until is is simultaneously received, so >> there is no limbo or race or uncertainty. one sender "wins" the select and >> the others remain blocked

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Burak Serdar
On Wed, Jul 10, 2019 at 10:45 AM Dan Eloff wrote: > > On Wed, Jul 10, 2019 at 7:54 AM Michael Jones wrote: >> >> unbuffered means nothing is sent until is is simultaneously received, so >> there is no limbo or race or uncertainty. one sender "wins" the select and >> the others remain blocked

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Dan Eloff
On Wed, Jul 10, 2019 at 7:54 AM Michael Jones wrote: > unbuffered means nothing is sent until is is simultaneously received, so > there is no limbo or race or uncertainty. one sender "wins" the select and > the others remain blocked waiting. > So I'm correct then: "Now one of two things must

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread jake6502
On Tuesday, July 9, 2019 at 12:27:32 PM UTC-4, Dan Eloff wrote: > > I couldn't use <-channel.Close since in my case the goroutine isn't > guaranteed to have something sent, so that would leak goroutines. I added a > cleanup goroutine to scan timed-out channels and close them, which solves >

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Michael Jones
unbuffered means nothing is sent until is is simultaneously received, so there is no limbo or race or uncertainty. one sender "wins" the select and the others remain blocked waiting. On Wed, Jul 10, 2019 at 6:24 AM Dan Eloff wrote: > Maybe I'm wrong here in my understanding of unbuffered

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Dan Eloff
Maybe I'm wrong here in my understanding of unbuffered channels, but I don't think so: Matt says earlier: "Only a buffered channel can "hold" anything. If the channel is unbuffered, then you are guaranteed that another goroutine has at least received the item you sent when the send statement

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Dan Eloff
> > If the channel is unbuffered, there are two parties, S and R (Sender and > Receiver). If the channel is buffered, it is another party, C (channel). > The delivery chain is really S -> C -> R. Whereas in the unbuffered case, > rendezvous means an atomic exchange of the resource (S -> R).

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-10 Thread Jesper Louis Andersen
On Tue, Jul 9, 2019 at 6:14 AM Daniel Eloff 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

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-09 Thread Daniel Eloff
> > > And not only that, it's complicated. The language spec is not the > right place to dig into the complexities of how to use select safely > while avoiding race conditions. There is just too much to say. And > there are no docs for select other than the language spec. > > Well here's

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-09 Thread Ian Lance Taylor
On Tue, Jul 9, 2019 at 10:36 AM Daniel Eloff wrote: >> >> >> In my opinion the best place for this kind of discussion is a blog >> post or talk. > > > I disagree strongly. If there's a mode of operation that's dangerous when I'm > operating a car or machinery I want it to not just be called out

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-09 Thread Daniel Eloff
> > > In my opinion the best place for this kind of discussion is a blog > post or talk. > I disagree strongly. If there's a mode of operation that's dangerous when I'm operating a car or machinery I want it to not just be called out in the manual, but called attention to in a big bold

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-09 Thread Ian Lance Taylor
On Tue, Jul 9, 2019 at 9:27 AM Dan Eloff wrote: > > So I guess the best thing to do in these cases is don't combine select with > sending unmanaged resources over a channel. It's probably worth warning about > this problem in the docs for select? It's not an obvious gotcha. In my opinion the

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-08 Thread Ian Lance Taylor
On Mon, Jul 8, 2019 at 9:14 PM Daniel Eloff 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 >

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-08 Thread Burak Serdar
On Mon, Jul 8, 2019 at 10:54 PM Tamás Gulácsi wrote: > > // Wait on the channel, or for timeout > select { > case fd := <-channel: > return fd, nil > case <-time.After(queue.timeout): > // Check again > select { > case fd := <-channel: > return fd, nil >

[go-nuts] Avoiding resource leaks with racing channels

2019-07-08 Thread Tamás Gulácsi
// Wait on the channel, or for timeout select { case fd := <-channel: return fd, nil case <-time.After(queue.timeout): // Check again select { case fd := <-channel: return fd, nil default: return nil, ErrTimeoutElapsed } } -- You received this

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-08 Thread Matt Harden
Only a buffered channel can "hold" anything. If the channel is unbuffered, then you are guaranteed that another goroutine has at least received the item you sent when the send statement returns. On Mon, Jul 8, 2019 at 9:42 PM Burak Serdar wrote: > On Mon, Jul 8, 2019 at 10:14 PM Daniel Eloff

Re: [go-nuts] Avoiding resource leaks with racing channels

2019-07-08 Thread Burak Serdar
On Mon, Jul 8, 2019 at 10:14 PM Daniel Eloff 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 >

[go-nuts] Avoiding resource leaks with racing channels

2019-07-08 Thread Daniel Eloff
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