magefree / magefree/mage

refactor: ConcurrentMap usage must be fixed

Open
#11,902 0 comments 0 reactions 0 assignees View on GitHub
bug refactoring
Dominant language
Java
Stars
2.4k
Forks
940
Avg merge
2d 12h
Merged PRs (30d)
160

Description

Problem code example:
``` java
public boolean updateDeck(UUID playerId, Deck deck) {
if (tournamentSessions.containsKey(playerId)) {
return tournamentSessions.get(playerId).updateDeck(deck);
}
return false;
}
```
It can generate NPE errors cause `containsKey` and `get` are two different commands and processing in diff time (e.g. after key/value removed in other thread):
```
java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)
at mage.server.tournament.TournamentController.updateDeck(TournamentController.java:328)
at mage.server.tournament.TournamentManagerImpl.updateDeck(TournamentManagerImpl.java:65)
```

I don't think it require details research and steps to reproduce. Just use an improved code like:
```
val = concurrentMap.getOrDefault(key, null);
if (val != null) then
```

~20 fields usages to check:
![shot_240303_171343](https://github.com/magefree/mage/assets/8344157/961e2224-3ed9-4d1d-a697-3b493ea5d667)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.