Bug? B4J 4.20BETA Release jar doesn't close Server

Bruce Axtens

Active Member
Licensed User
Longtime User
Context: Windows 10, B4J 4.20 BETA and B4J 4.01

These steps,
B4X:
f = new Http("http://localhost:8888/demo").setVerb("GET").openConnection().sendTraffic().result()
f = new Http("http://localhost:8888/demo/shutdown").setVerb("GET").openConnection().sendTraffic().result()
or their equivalent using curl or wget, work properly with a Debug build. That is, the call to /demo/shutdown causes the binary to exit cleanly.

When built with Release, the call to /demo/shutdown is ignored and the server continues to operate.

I have stepped through a Debug build and everything seems to WAA (work as advertised.)

A demo project is attached.

-- Bruce/bugmagnet
 

Attachments

  • RestServer.zip
    2.4 KB · Views: 240
Last edited:

sorex

Expert
Licensed User
Longtime User
did you try with 2 objects (f1 & f2) instead of 1?

maybe it works in debug due to the slowdown the debugger causes.
 

Bruce Axtens

Active Member
Licensed User
Longtime User
did you try with 2 objects (f1 & f2) instead of 1?
No. It's using JScript and a fluent Http library of my own making which performs adequately in other REST situations.
maybe it works in debug due to the slowdown the debugger causes.
Very likely. So how does one add such a delay into the release version? Should one?
 

sorex

Expert
Licensed User
Longtime User
using 2 download objects instead of 1. executing the second one my interupt the first one since they use the same reference.
 

Bruce Axtens

Active Member
Licensed User
Longtime User
using 2 download objects instead of 1. executing the second one my interupt the first one since they use the same reference.
From the JScript REPL, running against a Release build:
B4X:
> var f1 = new Http("http://localhost:8888/demo").setVerb("GET").openConnection().sendTraffic().result()
undefined
> f1
true,200,http://localhost:8888/demo,Date: Wed, 03 Feb 2016 15:30:35 GMT,Content-Length: 26,Server: Jetty(9.3.z-SNAPSHOT),
> var f2 = new Http("http://localhost:8888/demo/shutdown").setVerb("GET").openConnection().sendTraffic().result()
undefined
> f2
true,200,OK,Date: Wed, 03 Feb 2016 15:30:57 GMT,Content-Length: 2,Server: Jetty(9.3.z-SNAPSHOT),
This demonstrates that the /demo/shutdown handler does return "OK" and that the Handler finishes. However, the server itself, launched with
B4X:
java -jar restserver.jar
does not shut down.
 

sorex

Expert
Licensed User
Longtime User
just wondering... why would you want to close the server? Isn't a server supposed to keep running?
 

Bruce Axtens

Active Member
Licensed User
Longtime User
For the same reason that I close https://github.com/lightbody/browsermob-proxy after I have finished with it. I run BrowserMob to capture a specific HAR object. Similarly, the current project should at least be able to be shut down even if, sometime in the future, i set it up as a service/daemon.
 

Bruce Axtens

Active Member
Licensed User
Longtime User
Please note that I have modified the original posting to include the fact that B4J 4.01 also suffers from this problem.
 

billzhan

Active Member
Licensed User
Longtime User
Please note that I have modified the original posting to include the fact that B4J 4.01 also suffers from this problem.


In debug mode , all http threads runs in the main thread.
In release mode, http thread run in its own thread (separated from the main).

Release mode : CallsubUtils (using a timer to execute, not immediately) -> http thread destoryed-> CallsubUtils destoryed(callsub not executed)

It may works as CallsubUtils may not be destroyed in debug mode.

B4X:
'main thread
Sub Process_Globals
    Public restServer As Server
    Public is2close As Boolean =False
    Dim timer1 As Timer
End Sub

Sub AppStart (Args() As String)
    restServer.Initialize("srvr")
    restServer.Port = 8888
       
    restServer.AddHandler("/demo", "Demo", False)
    restServer.AddHandler("/demo/shutdown", "Shutdown", False)
    restServer.Start
    Log("Server started")
    timer1.Initialize("timer1", 5000)
    timer1.Enabled=True
   
    StartMessageLoop
End Sub


Sub timer1_tick
    If is2close=True Then
        timer1.Enabled=False
        ExitApplication
    End If
End Sub


'http thread

Sub Handle(req As ServletRequest, resp As ServletResponse)
    resp.Write("OK")
    Main.is2close=True

End Sub
 

Bruce Axtens

Active Member
Licensed User
Longtime User
Marvellous! Thank you very much.
 
Top