B4J Question Help for Error: WebSocketException

billyrudi

Active Member
Licensed User
Longtime User
Hi,
i have the following error closing a webpage while the server is sending data to the client.
The error is the same of the well note in ABMATERIAL.
Can you help me to solve it?
regards Paolo

Waiting for debugger to connect...
Program started.
2017-01-23 17:26:26.192:INFO::main: Logging initialized @3151ms
gen 23, 2017 5:26:26 PM com.mchange.v2.log.MLog <clinit>
INFORMAZIONI: MLog clients using java 1.4+ standard logging.
gen 23, 2017 5:26:27 PM com.mchange.v2.c3p0.C3P0Registry banner
INFORMAZIONI: Initializing c3p0-0.9.2.1 [built 20-March-2013 11:16:28 +0000; debug? true; trace: 10]
2017-01-23 17:26:27.476:INFO:eek:ejs.Server:main: jetty-9.3.z-SNAPSHOT
2017-01-23 17:26:27.694:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@1970963{/,file:///D:/Solar/se/Objects/www/,AVAILABLE}
2017-01-23 17:26:27.704:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened D:\Solar\se\Objects\logs\b4j-2017_01_23.request.log
2017-01-23 17:26:27.903:INFO:eek:ejs.ServerConnector:main: Started ServerConnector@765367{HTTP/1.1,[http/1.1]}{0.0.0.0:443}
2017-01-23 17:26:27.907:INFO:eek:ejs.Server:main: Started @4877ms
Emulated network latency: 100ms
gen 23, 2017 5:26:52 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFORMAZIONI: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 20000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1bqooyy9l1kn74k1spf4so|1333739, debugUnreturnedConnectionStackTraces -> false, descri...
.....
....

Error occurred on line: 1083 (wsImpianto)
org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]

at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:299)
at anywheresoftware.b4j.object.WebSocket.Flush(WebSocket.java:101)
at b4j.example.wsimpianto._mytimer_tick(wsimpianto.java:3751)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:192)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:111)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:301)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]
at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:299)
at anywheresoftware.b4j.object.WebSocket.Flush(WebSocket.java:101)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:193)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:111)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:301)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
java.lang.NullPointerException
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:111)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:301)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
Error occurred on line: 791 (wsImpianto)
org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]
at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:299)
at anywheresoftware.b4j.object.WebSocket.Flush(WebSocket.java:101)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:131)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:111)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:301)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
 

alwaysbusy

Expert
Licensed User
Longtime User
Error occurred on line: 1083 (wsImpianto)
1. Can you at least share this code line?
2. What does it do?
3. When does it happen?
4. Have you tried checking if the websocket is available before running this code (if page.ws.open then)?
5. What are your settings for CacheScavengePeriodSeconds and SessionMaxInactiveIntervalSeconds in ABMShared?
6. How does your code line srvr.SetStaticFilesOptions() in ABMApplication look like?
7. How many connections (users) where connected at the time the error happened?

Errors like this WILL happen. If you cut the connection WHILE sending data, jetty will raise this error. Unfortunately, it is uncatchable (in B4J, but even in Java or jetty itself).

Maybe it has something to do with to many connections and you should increate the ThreadPool of jServer? If found this on the net, but no idea (or can't remember :confused:) if this can be set in B4J?

B4X:
<Configure id="server"class="org.eclipse.jetty.server.Server"><Set name="threadPool"><Newclass="org.eclipse.jetty.util.thread.QueuedThreadPool"><Set name="minThreads">10</Set><Set name="maxThreads">1000</Set></New></Set></Configure>

@Erel Maybe we need a new method srvr.Initialize2() in jServer to get a better control over the threading?

B4X:
public void Initialize2(BA ba, String EventName, int maxThreads, int minThreads, int idleTimeout) {
     this.ba = ba;
     this.eventName = EventName.toLowerCase(BA.cul);
     // NEW
     QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads,minThreads,idleTimeou);
     Server server =newServer(threadPool);

     context = new ServletContextHandler();
     context.setContextPath("/");
}
 
Last edited:
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
Hi Erel,
i think that the problem is real
my web disconnect is
B4X:
Private Sub WebSocket_Disconnected   
'    Dim resHistory As String
'        resHistory = "/misc/index.html"
'        WS.Eval("window.location.href = arguments[0]", Array As Object(resHistory))
    Try
        myTimer.Enabled = False
    Catch
        Log(LastException)
    End Try
End Sub

the line 1083 is in the timer and is ws.flush

Alain and Erel i can send you all page code if necessary.
 
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
no
it is not true
the server stop to respond to all connection after this error.
I have a big problems with a big production webapp with many many stops overy day.
 
Upvote 0

mindful

Active Member
Licensed User
Hi Erel,
i think that the problem is real
my web disconnect is
B4X:
Private Sub WebSocket_Disconnected  
'    Dim resHistory As String
'        resHistory = "/misc/index.html"
'        WS.Eval("window.location.href = arguments[0]", Array As Object(resHistory))
    Try
        myTimer.Enabled = False
    Catch
        Log(LastException)
    End Try
End Sub

the line 1083 is in the timer and is ws.flush

Alain and Erel i can send you all page code if necessary.

I can see that you commented out the lines (WS.Eval) from the WebSocket_Disconnected sub ... Note that when this sub is fired the WS object is closed so you shouldn't have any code related to the WS object in this sub.

As you said the problem is in your Timer as it is trying to do a WS.Flush when the WS object is closed ... In your timer you are sending data to the browser and as @Erel pointed out: The connection was broken while the data was sent so you see an error message. Do you have any "complex" or time consuming code in the timer ? What interval have you set for your timer ? Note the if your ws connection is closed/broken when your timer code runs it will give you that error.

Maybe you could try to setup a private boolean variable (like WSisConnected) and in WebSocket_Connected at the first line you set it to True and in WebSocket_disconnected also at the first line in the sub you set it to False and in your timer when you send data or when you use the WS object you can check if WSisConnected is True ...

Also as @alwaysbusy said: Have you tried checking if the websocket is available before running this code (if page.ws.open then)? does the same thing !
 
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
Hi, the error is not in the code it rise alse i stop correctly the timer and chack if connection is close.
the error occours after the execution of the WebSocket_Disconnected sub if the client close the webpage.
There is something in the jetty server that i can not see...
i see thet there are many many users that have the same problem..
how we can do to solve?
i see in
http://codenav.org/code.html?projec....jetty.websocket.common/WebSocketSession.java
that there is
B4X:
  @
234     public  ()
235     {
236           = .().();
237
238         if (( == .) || ( == .))
239         {
240             return ;
241         }
242
243         throw new ("RemoteEndpoint unavailable, current state [" +  + "], expecting [OPEN or CONNECTED]");
244     }
245
there is the way to not raise an error in this sub?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Let me say it again. The fact that you are seeing an error message when you close the page is not en error (by itself). As you can see from the stack trace the error was raised while the WebSocket was flushed. This can happen and it is fine.

The questions are:

1. Does the server stop working after this error? Does it stop handling new requests?
2. If so please post the logs of the consequent errors. Make sure to run your app in release mode (the logs posted in the first post are debug logs).
 
Upvote 0

billyrudi

Active Member
Licensed User
Longtime User
Ereil i I agree with you: it's fine!

but the server stop working and stop handling new requests.

how i can post you log in release mode?

Where i can read it?

i don't use nohup . and my develop machine is windows 10
 
Upvote 0
Top