Fix extension cleanup when a process or unit tests exit.

Review Request #7548 — Created July 25, 2015 and submitted — Latest diff uploaded

Information

Djblets
release-0.9.x

Reviewers

There's an annoying warning that will appear when a process using
extensions with SignalHooks shuts down. This warning shows Django trying
to work with garbage state as the result of a weak reference
disappearing during garbage collection. It's reported upstream as a race
condition, with no fix.

To prevent this from happening, we're now registering an atexit function
to clear out the extension manager's state and shut down all extensions.
This will happen before Django's code gets called, preventing it from
dealing with garbage state.

We also now weakly track the extension manager instances, so that they
don't pile up when running unit tests or when an application wants to
just temporarily create an instance. This works along with a fix to the
shutdown code for an issue where two instances of the same
ExtensionManager would stomp over each other during shutdown. This is
needed to keep the unit tests from emitting lots of warnings from the
above fix.

Reproduced the original problem by enabling Power Pack, loading a page,
and then killing the dev server. I no longer saw the error message.

That unveiled the persistent instances and shutdown problems in the unit
tests, due to multiple instances of the same ExtensionManagers being kept
around and all trying to shut down already shut-down state. The latter
half of this fix prevented that from happening again.

Djblets and Review Board unit tests pass.