I have the following simple OCaml Async job which is supposed to write to a file and terminate the process.
Unix.openfile "foobar" ~mode:[`Creat;`Rdwr]>>= fun fd ->let wr = Writer.create fd inWriter.write wr "this is a test";Unix.close fd>>= fun () ->exit 0
However, it seems that fd
gets closed before the write is performed (part of displayed sexp is "writer fd unexpectedly closed"). Is there a way to wait for write to complete before closing the file descriptor? Actually, I don't understand why Writer.write
doesn't return a Deferred.t
just as Reader.read
does. Wouldn't it solve the issue?
My problem is actually a little more general. Basically, I have a periodic job that writes something to a file using Clock.every'
. The program can exit at any time and close the file descriptors. How to make sure that all the writes are processed before the fd
get closed?
If my stopping job is:
Unix.close fd>>= fun () ->exit 0
A scheduled write can very well happen between Unix.close fd
and exit 0
.