Eio - attach a handler between the fn finishes and forks are awaited

Hi, we have an Eio based http2/grpc client. Let’s assume we are creating a tcp socket in a switch, and when that switch finishes we want to wait for all writes to finish and write a Goaway to the other side, something analogous to triggering a cleanup on Sigint for the entire program. That said we want to leave the TCP socket open during that time.

It seems like there’s no natural way to handle it in eio.

I would want something like this:


Eio.Switch.run @@ fun sw ->
let socket = Eio.Net.connect ~sw ... in
run_client socket;
run_client socket

And after the second run_client returns (that is switch’s fn returns) I want to wait for the writes to finish and maybe do some cleanup before the socket is closed. The socket is closed in on_release.

Am I seeing correctly that the behavior I’m thinking about here is impossible to implement with the current APIs? Could we simply add another list of callbacks to switche’s state like before_release or whatever and put it here?

I am aware I can use a mixture of a child Switch and Cancel.protect to sort of achieve this but this puts a lot of burden on the users of my API and makes the entire program much more complex.

In the example, you’re not passing the switch to run_client, so all writes must have finished by the time it returns anyway.

If the client spawns a separate writer fiber internally, it will do so using its own switch, which must have finished by the time it returns.

If you wanted the client to spawn writers that outlive run_client, you could use a second switch for that, e.g.

Eio.Switch.run @@ fun sw ->
let socket = Eio.Net.connect ~sw ... in
Eio.Switch.run (fun sw ->
  run_client socket ~sw;
  run_client socket ~sw
);
(* Send goodbye here *)