Async: Fd not getting closed?

15 views
Skip to first unread message

Malcolm Matalka

unread,
Oct 11, 2012, 4:29:34 PM10/11/12
to ocaml...@googlegroups.com
I have some dirty code here: http://ideone.com/WG9mN

To compile:

ocamlfind ocamlopt -package core,async -thread -linkpkg -o foo foo.ml

The problem is the 'wait' never seems to come back. This appears to be
because the cat is still waiting on reading stdin. But the stdin
should be closed on line 77, shouldn't it?

What am I doing wrong?

Thanks!

/M

David House

unread,
Oct 11, 2012, 4:48:00 PM10/11/12
to ocaml...@googlegroups.com
Some general comments first:

* read_all seems to be trying to achieve the same thing as Reader.contents

* Is there any reason why you're using Core's waitpid function over
Async.Std.Unix.wait?

* Ivar.read (background foo) is equivalent to just foo, and you only
ever use the result of background inside an Ivar.read, so I think
background is pointless.

It's not immediately apparent where the actual bug is. One thing to
ask: do you get past the wait call, or are you getting stuck inside
one of the Deferred.both's? I.e. if you put a printf straight after
wait, do you see it? (Best to use [Printf.printf "foo\n%!"]; the
Printf module avoids going via Writer's buffer in case something weird
is going on, and you should always flush your debugging statements
with "%!"!)

Malcolm Matalka

unread,
Oct 11, 2012, 5:00:25 PM10/11/12
to ocaml...@googlegroups.com
Yes the code is in transition as I learn more about Async, so take all
non essentials with a grain of salt.

I switched the wait function to:

let wait pi =
Unix.wait (`Pid (Core.Std.Pid.of_int pi.pid)) >>= function
| (_, Result.Ok ()) ->
Deferred.return (`Exited 0)
| (_, Result.Error (`Exit_non_zero n)) ->
Deferred.return (`Exited n)
| (_, _) ->
Deferred.return `Unknown


with no change in behaviour.

I do not get past the wait call and a ps shows the cat is still
running. This function works properly for a program that doesn't read
from stdin such as an ls, which is what leads me to think somehow
stdin is not getting closed properly.

Thanks for the quick response,
/M
Reply all
Reply to author
Forward
0 new messages