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.
8 b68Fixed
Related Reports
Relates :  
Relates :  
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
keywords=bug4672609 bug5076965 bug4739238
run=ASSUMED_ACTION main LocalAddress\n
title=Test getLocalAddress getLocalPort


elapsed=819 0\:00\:00.819
end=Fri Nov 16 11\:33\:49 GMT 2012
execStatus=Failed. Execution failed\: `main' threw exception\: java.lang.NullPointerException
javatestOS=Linux 2.6.32-100.28.5.el6.x86_64 (amd64)
jtregVersion=jtreg 4.1 fcs b04
sections=script_messages build compile main
start=Fri Nov 16 11\:33\:49 GMT 2012

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)

command: build LocalAddress
reason: Named class compiled on demand
elapsed time (seconds): 0.693
result: Passed. Build successful

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
result: Passed. Compilation successful

command: main LocalAddress
reason: Assumed action based on file name: run main LocalAddress 
elapsed time (seconds): 0.126
	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: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

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.

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.