Azure-Samples / azure-cache-redis-samples

This repository contains samples that demonstrate best practices and general recommendations to communicate with Azure Redis Cache Service caches from various Redis client frameworks.
MIT License
69 stars 156 forks source link

Incorrect order of replacing old connection with new one #38

Open akrabi opened 1 year ago

akrabi commented 1 year ago

This issue is for a: (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Mention any other details that might be useful

From line 145 to line 159 there's the following logic:

  1. Close the current connection
  2. Replace _connection with null
  3. Create a new connection
  4. Replace _connection with the new connection

The issue is, that it opens a window for NullReferenceException and ObjectDisposedException exceptions:

The correct order should be:

  1. Create a new connection
  2. Swap current connection with the new connection (atomic)
  3. Dispose old connection - this one can still cause problems to any thread still using the connection, not sure what to do with it.