alibaba / alibaba/spring-context-support

FieldUtils may have concurrency issues

Open
#51 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
155
Forks
87
PR merge metrics
No merged PRs in 30d

Description

for example: String id=FieldUtils.getFieldValue(next, "id")

```
public class Base implements Serializable {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
```
```
public class Son extends Base implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```

```
public class TestCase {

public static void main(String[] args) {

List records = new ArrayList<>();
List records2 = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 1000; i++) {
long l = random.nextLong();
Son son = new Son();
son.setId(l + "");
records.add(son);
Son son2 = new Son();
son2.setId(l + "");
records2.add(son2);
}
new Thread(() -> {
Iterator iterator = records.iterator();
while (iterator.hasNext()) {
Son next = iterator.next();
String id = FieldUtils.getFieldValue(next, "id");
}
}).start();

new Thread(() -> {
Iterator iterator = records2.iterator();
while (iterator.hasNext()) {
Son next = iterator.next();
String id = FieldUtils.getFieldValue(next, "id");
}
}).start();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
```

Exception in thread "Thread-1" java.lang.IllegalStateException: Could not access method or field: Class org.springframework.util.ReflectionUtils can not access a member of class com.Base with modifiers "private"
at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:107)
at org.springframework.util.ReflectionUtils.getField(ReflectionUtils.java:652)
at com.alibaba.spring.util.FieldUtils.getFieldValue(FieldUtils.java:72)
at com.alibaba.spring.util.FieldUtils.getFieldValue(FieldUtils.java:28)
at com.TestCase.lambda$main$1(TestCase.java:41)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at com.alibaba.spring.util.FieldUtils.getFieldValue, especially FieldUtils.java:72, and reproduce the supplied two-thread example with the Base and Son classes. Trace the reflective field lookup and verify that concurrent calls complete without the reported IllegalStateException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.