Skip to content

NullPointerException in WebSocketAdapter.getFlashPolicy #206

Closed
@sippykup

Description

@sippykup

We encountered this NullPointerException today in production, and it caused the Websocket server to shut down. I had to restart our application server to get it operational again.

ERROR (WebSocketWorker-90) Websocket connection error from null
java.lang.NullPointerException
at org.java_websocket.WebSocketAdapter.getFlashPolicy(WebSocketAdapter.java:87)
at org.java_websocket.WebSocketImpl.decodeHandshake(WebSocketImpl.java:193)
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:161)
at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:663)
2013-09-12 04:31:08,247 INFO (WebSocketWorker-90) Web socket server stopped

Here is the line that causes the NPE:

return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0";

conn.getLocalSocketAddress() is what's returning null here. Here's what it does:

return wsl.getLocalSocketAddress( this );

And here's what that does:

return (InetSocketAddress) getSocket( conn ).getLocalSocketAddress();

So the call to getLocalSocketAddress() here on java.net.Socket is returning null.

The Javadoc for this method says:

Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.

I don't know why this method is getting invoked when the socket is not yet bound, but the bug is either that you're invoking this method before ensuring that the socket is bound (my guess), or that you need to handle a null return from getLocalSocketAddress().

Additionally, this exception (and all RuntimeExceptions) needs to be properly handled, and NOT cause the server to throw up its hands and shut down. I'll file a separate bug for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions