unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#26503: Local variables reclaimed early vs. finalizers
@ 2017-04-14 21:56 Ludovic Courtès
  2017-04-19  8:00 ` Andy Wingo
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic Courtès @ 2017-04-14 21:56 UTC (permalink / raw)
  To: 26503

Hello!

Consider this code:

--8<---------------cut here---------------start------------->8---
(use-modules (system foreign))

(define %table
  (make-weak-value-hash-table))

(define %abort
  (dynamic-func "abort" (dynamic-link)))

(let ((ptr (make-pointer 123 %abort)))
  (display "hello\n")
  (gc))
--8<---------------cut here---------------end--------------->8---

Guile is free to collect ‘ptr’ when ‘gc’ is called since it has become
unreachable at that point; that’s what 2.2.0 does, as explained in
‘NEWS’.

However, there’s a finalizer here so collecting ‘ptr’ has an observable
side effect.  This side effect makes the semantic change visible: the
“expected” semantics would be that ‘ptr’ is not subject to GC while it’s
in scope.

(In 2.0 the finalizer is not called until ‘ptr’ is no longer in scope.)

I’m not sure this counts as a bug, but it’s certainly a pitfall when
working with finalizers and the FFI.

Thoughts?

Ludo’.





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

* bug#26503: Local variables reclaimed early vs. finalizers
  2017-04-14 21:56 bug#26503: Local variables reclaimed early vs. finalizers Ludovic Courtès
@ 2017-04-19  8:00 ` Andy Wingo
  2017-04-19  9:50   ` Ludovic Courtès
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Wingo @ 2017-04-19  8:00 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: 26503

ludo@gnu.org (Ludovic Courtès) writes:

> Consider this code:
>
> (use-modules (system foreign))
>
> (define %abort
>   (dynamic-func "abort" (dynamic-link)))
>
> (let ((ptr (make-pointer 123 %abort)))
>   (display "hello\n")
>   (gc))
>
> Guile is free to collect ‘ptr’ when ‘gc’ is called since it has become
> unreachable at that point; that’s what 2.2.0 does, as explained in
> ‘NEWS’.
>
> However, there’s a finalizer here so collecting ‘ptr’ has an observable
> side effect.  This side effect makes the semantic change visible: the
> “expected” semantics would be that ‘ptr’ is not subject to GC while it’s
> in scope.

This would indicate that the user has erroneous expectations ;-)

Note that here since (gc) is in tail position, ptr is in fact not
protected in any way, even given this mental model, though with a single
thread it may be that the collection actually happens later in 2.0 given
that finalizers are run by asyncs.  Also ptr is not protected during the
"display" either, in 2.0; in 2.0 this "let" reduces to "begin" under
peval since the ptr is not used.

> (In 2.0 the finalizer is not called until ‘ptr’ is no longer in scope.)
>
> I’m not sure this counts as a bug, but it’s certainly a pitfall when
> working with finalizers and the FFI.
>
> Thoughts?

For me, I don't think this is a bug.  Rather the contrary, as it's more
in spirit with safe-for-space principle that a continuation should only
keep alive those values that it uses; any other data should be available
for the GC to reclaim.

In any case, I think this manual section treats the problem adequately,
for me at least:

  https://www.gnu.org/software/guile/manual/html_node/Foreign-Object-Memory-Management.html

Would you like to add something there?

Andy





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

* bug#26503: Local variables reclaimed early vs. finalizers
  2017-04-19  8:00 ` Andy Wingo
@ 2017-04-19  9:50   ` Ludovic Courtès
  2017-04-19 12:23     ` Andy Wingo
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic Courtès @ 2017-04-19  9:50 UTC (permalink / raw)
  To: Andy Wingo; +Cc: 26503

Andy Wingo <wingo@pobox.com> skribis:

> ludo@gnu.org (Ludovic Courtès) writes:
>
>> Consider this code:
>>
>> (use-modules (system foreign))
>>
>> (define %abort
>>   (dynamic-func "abort" (dynamic-link)))
>>
>> (let ((ptr (make-pointer 123 %abort)))
>>   (display "hello\n")
>>   (gc))
>>
>> Guile is free to collect ‘ptr’ when ‘gc’ is called since it has become
>> unreachable at that point; that’s what 2.2.0 does, as explained in
>> ‘NEWS’.
>>
>> However, there’s a finalizer here so collecting ‘ptr’ has an observable
>> side effect.  This side effect makes the semantic change visible: the
>> “expected” semantics would be that ‘ptr’ is not subject to GC while it’s
>> in scope.
>
> This would indicate that the user has erroneous expectations ;-)
>
> Note that here since (gc) is in tail position, ptr is in fact not
> protected in any way, even given this mental model, though with a single
> thread it may be that the collection actually happens later in 2.0 given
> that finalizers are run by asyncs.  Also ptr is not protected during the
> "display" either, in 2.0; in 2.0 this "let" reduces to "begin" under
> peval since the ptr is not used.

Indeed (in practice ‘ptr’ would happen to be finalized later, but that’s
“out of luck”.)

>> (In 2.0 the finalizer is not called until ‘ptr’ is no longer in scope.)
>>
>> I’m not sure this counts as a bug, but it’s certainly a pitfall when
>> working with finalizers and the FFI.
>>
>> Thoughts?
>
> For me, I don't think this is a bug.  Rather the contrary, as it's more
> in spirit with safe-for-space principle that a continuation should only
> keep alive those values that it uses; any other data should be available
> for the GC to reclaim.
>
> In any case, I think this manual section treats the problem adequately,
> for me at least:
>
>   https://www.gnu.org/software/guile/manual/html_node/Foreign-Object-Memory-Management.html
>
> Would you like to add something there?

Hmm, I don’t think so (great section, BTW).

I need to chew a bit more on this, but the conclusion is probably that
my expectations were incorrect, indeed.  :-)

Thanks,
Ludo’.





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

* bug#26503: Local variables reclaimed early vs. finalizers
  2017-04-19  9:50   ` Ludovic Courtès
@ 2017-04-19 12:23     ` Andy Wingo
  0 siblings, 0 replies; 4+ messages in thread
From: Andy Wingo @ 2017-04-19 12:23 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: 26503-done

On Wed 19 Apr 2017 11:50, ludo@gnu.org (Ludovic Courtès) writes:

> I need to chew a bit more on this, but the conclusion is probably that
> my expectations were incorrect, indeed.  :-)

OK I close this bug in the meantime then :)  Feel free to reopen if
there is a thing to do!

Andy





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

end of thread, other threads:[~2017-04-19 12:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-14 21:56 bug#26503: Local variables reclaimed early vs. finalizers Ludovic Courtès
2017-04-19  8:00 ` Andy Wingo
2017-04-19  9:50   ` Ludovic Courtès
2017-04-19 12:23     ` Andy Wingo

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 read-only IMAP folder(s) and NNTP newsgroup(s).