Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The check against null and usage of the field updateThread is typically protected by synchronization, but not in 1 location. #1413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adriannistor
Copy link

@adriannistor adriannistor commented Apr 6, 2020

Issue Report on JIRA

I also reported this issues on JIRA at:

https://issues.apache.org/jira/browse/LUCENE-9306

Bug 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, 357-358, and 380-381.

However, the null check at line 387 and the usage at line 388 are not protected by synchronized.

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) 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, 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 synchronized, just like the other methods containing null checks and usages of updateThread.

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

…ly protected by synchronization, but not in 1 location.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant