-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix possible NPE from WebSocketAdapter #7294
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,11 @@ | |
public class WebSocketAdapter implements WebSocketListener | ||
{ | ||
private volatile Session session; | ||
private RemoteEndpoint remote; | ||
|
||
public RemoteEndpoint getRemote() | ||
{ | ||
return session.getRemote(); | ||
return remote; | ||
} | ||
|
||
public Session getSession() | ||
|
@@ -34,7 +35,8 @@ public Session getSession() | |
|
||
public boolean isConnected() | ||
{ | ||
return session.isOpen(); | ||
Session sess = this.session; | ||
return (sess != null) && (sess.isOpen()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is standard multi threading coding practice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. Thanks. |
||
} | ||
|
||
public boolean isNotConnected() | ||
|
@@ -58,6 +60,7 @@ public void onWebSocketClose(int statusCode, String reason) | |
public void onWebSocketConnect(Session sess) | ||
{ | ||
this.session = sess; | ||
this.remote = sess.getRemote(); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO
return session == null ? null : session.getRemote();
might be better. This is minor, but could save theremote
field. And I think the readability is not affected too much either way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more surprising for
getRemote()
to suddenly start returningnull
after it was non-null.Let the usage of
Remote
trigger the exceptions indicating that you no longer have a valid connection/session/whatnot.