Skip to content
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

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
public class WebSocketAdapter implements WebSocketListener
{
private volatile Session session;
private RemoteEndpoint remote;

public RemoteEndpoint getRemote()
{
return session.getRemote();
return remote;
Copy link

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 the remote field. And I think the readability is not affected too much either way.

Copy link
Contributor

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 returning null after it was non-null.

Let the usage of Remote trigger the exceptions indicating that you no longer have a valid connection/session/whatnot.

}

public Session getSession()
Expand All @@ -34,7 +35,8 @@ public Session getSession()

public boolean isConnected()
{
return session.isOpen();
Session sess = this.session;
return (sess != null) && (sess.isOpen());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just return session != null && session.isOpen(); should be more readable. Creating sess here seems redundant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is standard multi threading coding practice.
The value of this.session can actually change between the 2 evaluations (non-null check and isopen).
It could go from non-null to null between the two checks.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Thanks.

}

public boolean isNotConnected()
Expand All @@ -58,6 +60,7 @@ public void onWebSocketClose(int statusCode, String reason)
public void onWebSocketConnect(Session sess)
{
this.session = sess;
this.remote = sess.getRemote();
}

@Override
Expand Down