Skip to content

Potential approaches to address the Python object lifecycle issue #236

Description

@Sichao25

Python relies on reference counting to clean up objects. In our case, however, the reference count for the library and mesh objects is not guaranteed to be correct, which cause some error when mesh is not manually deleted.

At the moment, I see two potential approaches to address the bug:

  1. Correct the reference count using py::keep_alive from pybind11. This is the approach implemented in Improve MPI lifecycle management in PyOmega_h #233 . It keeps the existing code structure nearly unchanged. The concern is that keep_alive is not a particularly common approach as far as I know. Most software tends to address object lifetime issues on the C++ side rather than modifying the lifetime relationship from the Python binding side. Therefore, we are not sure what other consequences this approach might have.

  2. Initialize the Omega_h library when importing the Python module and explicitly register its cleanup. With this approach, the lifecycle of the Omega_h library is moved outside the normal Python script objects scope. This would ensure that the library remains alive until after the mesh objects have been destroyed. The concern is that a slightly different pattern (library becomes kind of global singleton) is introduced from what we currently use on the C++ side.

I would appreciate any feedback on which approach would be more appropriate, or whether there is another solution we should consider. Some related code from other software is shown below for reference.
pykokkos bindings
PETSc initialize/finalize

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions