weakrefs and avoiding bad gc cycles
Martin Pool
mbp at canonical.com
Tue Jun 14 19:21:07 UTC 2011
On 14 June 2011 11:26, Aaron Bentley <aaron at aaronbentley.com> wrote:
> On 11-06-13 05:57 PM, Martin Pool wrote:
>> When we want to for instance
>> assert that something is definitely closed before the end of a test,
>> there are better deterministic ways to do it without involving gc.
>
> For objects where we do want to warn about failure to clean up, perhaps
> they should register themselves with a singleton, and unregister on
> cleanup? Then we can ask the singleton whether any items still need
> cleanup.
In production, when a particular bit of code finishes, we want it to
clean up after itself. This is probably most easily done using
addCleanup or with, hanging off something with the appropriate
lifetime. Generally this shouldn't really hang off a real global; if
it's something that should last as long as the bzr process there is a
library state object.
For tests, we might want to check whether those cleanups are actually
happening, and there perhaps observing all the objects that opened and
closed is worthwhile. Perhaps a clean way is to add a hook that keeps
track of them. I would probably incline away from having the global
registry always present just in case we want to use it in the test,
because it might impede gc and it might encourage people to keep
things for ever. Perhaps in some cases it's enough to just observe
all the cleanups were called. For things like locks where it is
possible to independently observe if the cleanup has been done, I
would rather just directly check that: it avoids adding infrastructure
and is a stronger assertion. Things like network connections where we
can't directly observe them probably need to be tracked.
Martin
More information about the bazaar
mailing list