tyrus
  1. tyrus
  2. TYRUS-91

connectToServer creates 2 SessionImpl instances

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0-b10
    • Fix Version/s: 1.0-b12
    • Component/s: None
    • Labels:
      None

      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)

        Activity

        Hide
        johanvos added a comment -

        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);
          }

        Show
        johanvos added a comment - 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: This patch file was generated by NetBeans IDE 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); }
        Hide
        Pavel Bucek added a comment - - edited

        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).

        Show
        Pavel Bucek added a comment - - edited 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).
        Hide
        Pavel Bucek added a comment -

        fixed in the trunk, thanks!

        Show
        Pavel Bucek added a comment - fixed in the trunk, thanks!

          People

          • Assignee:
            Pavel Bucek
            Reporter:
            johanvos
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: