github/Akavache — pull request #29 by Haacked (2013-02-25 00:04:26+00:00)
Title: Dispose race condition fix

This PR ensures that Dispose doesn't cause a `NullReferenceException` for outstanding operations.

I added a very complicated looking unit test (though in principle it's simple, it just looks nasty because THREADING). The test sets up three threads so we can get into the error situation.

The rules are: `Dispose` effectively waits for outstanding operations on `MemoizedRequests` to complete before it does it's thing. Likewise, any new requests must wait for `Dispose` to complete.

`Dispose is only called once. Any future calls exit immediately and do nothing.`

If an outstanding operation is waiting on a lock of `MemoizedRequests` and the cache is disposed, once that operation obtains the lock it will throw an `ObjectDisposedException`.

If an object is already disposed, operations throw an `ObjectDisposedException`.