Fork me on GitHub
#core-async
<
2017-05-21
>
ghaskins01:05:46

hi all, im not sure if this is a core.async issue or a clojurescript/nodejs issue, but I am struggling to figure out the best way to shut down an app that has at least one (timeout) pending

ghaskins02:05:22

in a nutshell, I have an (alt!) that waits on two channels: one of which is a core.async/timeout

ghaskins02:05:06

but I find that my program hangs at shutdown until the timeout eventually fires...which is the common scenario because the timeout just an exceptional condition

ghaskins02:05:28

so, i am wondering if there are some best practices here, or other advice...for instance, canceling the timeout, telling core.async to shutdown, a way to wait on a channel with a timeout, etc

noisesmith02:05:30

the way to wait on a channel with a timeout is to use alt! or alts!

noisesmith02:05:49

if your channel has a value before the timeout does, that's the value you will get

ghaskins02:05:10

right, understood...i think i am explaining poorly

ghaskins02:05:30

my (alt!) works as expected, but then my program cannot exit until the timer also fires

noisesmith02:05:45

oh, add a third channel - called a poison-pill

noisesmith02:05:51

close that channel when you want to exit

noisesmith02:05:53

but never write to it

noisesmith02:05:15

a closed channel returns nil immediately, so if it is one of your alts it will fire

ghaskins02:05:52

hmm, the problem is not with the (alt!) firing...the logic is part of a command line app....so start -> (alt!) -> exit

ghaskins02:05:23

the (alt!) is written something like (alt! normal-ch timeout-ch)

ghaskins02:05:35

normally, the normal-ch fires first

ghaskins02:05:52

but the program hangs until an interval approximately equal to the timeout

noisesmith02:05:07

what is it waiting for then?

ghaskins02:05:11

its not hanging in the (alt!), but in some seeming system shutdown

noisesmith02:05:40

in clojure I would say you need to run (shutdown-agents) but I don't think that applies in node

ghaskins02:05:55

yeah, it feels like that kind of problem

ghaskins02:05:00

but I am not sure what the node solution is

noisesmith02:05:02

if the app is waiting on a timeout that you are not reading, that sounds like a bug to me

noisesmith02:05:24

but, what if you call close! on the timeout channel?

ghaskins02:05:49

i tried that (even though the doc comments said "dont") but it had no effect

noisesmith02:05:40

using close! on a timeout works in my repl - let me double check in a node repl

ghaskins02:05:21

it "feels" like node is still seeing something with active callbacks in the event loop, or whatever (not a node-internals expert)

ghaskins02:05:33

ala it needs a (shutdown-agents) whack

ghaskins02:05:37

I can probably write this a half dozen other ways, its just seemed like (alt!)+(timeout) was the idiomatic way to do it

noisesmith02:05:21

just curious, where did you see the comment not to call close! on a timeout?

ghaskins02:05:56

see commands at bottom

ghaskins02:05:02

comments, not commands

ghaskins02:05:16

granted, its a user comment, not the official doc

ghaskins02:05:31

but it seemed legit

noisesmith02:05:39

but if you want your whole app to exit, that caveat doesn't apply

noisesmith02:05:48

too bad that it does that though

ghaskins02:05:49

yeah, thats why I went for it anyway

ghaskins02:05:27

i wasnt 100% sure it wouldnt have a reentrancy problem, but I think for my use case, that would have been ok

ghaskins02:05:34

but like I said, it seemed to have no impact

ghaskins02:05:31

I suspect there aren't a whole bunch of people writing cljs+node command line apps

ghaskins02:05:39

thats probably part of the problem

ghaskins02:05:40

just based on my experience getting them working