JDK-8185092 : Data race in FilterOutputStream.close
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2017-07-21
  • Updated: 2017-12-14
  • Resolved: 2017-07-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10
10 b18Fixed
Related Reports
Relates :  
Description
There have been several attempts to fix FilterOutputStream.close, but a casual look reveals that close() has an obvious data race when close is called by separate threads.  

    public void close() throws IOException {
        if (closed) {
            return;
        }
        closed = true;

This is a problem because FilterOutputStream makes no effort to be thread-safe but its subclasses like BufferedOutputStream do, without overriding close.  Maybe it's just too hard to have a thread-unsafe superclass with thread-safe subclasses?  One is tempted to rewrite classes like BufferedOutputStream without using any of the FilterOutputStream helper methods!

This race is likely to be mostly benign in practice.  I investigated this because we see TSAN warnings in jdk8.  There's no closed field in jdk8, but the fundamental problem of implementing close so that it works with thread-safe and thread-unsafe subclasses remains.

Comments
(Surprisingly ...) It looks like there are no thread-safety specs anywhere in java.io, even though many classes try to achieve some thread safety. The specs should be fixed and tests should be added, but it's obviously a big project.
25-07-2017