[TYRUS-91] connectToServer creates 2 SessionImpl instances Created: 13/Feb/13  Updated: 14/Feb/13  Resolved: 14/Feb/13

Status: Resolved
Project: tyrus
Component/s: None
Affects Version/s: 1.0-b10
Fix Version/s: 1.0-b12

Type: Bug Priority: Major
Reporter: johanvos Assignee: Pavel Bucek
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I had an issue with some simple clientcode:

container = ContainerProvider.getClientContainer();
Session session = container.connectToServer(MyEndpoint.class, null, new
URI(server));
session.addMessageHandler(myHandler);

The onMessage call on myHandler never got called.

I debugged this, and it turned out that there were 2 Session objects:
the one returned by Container.connectToServer, and a second one that
was created during handshake. Since I didn't add a handler to the
second one, my callback method never got called.
I could easily fix this by having the handler added in the onOpen
method on the Endpoint in MyEndpoint.class:
@Override
public void onOpen(Session session, EndpointConfiguration config) {
LOGGER.info("RedFXWebSocketEndpoint is opened, session
= "+session);
this.session = session;
session.addMessageHandler(this);
}

The Session object that is passed into this onOpen method is the second
SessionImpl.

I wonder however, if it is intentional that a second SessionImpl is
created?
For debugging info, I dumped the stacktrace for the 2 calls to the
SessionImpl constructor:
at java.lang.Thread.dumpStack(Thread.java:1342)
at org.glassfish.tyrus.SessionImpl.<init>(SessionImpl.java:115)
at
org.glassfish.tyrus.EndpointWrapper.createSessionForRemoteEndpoint(Endp
ointWrapper.java:305)
at
org.glassfish.tyrus.container.grizzly.GrizzlyClientSocket.addEndpoint(G
rizzlyClientSocket.java:244)
at
org.glassfish.tyrus.container.grizzly.GrizzlyEngine.openClientSocket(Gr
izzlyEngine.java:112)
at
org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.
java:198)
at
org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.
java:140)
at
org.redfx.client.websocket.WebSocketCommunicator.initialize(WebSocketCo
mmunicator.java:79)
...

And
at java.lang.Thread.dumpStack(Thread.java:1342)
at org.glassfish.tyrus.SessionImpl.<init>(SessionImpl.java:115)
at
org.glassfish.tyrus.EndpointWrapper.onConnect(EndpointWrapper.java:317)
at
org.glassfish.tyrus.container.grizzly.GrizzlyClientSocket.onConnect(Gri
zzlyClientSocket.java:336)
at
org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleClientHandS
hake(WebSocketFilter.java:317)
at
org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleHandshake(W
ebSocketFilter.java:308)
at
org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleRead(WebSoc
ketFilter.java:230)
at
org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorRe
solver.java:119)
at
org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(Defa
ultFilterChain.java:273)
at
org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(D
efaultFilterChain.java:200)
at
org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFil
terChain.java:134)
at
org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFil
terChain.java:112)
at
org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:
77)
at
org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOT
ransport.java:818)
at
org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(Abstrac
tIOStrategy.java:112)
at
org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThre
adIOStrategy.java:115)
at
org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(Work
erThreadIOStrategy.java:55)
at
org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRun
nable.run(WorkerThreadIOStrategy.java:135)
at
org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(Abstr
actThreadPool.java:562)
at
org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(Abstract
ThreadPool.java:542)
at java.lang.Thread.run(Thread.java:722)



 Comments   
Comment by johanvos [ 13/Feb/13 ]

The following patch on RemoteEndpoint fixed my problem – but I didn't look at the internals of SPIRemoteEndpoint, so this may introduce other issues, although all tests still run fine:

  1. This patch file was generated by NetBeans IDE
  2. It uses platform neutral UTF-8 encoding and \n newlines.
      • Base (BASE)
        +++ Locally Modified (Based On LOCAL)
        @@ -302,8 +302,10 @@
        @Override
        public Session createSessionForRemoteEndpoint(SPIRemoteEndpoint re, String subprotocol, List<Extension> extensions) { - return new SessionImpl(container, re, this, subprotocol, extensions, isSecure, + SessionImpl answer = new SessionImpl(container, re, this, subprotocol, extensions, isSecure, uri == null ? null : URI.create(uri), queryString, templateValues); + remoteEndpointToSession.put(re, answer); + return answer; }

@Override
@@ -314,8 +316,11 @@
@Override
public void onConnect(SPIRemoteEndpoint gs, String subprotocol, List<Extension> extensions) {
// create a new session

  • SessionImpl session = new SessionImpl(container, gs, this, subprotocol, extensions, isSecure,
    + SessionImpl session = remoteEndpointToSession.get(gs);
    + if (session == null) { + session = new SessionImpl(container, gs, this, subprotocol, extensions, isSecure, uri == null ? null : URI.create(uri), queryString, templateValues); + }

    remoteEndpointToSession.put(gs, session);
    endpoint.onOpen(session, configuration);
    }

Comment by Pavel Bucek [ 13/Feb/13 ]

well, i have it fixed already in my local workspace and I took similar approach .. nevertheless, having you posted this here makes it actually more difficult I cannot accept any contribution without OCA - http://www.oracle.com/technetwork/community/oca-486395.html. Have you signed that? (Unable to find your name there).

Comment by Pavel Bucek [ 14/Feb/13 ]

fixed in the trunk, thanks!

Generated at Sun Mar 26 02:12:46 UTC 2017 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.