caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Exceptions and Gc.
@ 2017-03-20 19:22 Romain Beauxis
  2017-03-21 11:17 ` Dmitry Bely
  0 siblings, 1 reply; 6+ messages in thread
From: Romain Beauxis @ 2017-03-20 19:22 UTC (permalink / raw)
  To: OCaml List

[-- Attachment #1: Type: text/plain, Size: 859 bytes --]

Hi guys,

Apologies if I'm beating a dead horse but I think I've never wondered about
this: What are the assumptions related to the garbage collector when an
exception is raised?

I sort-of always assumed it would be run but it doesn't seem that this is
the case. This code:
let () =
  let g () =
    let f _ =
      Printf.printf "Collecting x\n%!"
    in
    let x = Bytes.create 1024 in
    Gc.finalise f x;
    ()
  in
  g ();
  Gc.full_major();
  raise Not_found

Shows that the finalization function is never call if I remove the call to
full_major. Any reason for that?

The reason I'm asking if that I know I've been writing C bindings where
some cleanup operations are wrapped up in the finalization code with the
expectation that, except for a hard crash, it would always be executed at
some point in the future..

Thanks for y'all comments!
Romain

[-- Attachment #2: Type: text/html, Size: 1194 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Exceptions and Gc.
  2017-03-20 19:22 [Caml-list] Exceptions and Gc Romain Beauxis
@ 2017-03-21 11:17 ` Dmitry Bely
  2017-03-21 11:58   ` Max Mouratov
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Bely @ 2017-03-21 11:17 UTC (permalink / raw)
  Cc: OCaml List

It has nothing to do with exceptions. The problem is that OCaml
runtime does not execute the garbage collector on program exit. But
you can write

let _ = at_exit Gc.full_major

if you need to force GC.

On Mon, Mar 20, 2017 at 10:22 PM, Romain Beauxis
<romain.beauxis@gmail.com> wrote:
> Hi guys,
>
> Apologies if I'm beating a dead horse but I think I've never wondered about
> this: What are the assumptions related to the garbage collector when an
> exception is raised?
>
> I sort-of always assumed it would be run but it doesn't seem that this is
> the case. This code:
> let () =
>   let g () =
>     let f _ =
>       Printf.printf "Collecting x\n%!"
>     in
>     let x = Bytes.create 1024 in
>     Gc.finalise f x;
>     ()
>   in
>   g ();
>   Gc.full_major();
>   raise Not_found
>
> Shows that the finalization function is never call if I remove the call to
> full_major. Any reason for that?
>
> The reason I'm asking if that I know I've been writing C bindings where some
> cleanup operations are wrapped up in the finalization code with the
> expectation that, except for a hard crash, it would always be executed at
> some point in the future..
>
> Thanks for y'all comments!
> Romain

- Dmitry Bely

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Exceptions and Gc.
  2017-03-21 11:17 ` Dmitry Bely
@ 2017-03-21 11:58   ` Max Mouratov
  2017-03-21 15:07     ` Romain Beauxis
  0 siblings, 1 reply; 6+ messages in thread
From: Max Mouratov @ 2017-03-21 11:58 UTC (permalink / raw)
  To: Romain Beauxis, Dmitry Bely; +Cc: caml-list

Tuesday, March 21, 2017, 12:22:54 AM, Romain wrote:
> The reason I'm asking if that I know I've been writing C bindings
> where some cleanup operations are wrapped up in the finalization
> code with the expectation that, except for a hard crash, it would
> always be executed at some point in the future..

Tuesday, March 21, 2017, 4:17:05 PM, Dmitry wrote:
> It has nothing to do with exceptions. The problem is that OCaml
> runtime does not execute the garbage collector on program exit. But
> you can write
> let _ = at_exit Gc.full_major
> if you need to force GC.

It won't guarantee running all finalisers, as some of the objects may
still be reachable. As part of a yet unmerged patch [1] that will land 
in 4.06, I have added an option that makes the runtime shut down 
properly on process exit (by an implicit call to the new caml_shutdown
function), but unfortunately it doesn't handle Gc.finalise yet, as the
relevant logic is not so trivial (and is probably a subject for a
different PR). However, custom blocks [2] are guaranteed to be
finalised properly with caml_shutdown, so you might look into this.

Links:
[1] https://github.com/ocaml/ocaml/pull/71
[2] https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html#sec442


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Exceptions and Gc.
  2017-03-21 11:58   ` Max Mouratov
@ 2017-03-21 15:07     ` Romain Beauxis
  2017-03-21 16:05       ` Gabriel Scherer
  2017-03-25 11:11       ` SP
  0 siblings, 2 replies; 6+ messages in thread
From: Romain Beauxis @ 2017-03-21 15:07 UTC (permalink / raw)
  To: Max Mouratov; +Cc: Dmitry Bely, caml-list

