You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 54 Next »

Overriding thread-safe methods with methods that are not thread-safe can result in improper synchronization if the client inadvertently operates on an instance of the thread-unsafe subclass. An overridden synchronized method's contract may be violated if a subclass provides an implementation that is not safe for concurrent use.

Overriding thread-safe methods with methods that are not thread-safe is not in itself an error, but is disallowed by this guideline because it may easily result in errors that are otherwise difficult to diagnose.

The locking strategy of classes designed for inheritance should always be documented. This information can subsequently be used to determine an appropriate locking strategy for subclasses (see CON04-J. Use private final lock objects to synchronize classes that may interact with untrusted code).

Noncompliant Code Example (synchronized method)

This noncompliant code example overrides the synchronized doSomething() method in class Base with an unsynchronized method in the derived class Derived.

class Base {
  public synchronized void doSomething() {
    // ...	
  }
}

class Derived extends Base {
  public void doSomething() {
    // ...		
  }
}

The doSomething() method of class Base can be safely used by multiple threads. However, instances of the subclass Derived cannot be safely used by multiple threads.

This programming error can be difficult to diagnose because threads that accept instances of Base can also accept instances of its subclasses. Consequently, clients may be unaware that they are operating on an instance of the subclass of a thread-safe class that is not thread-safe.

Compliant Solution (synchronized method)

This compliant solution synchronizes the doSomething() method of the subclass.

class Base {
  public synchronized void doSomething() {
    // ...	
  }
}

class Derived extends Base {
  public synchronized void doSomething() {
    // ...		
  }
}

This compliant solution does not violate CON04-J. Use private final lock objects to synchronize classes that may interact with untrusted code because the accessibility of the class is package-private which is allowable when untrusted code cannot infiltrate the package.

Compliant Solution (private final lock object)

This compliant solution ensures that the Derived class is thread-safe by overriding the synchronized doSomething() method of the Base class with a method that synchronizes on a private final lock object.

class Base {

  public synchronized void doSomething() {
    // ...	
  }
}

class Derived extends Base {

  private final Object lock = new Object();

  public void doSomething() {
    synchronized (lock) {
      // ...	
    }	
  }
}

This is an acceptable solution, provided that the Derived class has a consistent locking policy.

Noncompliant Code Example (private lock)

This noncompliant code example defines a doSomething() method in class Base that uses a private final lock, in accordance with CON04-J. Use private final lock objects to synchronize classes that may interact with untrusted code.

class Base {
  private final Object lock = new Object();

  public void doSomething() {
    synchronized (lock) {
      // ...	
    }
  }
}

class Derived extends Base {
  public void doSomething() {
    try {
      super.doSomething();
    } finally {
      logger.log(Level.FINE, "Did something"); 
    }
  }
}

It is possible that multiple threads may cause the entries to be logged in an order that differs from the order in which the tasks are performed. Consequently, The doSomething() method of class Derived cannot be safely used by multiple threads because it is not thread-safe.

Compliant Solution (private lock)

This compliant solution synchronizes the doSomething() method of the subclass using a private final lock object.

class Base {
  private final Object lock = new Object();

  public void doSomething() {
    synchronized (lock) {
      // ...	
    }
  }
}

class Derived extends Base {
  private final Object lock = new Object();

  public void doSomething() {
    synchronized (lock) {
      try {
        super.doSomething();
      } finally {
        logger.log(Level.FINE, "Did something"); 
      }
    }
  }
}

Note that the objects of Base and Derived maintain distinct locks that are inaccessible from each others' classes. Consequently, Derived can provide thread-safety guarantees independent of Base.

Risk Assessment

Overriding thread-safe methods with methods that are not thread-safe can result in unexpected behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

CON10- J

low

probable

medium

P4

L3

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

[[API 06]]
[[SDN 08]] Sun bug database, Bug ID 4294756


VOID CON09-J. Do not invoke alien methods that rely on invariants protected by the same lock object      11. Concurrency (CON)      CON11-J. Do not assume that declaring an object reference volatile guarantees visibility of its members

  • No labels