Azure-Samples / Azure-Samples/azure-cache-redis-samples
Incorrect order of replacing old connection with new one
- Dominant language
- Bicep
- Stars
- 77
- Forks
- 169
- Avg merge
- 1h 6m
- Merged PRs (30d)
- 1
Description
### 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](https://github.com/Azure-Samples/azure-cache-redis-samples/blob/7fb7e32aeea2d202f108814b8b738d5045faf678/quickstart/dotnet-core/RedisConnection.cs#L145) to line [159](https://github.com/Azure-Samples/azure-cache-redis-samples/blob/7fb7e32aeea2d202f108814b8b738d5045faf678/quickstart/dotnet-core/RedisConnection.cs#L159) 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:
- Any thread holding the current connection reference after 1 has run, but before 2 has run will get an ObjectDisposedException.
- Any thread using _connection after 2 has run and before 4 has run will get a NullReferenceException.
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.
Contributor guide
Research direction
Start in quickstart/dotnet-core/RedisConnection.cs at lines 145-159 and trace all uses of _connection. Review the replacement sequence under concurrent access, including disposal of the old connection. Done means eliminating the null-reference window and defining safe handling for threads still using the old connection; no test is named.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp, redis
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100