2017-03-21 6:58 GMT-05:00 Max Mouratov <max.mouratov@outlook.com>:
> Tuesday, March 21, 2017, 12:22:54 AM, Romain wrote:
>> The reason I'm asking if that I know I've been writing C bindings
>> where some cleanup operations are wrapped up in the finalization
>> code with the expectation that, except for a hard crash, it would
>> always be executed at some point in the future..
>
> Tuesday, March 21, 2017, 4:17:05 PM, Dmitry wrote:
>> It has nothing to do with exceptions. The problem is that OCaml
>> runtime does not execute the garbage collector on program exit. But
>> you can write
>> let _ = at_exit Gc.full_major
>> if you need to force GC.
>
> It won't guarantee running all finalisers, as some of the objects may
> still be reachable. As part of a yet unmerged patch [1] that will land
> in 4.06, I have added an option that makes the runtime shut down
> properly on process exit (by an implicit call to the new caml_shutdown
> function), but unfortunately it doesn't handle Gc.finalise yet, as the
> relevant logic is not so trivial (and is probably a subject for a
> different PR). However, custom blocks [2] are guaranteed to be
> finalised properly with caml_shutdown, so you might look into this.

Thanks guys.

That patch sounds great. I was just wondering since after all this
time writing OCaml I never thought about it nor actually ready
anything about this topic.

Being able to clean everything up when the program exits seems like a
reasonable feature.

Romain

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Exceptions and Gc.
  2017-03-21 15:07     ` Romain Beauxis
@ 2017-03-21 16:05       ` Gabriel Scherer
  2017-03-25 11:11       ` SP
  1 sibling, 0 replies; 6+ messages in thread
From: Gabriel Scherer @ 2017-03-21 16:05 UTC (permalink / raw)
  To: Romain Beauxis; +Cc: Max Mouratov, Dmitry Bely, caml-list

[-- Attachment #1: Type: text/plain, Size: 2045 bytes --]

By the way, the unloading patch is now merged. (In trunk; it should not be
part of the 4.05 release in preparation, but of 4.06 that should hopefully
happen six months after that.)

On Tue, Mar 21, 2017 at 4:07 PM, Romain Beauxis <romain.beauxis@gmail.com>
wrote:

> 2017-03-21 6:58 GMT-05:00 Max Mouratov <max.mouratov@outlook.com>:
> > Tuesday, March 21, 2017, 12:22:54 AM, Romain wrote:
> >> The reason I'm asking if that I know I've been writing C bindings
> >> where some cleanup operations are wrapped up in the finalization
> >> code with the expectation that, except for a hard crash, it would
> >> always be executed at some point in the future..
> >
> > Tuesday, March 21, 2017, 4:17:05 PM, Dmitry wrote:
> >> It has nothing to do with exceptions. The problem is that OCaml
> >> runtime does not execute the garbage collector on program exit. But
> >> you can write
> >> let _ = at_exit Gc.full_major
> >> if you need to force GC.
> >
> > It won't guarantee running all finalisers, as some of the objects may
> > still be reachable. As part of a yet unmerged patch [1] that will land
> > in 4.06, I have added an option that makes the runtime shut down
> > properly on process exit (by an implicit call to the new caml_shutdown
> > function), but unfortunately it doesn't handle Gc.finalise yet, as the
> > relevant logic is not so trivial (and is probably a subject for a
> > different PR). However, custom blocks [2] are guaranteed to be
> > finalised properly with caml_shutdown, so you might look into this.
>
> Thanks guys.
>
> That patch sounds great. I was just wondering since after all this
> time writing OCaml I never thought about it nor actually ready
> anything about this topic.
>
> Being able to clean everything up when the program exits seems like a
> reasonable feature.
>
> Romain
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 3038 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Exceptions and Gc.
  2017-03-21 15:07     ` Romain Beauxis
  2017-03-21 16:05       ` Gabriel Scherer
@ 2017-03-25 11:11       ` SP
  1 sibling, 0 replies; 6+ messages in thread
From: SP @ 2017-03-25 11:11 UTC (permalink / raw)
  To: caml-list

On 21/03/2017 15:07, Romain Beauxis wrote:
> Being able to clean everything up when the program exits seems like a
> reasonable feature.

Maybe the "appropriate" way with the current API is to have bindings
that deallocate the resources from the external heap, and only leave the
OCaml allocations (hopefully just pointers) to be collected later.

That way you can do any critical external finalisations at the exact
point you wish.

Would that work?

-- 
    SP

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-03-25 11:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-20 19:22 [Caml-list] Exceptions and Gc Romain Beauxis
2017-03-21 11:17 ` Dmitry Bely
2017-03-21 11:58   ` Max Mouratov
2017-03-21 15:07     ` Romain Beauxis
2017-03-21 16:05       ` Gabriel Scherer
2017-03-25 11:11       ` SP

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).