Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure same name arrays do not shallow copy #2872

Merged
merged 3 commits into from
Jun 27, 2022
Merged

Conversation

akaszynski
Copy link
Member

@akaszynski akaszynski commented Jun 26, 2022

Resolves #2864 by ensuring that same named arrays are not shallow copied, but rather have their reference directly returned by _prepare_array.

For whatever reason, arrays that have been shallow copied still have their underlying arrays collected by VTK despite that they're used.

You can reproduce the memory issue (from a fresh python shell) with:

import numpy as np
import pyvista

points = np.zeros((10000000, 3))
dataset = pyvista.PointSet(points)
data = np.arange(dataset.n_points, dtype=float)

dataset['scalars'] = data.copy()
dataset['scalars'] *= 0.5
print(dataset['scalars'])

@akaszynski akaszynski requested a review from adeak June 26, 2022 20:42
@github-actions github-actions bot added the bug Uh-oh! Something isn't working as expected. label Jun 26, 2022
@akaszynski akaszynski changed the title ensure same name arrays do not shallow_copy Ensure same name arrays do not shallow copy Jun 26, 2022
@codecov
Copy link

codecov bot commented Jun 26, 2022

Codecov Report

Merging #2872 (268164f) into main (257f4aa) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #2872      +/-   ##
==========================================
- Coverage   94.09%   94.04%   -0.06%     
==========================================
  Files          76       76              
  Lines       16426    16428       +2     
==========================================
- Hits        15456    15449       -7     
- Misses        970      979       +9     

@adeak
Copy link
Member

adeak commented Jun 26, 2022

Hmm, I don't think I fully understand the issue, but I guess if the change makes the segfault go away it's definitely the right call.

But looking at the use case of the fix we can come up with an even more absurd test:

import numpy as np
import pyvista

points = np.zeros((10000000, 3))
dataset = pyvista.PointSet(points)
data = np.arange(dataset.n_points, dtype=float)

dataset['scalars'] = data.copy()
dataset['scalars'] = dataset['scalars']
print(dataset['scalars'])

I can't really put my finger on the issue, because it seems to me that in the buggy case execution went like this:

self.set_array(value, name=key)

vtk_arr = self._prepare_array(data, name, deep_copy)
self.VTKObject.AddArray(vtk_arr)
self.VTKObject.Modified()

And here vtk_arr ended up being a shallow copy instead of a reference:

vtk_arr = copy_vtk_array(data.VTKObject, deep=deep_copy)
if isinstance(name, str):
vtk_arr.SetName(name)
return vtk_arr

I would assume what's going on is that vtkPointData (through DataSetAttributes) sees that key 'scalars' is being replaced, decreases some internal refcount of the original array, but since it's trying to add the same array back to itself (through the shallow copy) things blow up.

But then why does this not reproduce the segfault:

import numpy as np
import pyvista

points = np.zeros((10000000, 3))
dataset = pyvista.PointSet(points)
data = np.arange(dataset.n_points, dtype=float)
dataset['scalars'] = data.copy()

# pure-pyvsta version
#dataset['scalars'] = dataset['scalars']

# VTK version
shallow = type(dataset['scalars'].VTKObject)()  # create shallow copy
shallow.ShallowCopy(dataset['scalars'].VTKObject)  # initialize shallow copy
shallow.SetName(dataset['scalars'].VTKObject.GetName())  # rename shallow copy
dataset.point_data.VTKObject.AddArray(shallow)  # (re-)add shallow copy
dataset.point_data.VTKObject.Modified()  # set to modified

print(dataset['scalars'])  # no explosion

pyvista/core/datasetattributes.py Outdated Show resolved Hide resolved
Co-authored-by: Tetsuo Koyama <tkoyama010@gmail.com>
@akaszynski
Copy link
Member Author

Hmm, I don't think I fully understand the issue, but I guess if the change makes the segfault go away it's definitely the right call.

I spent a while trying every alternative to my proposal, and only this one did the trick.

But then why does this not reproduce the segfault...

I find that perplexing as well, especially as we're effectively following the same steps as in _prepare_array.

Recommending that we proceed and debug as later should this ever crop up again.

Copy link
Member

@adeak adeak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommending that we proceed and debug as later should this ever crop up again.

Good call, let's be pragmatic here. And let's be suspicious if someone mentions "segfault" in the near future.

@akaszynski akaszynski merged commit 8ba5eca into main Jun 27, 2022
@akaszynski akaszynski deleted the fix/gc_same_name branch June 27, 2022 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Uh-oh! Something isn't working as expected.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array segmentation fault
3 participants