apache / apache/lucene

The check against null and usage of the field updateThread is typically protected by synchronization, but not in 1 location. [LUCENE-9306]

Open
#10,346 2 comments 0 reactions 0 assignees View on GitHub
legacy-jira-priority:Major module:replicator type:bug
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

# Github Pull Request

I created a pull request on github for this issue at:

# Description

Checks against `null` and usages of the field `updateThread` are typically made atomic by synchronization on `this` (synchronized methods), e.g., at lines: [339-341](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L339-L341), [357-358](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L357-L358), and [380-381](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L380-L381).

However, the `null` check at [line 387](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L387) and the usage at [line 388](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L388) are not protected by `synchronized`.

```java
public String toString() {
String res = "ReplicationClient";
if (updateThread != null) { <<<<<<<<<<<<<<<<<<<<<
res += " (" + updateThread.getName() + ")"; <<<<<<<<<<<<<<<<<
```

This check against `null` and usage of `updateThread` are in `toString()`.

However, the problem is not that the `toString()` will give a garbled string (i.e., a relatively minor issues).

The problem is that, in between the `null` check and the usage, `updateThread` can be set to `null` (e.g., by [line 369](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L369)) and therefore the code can crash.

I.e., without `synchronized`, the `null` check does not protect the `updateThread.getName()` usage.

I don't know how `toString()` is called concurrently. However, it seems like a dangerous assumption to make that the callers of `toString()` know it should not be called concurrently with [line 369](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L369), especially as the other methods do protect with synchronization the `null` check and usage.
# This Patch's Code

The fix is very simple: just make the method containing [lines 387-388](https://github.com/apache/lucene-solr/blob/531015245042507d71845b6d584e7e7389303093/lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java#L387-L388) `synchronized`, just like the other methods containing `null` checks and usages of `updateThread`.

```java
public synchronized String toString() { <<<<<<<<<<<<<< added "synchronized" here
String res = "ReplicationClient";
if (updateThread != null) {
res += " (" + updateThread.getName() + ")";
```

---
Migrated from [LUCENE-9306](https://issues.apache.org/jira/browse/LUCENE-9306) by Adrian Nistor
Attachments: [LUCENE-9306.patch](https://apache.github.io/lucene-jira-archive/attachments/LUCENE-9306/LUCENE-9306.patch)

Contributor guide

Open the contributing guide

Research direction

Read lucene/replicator/src/java/org/apache/lucene/replicator/ReplicationClient.java, focusing on toString() around lines 387-388 and the synchronized methods that access updateThread. Confirm that the change matches the synchronization used elsewhere and that the concurrent null-check concern is addressed; the issue already provides the proposed patch and links to an existing pull request.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.