JDK-8003833 : Spurious NPE from Socket.getIn/OutputStream
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 8
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2012-11-21
  • Updated: 2013-05-06
  • Resolved: 2012-11-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 8
8 b68Fixed
Related Reports
Relates :  
Relates :  
Description
One of the NIO tests has start failing intermittently in recent builds. Here's a sample .jtr:


#Test Results (version 2)
#Fri Nov 16 11:33:49 GMT 2012
#checksum:100a557b15f30270
#-----testdescription-----
$file=/home/jtest/openjdk/jdk8b65/jdk/test/java/nio/channels/SocketChannel/LocalAddress.java
$root=/home/jtest/openjdk/jdk8b65/jdk/test
keywords=bug4672609 bug5076965 bug4739238
library=..
run=ASSUMED_ACTION main LocalAddress\n
source=LocalAddress.java
title=Test getLocalAddress getLocalPort

#-----environment-----

#-----testresult-----
description=file\:/home/jtest/openjdk/jdk8b65/jdk/test/java/nio/channels/SocketChannel/LocalAddress.java
elapsed=819 0\:00\:00.819
end=Fri Nov 16 11\:33\:49 GMT 2012
environment=regtest
execStatus=Failed. Execution failed\: `main' threw exception\: java.lang.NullPointerException
hostname=limbo
javatestOS=Linux 2.6.32-100.28.5.el6.x86_64 (amd64)
javatestVersion=4.4
jtregVersion=jtreg 4.1 fcs b04
script=com.sun.javatest.regtest.RegressionScript 
sections=script_messages build compile main
start=Fri Nov 16 11\:33\:49 GMT 2012
test=java/nio/channels/SocketChannel/LocalAddress.java
user.name=jtest
work=/home/jtest/results/b65/testresults/b65/jdk/JTwork/java/nio/channels/SocketChannel

#section:script_messages
----------messages:(4/184)----------
JDK under test: (/home/jtest/jdks/jdk1.8.0)
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b65)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b09, mixed mode)

#section:build
----------messages:(3/97)----------
command: build LocalAddress
reason: Named class compiled on demand
elapsed time (seconds): 0.693
result: Passed. Build successful

#section:compile
----------messages:(3/184)----------
command: compile /home/jtest/openjdk/jdk8b65/jdk/test/java/nio/channels/SocketChannel/LocalAddress.java
reason: .class file out of date or does not exist
elapsed time (seconds): 0.692
----------System.out:(0/0)----------
----------System.err:(0/0)----------
result: Passed. Compilation successful

#section:main
----------messages:(3/123)----------
command: main LocalAddress
reason: Assumed action based on file name: run main LocalAddress 
elapsed time (seconds): 0.126
----------System.out:(0/0)----------
----------System.err:(11/602)----------
java.lang.NullPointerException
	at java.io.FileOutputStream.<init>(FileOutputStream.java:232)
	at java.net.SocketOutputStream.<init>(SocketOutputStream.java:58)
	at java.net.AbstractPlainSocketImpl.getOutputStream(AbstractPlainSocketImpl.java:441)
	at java.net.Socket$3.run(Socket.java:914)
	at java.net.Socket$3.run(Socket.java:912)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.Socket.getOutputStream(Socket.java:911)
	at TestServers$EchoServer$EchoConnection.run(TestServers.java:369)
STATUS:Passed.
STATUS:Failed.`main' threw exception: java.lang.NullPointerException
result: Failed. Execution failed: `main' threw exception: java.lang.NullPointerException


test result: Failed. Execution failed: `main' threw exception: java.lang.NullPointerException

Comments
There appears to be a very small window in AbstractPlainSocketImpl.getIn/OutputStream where isClosedOrPending() grabs the fdLock to check if the socket is closed, or not, and the construction of the in/output stream, where another thread may asynchronously close the socket. This is exactly what the EchoServer in the test regression library is doing.
21-11-2012

Socket.getOutputStream should never throw NPE. It appears that the long standing behavior is to throw IOException("Socket is closed"), this seems to be implied by the spec too.
21-11-2012