Skip to content

Commit

Permalink
Merge pull request #28 from gaborigloi/cleanup
Browse files Browse the repository at this point in the history
Cleanup and documentation for CA-273309
  • Loading branch information
mseri authored Nov 28, 2017
2 parents c2cf6d1 + e2ce292 commit cace624
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ env:
- OCAML_VERSION=4.04.2
- DISTRO=debian-stable
- PACKAGE=xapi-nbd
- PINS="nbd:git://github.com/xapi-project/nbd.git#bugfix-v2.x xen-api-client:git://github.com/xapi-project/xen-api-client#feature/CBT xen-api-client-lwt:git://github.com/xapi-project/xen-api-client#feature/CBT xen-api-client-async:git://github.com/xapi-project/xen-api-client#feature/CBT"
matrix:
- BASE_REMOTE=git://github.com/xapi-project/xs-opam
- EXTRA_REMOTES=git://github.com/xapi-project/xs-opam
Expand Down
8 changes: 8 additions & 0 deletions src/cleanup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ module Block = struct
end

module Runtime = struct
(* Exceptions raised from signal handlers are not caught by the Lwt.finalize
functions unfortunately, therefore just raising an exception from the
signal handlers is not enough: We need to use a global reference instead
to collect the things to be cleaned up, and add cleanup code to the signal
handlers. It is still necessary to raise an exception from the signal
handlers to stop the program.
See https://github.com/ocsigen/lwt/issues/451 for details. *)

let cleanup_resources signal =
let cleanup () =
Lwt_log.warning_f "Caught signal %d, cleaning up" signal >>= fun () ->
Expand Down
6 changes: 3 additions & 3 deletions src/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ let handle_connection fd tls_role =
let vdi_uuid = if path <> "" then String.sub path 1 (String.length path - 1) else path in
Xen_api.VDI.get_by_uuid ~rpc ~session_id ~uuid:vdi_uuid
>>= fun vdi_ref ->
Xen_api.VDI.get_record ~rpc ~session_id ~self:vdi_ref
>>= fun vdi_rec ->
with_attached_vdi vdi_ref rpc session_id
(fun filename ->
Cleanup.Block.with_block filename (Nbd_lwt_unix.Server.serve t ~read_only:true (module Block))
Expand Down Expand Up @@ -219,7 +217,9 @@ let setup_logging () =
let () =
(* We keep track of the VBDs we've created but haven't yet cleaned up, and
when we receive a SIGTERM or SIGINT signal, we clean up these leftover
VBDs first and then fail with an exception. *)
VBDs first and then fail with an exception.
This means that when systemd stops the service with SIGTERM, it will be
in the failed state, and a backtrace will show up in the logs. *)
Cleanup.Runtime.register_signal_handler ();
setup_logging ();
match Term.eval cmd with
Expand Down

0 comments on commit cace624

Please sign in to comment.