diff --git a/typing/env.ml b/typing/env.ml index 54db4b2aa..74914145e 100644 --- a/typing/env.ml +++ b/typing/env.ml @@ -788,18 +788,23 @@ let save_signature sg modname filename = components_of_module empty Subst.identity (Pident(Ident.create_persistent modname)) (Tmty_signature sg) in let oc = open_out_bin filename in - output_string oc cmi_magic_number; - output_value oc (modname, sg); - flush oc; - let crc = Digest.file filename in - let crcs = (modname, crc) :: imported_units() in - output_value oc crcs; - close_out oc; - (* Enter signature in persistent table so that imported_unit() - will also return its crc *) - let ps = - { ps_name = modname; ps_sig = sg; ps_comps = comps; ps_crcs = crcs } in - Hashtbl.add persistent_structures modname ps + try + output_string oc cmi_magic_number; + output_value oc (modname, sg); + flush oc; + let crc = Digest.file filename in + let crcs = (modname, crc) :: imported_units() in + output_value oc crcs; + (* Enter signature in persistent table so that imported_unit() + will also return its crc *) + let ps = + { ps_name = modname; ps_sig = sg; ps_comps = comps; ps_crcs = crcs } in + Hashtbl.add persistent_structures modname ps; + close_out oc + with exn -> + close_out oc; + remove_file filename; + raise exn (* Make the initial environment *)