[Haskell-cafe] Wrapping external process communication in a nice API?

Mario Lang mlang at blind.guru
Mon May 31 17:03:50 UTC 2021

Tristan Cacqueray <tdecacqu at redhat.com> writes:

> On Mon, May 31, 2021 at 17:31 Mario Lang wrote:
>> Tristan Cacqueray <tdecacqu at redhat.com> writes:
>>> It seems like it would be safer to use withCreateProcess instead of
>>> createProcess to ensure the process is not left unattended.
>> Maybe I am unimaginative, but it seems to me I can not use
>> withCreateProcess as I need to read/write the Handles outside of the
>> initialisation phase.  It seems to me withCreateProcess would kill my
>> external process once my initialisation function is done.
>> Or do I misunderstand bracket somehow?
>> AIUI, withCreateProcess always calls cleanupProcess once the action is
>> done, right?
> I guess you would have to replace `start'` with something like:
> ```haskell
> withEngine :: Time unit -> ... -> (Engine -> IO ()) -> IO ()
> ```
> And let your user provide the `Engine -> IO ()` callback.

Ahh, of course, push the problem up the caller chain :-)
And I guess if that caller needs a sort of handle, it would do it with a
channel to communicate with its action?
I know I was asking for idiomatic code, and I am thankful for
the pointer.  Exception safety is clearly a good goal. But, I wonder, is
limiting the API in such a way worth the trouble?
It looks like this change would trigger a complete rewrite of
the current module and all its clients.
I'd like to be confident this is the right thing to do before doing that.

> Alternatively, perhaps the `managed` library can be used to get
> something closer to your existing `start'`, e.g.:
> http://hackage.haskell.org/package/managed-1.0.8/docs/Control-Monad-Managed.html

Ahh, thanks again.  I'll have to digest all of this.


More information about the Haskell-Cafe mailing list