Subject: bug#58732: installer: finalizers & device destroy segfault

From: Ludovic Courtès <[email-masked]>
Date: Wed, 02 Nov 2022 11:55:37 +0100

Hi,

Mathieu Othacehe <[email-masked]> skribis:

> null! I guess this has to deal with device pointer finalizers. I'm a bit
> disappointed because I thought we had overcome those mistakes.

There are several things we should audit in Guile-Parted regarding
object lifecycles.

Common issues when writing bindings that could cause problems like what
you report:

  1. Bindings create wrappers for C pointers—e.g., with
     ‘pointer->device’.  If several C functions return a pointer P, you
     must make sure to return always the same wrapper and not create a
     new one.

     ‘pointer->device!’ attempts to do that but I think it’s bogus: it
     uses a weak-value hash table, where the value is the wrapper.  So
     if the wrapper disappears before the underlying C object, then the
     pointer is called and bad things ensue.

     ‘define-wrapped-pointer-type’ in Guile is meant to help with these
     things (info "(guile) Void Pointers and Byte Access").  We can’t
     use it directly here because we’re using bytestructures and all
     that.

  2. Scheme wrappers must mirror the aggregation relations of the C
     objects they wrap.

     For instance, let’s say a PedDisk aggregates PedPartition
     objects—i.e., that PedDisk “owns” them and outlives them.  Then the
     Scheme <disk> wrappers must outlive the Scheme <partition> wrappers
     too, so that we don’t end up calling partition finalizers while the
     disk that owns them is still alive.  This is done using a weak-key
     hash table keyed by <disk> objects in this case and populated
     anytime a <disk> getter returns a <partition>.  (See for example
     ‘%commit-owners’ in Guile-Git.)

I think we should audit Guile-Parted with these issues in mind.

WDYT?

Ludo’.