Skip to content

Commit f214e37

Browse files
committed
Fix ref-counting error in example
The restorer table takes ownership of the reference (back-port from Eio branch).
1 parent 353fcae commit f214e37

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

examples/sturdy-refs-3/logger.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ let rec local ~services sr label =
2323
let id = Capnp_rpc_net.Restorer.Id.generate () in
2424
let sr = Capnp_rpc_net.Restorer.Table.sturdy_ref services id in
2525
let sub = local ~services sr (Printf.sprintf "%s/%s" label sub_label) in
26-
Capnp_rpc_net.Restorer.Table.add services id sub;
2726
let response, results = Service.Response.create Results.init_pointer in
2827
Results.logger_set results (Some sub);
29-
Capability.dec_ref sub;
28+
Capnp_rpc_net.Restorer.Table.add services id sub; (* Takes ownership of [sub] *)
3029
Service.return response
30+
31+
method! pp f =
32+
Fmt.pf f "Logger(%s)" label
3133
end
3234

3335
module Logger = Api.Client.Logger

examples/sturdy-refs-3/main.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ let or_fail = function
1414
| Ok x -> x
1515
| Error (`Msg m) -> failwith m
1616

17-
let start_server () =
17+
let start_server ~switch () =
1818
let config = Capnp_rpc_unix.Vat_config.create ~secret_key listen_address in
1919
let make_sturdy = Capnp_rpc_unix.Vat_config.sturdy_uri config in
2020
let services = Restorer.Table.create make_sturdy in
21+
Lwt_switch.add_hook (Some switch) (fun () -> Restorer.Table.clear services; Lwt.return_unit);
2122
let restore = Restorer.of_table services in
2223
(* $MDX part-begin=root *)
2324
let root_id = Capnp_rpc_unix.Vat_config.derived_id config "root" in
@@ -27,18 +28,20 @@ let start_server () =
2728
in
2829
(* $MDX part-end *)
2930
Restorer.Table.add services root_id root;
30-
Capnp_rpc_unix.serve config ~restore >|= fun _vat ->
31+
Capnp_rpc_unix.serve ~switch config ~restore >|= fun _vat ->
3132
Capnp_rpc_unix.Vat_config.sturdy_uri config root_id
3233

3334
let run_client cap_file =
34-
let vat = Capnp_rpc_unix.client_only_vat () in
35+
Lwt_switch.with_switch @@ fun switch ->
36+
let vat = Capnp_rpc_unix.client_only_vat ~switch () in
3537
let sr = Capnp_rpc_unix.Cap_file.load vat cap_file |> or_fail in
3638
Sturdy_ref.with_cap_exn sr @@ fun for_alice ->
3739
Logger.log for_alice "Message from Alice"
3840

3941
let () =
4042
Lwt_main.run begin
41-
start_server () >>= fun root_uri ->
43+
Lwt_switch.with_switch @@ fun switch ->
44+
start_server ~switch () >>= fun root_uri ->
4245
let vat = Capnp_rpc_unix.client_only_vat () in
4346
let root_sr = Capnp_rpc_unix.Vat.import vat root_uri |> or_fail in
4447
Sturdy_ref.with_cap_exn root_sr @@ fun root ->

examples/sturdy-refs-4/logger.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ let local ~persist_new sr label =
3131
Capability.dec_ref logger;
3232
Ok response
3333
(* $MDX part-end *)
34+
35+
method! pp f =
36+
Fmt.pf f "Logger(%s)" label
3437
end
3538

3639
module Logger = Api.Client.Logger

0 commit comments

Comments
 (0)