B4J Question [SOLVED - ALMOST] Docker Container HealthCheck - How does a WebApp work actually?

hatzisn

Expert
Licensed User
Longtime User
Good evening everyone,

I am creating a docker container healthcheck. Such a healthcheck is any application that can run inside the container a test and exit with 0 if everything is ok or exit with any positive number if healthcheck is not ok. This result is noted by docker to know the health of the container. I have done it and it works when the WebApp I want to check is not running (it exits with a positive number) and when it is running (it exits with 0). The problem arises when I want to break functionality in my webapp intentionally to check the healthcheck.

I tried with creating a handler that assigns to an int variable the division 1/0 but nothing changed (not even the handler broke). Healthcheck is 0. Then I assigned to an int variable the division "nikos"/0. This created an exception when running but when I checked the healthcheck by calling the healthcheck handler in the webapp it still reports that it gets a correct response. Healthcheck is again 0. What is happening here? If the code in one handler breaks the rest of the app is left untouched?
 
Last edited:

hatzisn

Expert
Licensed User
Longtime User
Gemini to the rescue:

Prompt:

If I run in two seperate threads two voids and in one of them the code breaks does the whole application stop working?


Response:

No, the application will not stop working.
In Java, each thread runs independently and has its own isolated call stack. If an unhandled exception occurs and "breaks" the code in one thread, only that specific thread will terminate. The other thread and the rest of your application will continue running normally.
The only exceptions to this rule are:
  1. If the thread throws a critical, system-wide error that crashes the entire JVM (such as an OutOfMemoryError).
  2. If your code catches the exception and explicitly terminates the program using System.exit().
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
It is not solved completely. I tried to run the problematic code in Main with a CallSubDelayed from the handler. It breaks but it is still running and responds correctly to the healthcheck request. If the code breaks in Main isn't this the main thread?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Do you mean B4J server?

A handler can be set single or multithreaded.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Do you mean B4J server?

A handler can be set single or multithreaded.

Hi, @aeric. Wow, still awake?

I know it about the handler that each one runs in its own thread if it is multithreaded, or just in one thread, yet separate, if it is single threaded. I did not know that breaking the code in a seperate thread does not break the application in whole.
The question though is the following: If I call from a handler with CallSubDelayed a code in Main that is a breaking code (f.e. the following) and indeed the code will break if I visit the handler, doesn't the main thread break so the server object cannot serve?

B4X:
'This code is called from the handler with CallSubDelayed and it is in Main
Private Sub BreakTheCode
    Dim iErr As Int = "nikos"/0
  
End Sub
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
New findings. In order to call the breaking code I call it from a handler named /myerror. If I call it once, I see the unhandled exception in logs. If I clear the logs and call again /myerror I do not see an exception which means the main thread was broken. I still do not get it though. If the main thread is broken and the srvr (server) object is declared in the main thread how can the healthcheck handler still serve requests?
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
I am getting out of my mind. I cannot believe it. Main thread breaks and server object still serves requests in healthcheck handler in which I have changed the response to display also time. In each request I see current time in format HH:mm:ss so it is still serving current requests.
 
Upvote 0
Top