Skip to content

Fix reference cycle between Table and Column - #2879

Merged
embray merged 2 commits into
astropy:masterfrom
embray:table/reference-cycle
Aug 26, 2014
Merged

embray merged 2 commits into
astropy:masterfrom
embray:table/reference-cycle

Conversation

@embray

@embray embray commented Aug 20, 2014

Copy link
Copy Markdown
Member

As discovered while looking into #2877 there is a reference cycle that goes:

Table -> TableColumns -> Column -> Table

by way of the parent_table attribute on columns. This fixes it by making parent_table a weakref (hidden away via a property allowing minimal code changes elsewhere). A test demonstrating the fix is included.

@embray embray added this to the v0.4.2 milestone Aug 20, 2014
@embray

embray commented Aug 20, 2014

Copy link
Copy Markdown
Member Author

Note: This doesn't fully resolve #2877 which reported a segfault. But it does lessen the impact.

@embray

embray commented Aug 20, 2014

Copy link
Copy Markdown
Member Author

A note I would like to add about the regression test for this. To quote the docs:

Objects that have __del__() methods and are part of a reference cycle cause the entire reference cycle to be uncollectable, including objects not necessarily in the cycle but reachable only from it. Python doesn’t collect such cycles automatically because, in general, it isn’t possible for Python to guess a safe order in which to run the __del__() methods.

Because the test defines a subclass of Table with a __del__ it sort of forces the issue. If the TestTable.__del__ method is called, that proves (presumably) that the table is not part of a reference cycle.

@embray
embray force-pushed the table/reference-cycle branch from 9805e74 to d2697ef Compare August 20, 2014 22:51
@mhvk

mhvk commented Aug 21, 2014

Copy link
Copy Markdown
Contributor

@embray - I didn't look in detail at what you did, but if Column needs a weakref to its parent table, then probably Row would need one too...

@embray

embray commented Aug 21, 2014

Copy link
Copy Markdown
Member Author

I don't think so. As I understand it Rows are fairly lightweight objects that are only created when a table is indexed. The Row contains a reference to the table it came from, but the table does not keep a reference to the Row object. So there is no reference cycle.

@mhvk

mhvk commented Aug 21, 2014

Copy link
Copy Markdown
Contributor

@embray - a, yes, I didn't think it through properly; makes sense that Row is OK.

@mdboom

mdboom commented Aug 25, 2014

Copy link
Copy Markdown
Contributor

👍

embray added 2 commits August 26, 2014 13:55
…t_table as a weakref. This is achievable since most of the Column class seems to work find if there isn't a parent_table. Also makes parent_table a property, allowing minimal changes elsewhere.
@embray
embray force-pushed the table/reference-cycle branch from d2697ef to 9237d6b Compare August 26, 2014 17:55
@embray

embray commented Aug 26, 2014

Copy link
Copy Markdown
Member Author

Weird that I had to rebase this since there were no merge conflicts. Oh well.

@taldcroft

Copy link
Copy Markdown
Member

Looks fine to me.

embray added a commit that referenced this pull request Aug 26, 2014
Fix reference cycle between Table and Column
@embray
embray merged commit 9ac577e into astropy:master Aug 26, 2014
@embray
embray deleted the table/reference-cycle branch August 26, 2014 21:07
embray added a commit that referenced this pull request Sep 17, 2014
Fix misplayed changelog entry for #2879
embray added a commit that referenced this pull request Sep 18, 2014
Fix reference cycle between Table and Column
Conflicts:

	CHANGES.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants