Console network application crashes when closed

Just starting out? Need help? Post your questions and find answers here.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Console network application crashes when closed

Post by Oso »

I find often that PureBasic routines crash on being closed, when they contain network-related functions, even for the very simplest network applications. In this particular case, my console application works perfectly in all respects, except if I close its console window using the top-right button while there is an active connection.

Image

Image

Curiously, when I close the window in this way, the application continues and executes the line PrintN("Server has closed...") although it doesn't execute this if I close the console application by pressing CTRL/C. The line PrintN("Server has closed...") is not executed until I click the application close button, which suggests to me that there is some wrapping-up being done, perhaps with a bug that doesn't like the fact that a network connection was active. I don't seem to be able to resolve this, as it's being done outside my control.

Code: Select all

        netevent.i = NetworkClientEvent(netservercon.i)           ; Obtain the event, if any
        Select netevent.i                                         ; Check for event types
            
          Case #PB_NetworkEvent_Data
            .
            .
            .
          Case #PB_NetworkEvent_Disconnect

             PrintN("Server has closed the connection : " + nethostname)
If I use the debugger and set a breakpoint just after the PrintN(), it stops there and highlights the breakpoint but at the same time says "The debugged executable quit unexpectedly", so I can't single-step through to see what's happened.

If I use the purifier, it highlights a call to an unrelated procedure responsible simply for outputting text to the console, saying 'memory error etc.'. It's the same call that's sending the message "Server has closed the connection" and therefore nothing wrong with that procedure. It seems that something PureBasic is doing to wrap-up networking usage, contains a bug.
Last edited by Oso on Sun Dec 03, 2023 2:39 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 6764
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Console network application crashes when closed

Post by infratec »

No time left, but you can try this:

Code: Select all

#SIGINT	=	2	; Interactive attention
#SIGILL	=	4	; Illegal instruction
#SIGFPE	=	8	; Floating point error
#SIGSEGV = 11	; Segmentation violation
#SIGTERM = 15	; Termination request
#SIGBREAK	= 21	; Control-Break
#SIGABRT = 22	; Abnormal termination (abort)


Procedure sigHandler(sig.i)
  Debug "Signal " + Str(sig) + " received, exiting"
EndProcedure

Import ""
  signal(i, n)
EndImport


signal(#SIGINT, @sigHandler())
signal(#SIGBREAK, @sigHandler()); handles Ctrl-Break on Win32
signal(#SIGABRT, @sigHandler())
signal(#SIGTERM, @sigHandler())
If you reach the signal handler you can set a variable to close gracefully.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Console network application crashes when closed

Post by Oso »

infratec wrote: Sun Dec 03, 2023 1:39 pm If you reach the signal handler you can set a variable to close gracefully.
Many thanks for this Infratec, I see it works for a small test application, returning "Signal 21..." in the debugger, but for my network application, it doesn't execute it. The behaviour remains the same, crashing, with the message "The debugger executable quit unexpectedly".

It's a strange one to understand. I've removed a lot of code to strip it to the bare minimum but it still crashes.

The crash doesn't actually prevent the application from being used, because it is to be executed as a background process (via the task manager when the user logs-in), so there will not normally be an opportunity to close it in this way.

The reason I wanted to show this, is because I often find that my PB network routines crash easily, so I'm trying to find a way to show it happening, enabling it to be tried by others and hopefully resolved. I still need to put a simple demo programme together, to demonstrate it.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Console network application crashes when closed

Post by Oso »

Actually, some more testing has revealed that PB is executing Case #PB_NetworkEvent_Disconnect after it's already started clearing memory and resources. I think this is a common problem I've been seeing with network applications when they are closed. It's calling my procedure MessageOut() when the application is closed, which then proceeds to write the message to a log file, within procedure UpdateLog().

Just to be clear here, I'm not asking it to run MessageOut() when the application is closed — PB is doing that, presumably the result of having triggered the Case #PB_NetworkEvent_Disconnect.

If I turn off message logging in MessageOut() with logs.i = 0, then it doesn't crash. PB has obviously started clearing-up memory and resources, yet still insists on executing my code under Case #PB_NetworkEvent_Disconnect.

Code: Select all

; **
; ** Output log messages with prefixed time
; **
Procedure MessageOut(message.s)
  If runmode.b                                                          ; Output to console (runmode is a global)
    PrintN(FormatDate("%hh:%ii:%ss", Date()) + " C : " + message.s)
  EndIf
  If logs.i                                                             ; Output to log file if number of log files specified above zero
    UpdateLog(FormatDate("%hh:%ii:%ss", Date()) + " C : " + message.s)
  EndIf
EndProcedure




    netevent.i = NetworkClientEvent(netservercon.i)           ; Obtain the event, if any
    Select netevent.i                                         ; Check for event types
            
      Case #PB_NetworkEvent_Data
            .
            .
            .
      Case #PB_NetworkEvent_Disconnect

	;   logs.i = 0   ;  <---- Set to zero here prevents the crash (I cannot possibly do this however,
	;   because I need to log normal everyday network disconnections)
	;
        MessageOut(Str(netservercon.i) + "Server has closed the connection : " + nethostname.s + " : " + netportno.i)
        
PB is executing the Case #PB_NetworkEvent_Disconnect when the user closes the application, even though it can't successfully execute my procedure which is responsible for logging console message output. Why is PB doing that? Shouldn't it abandon the application as instructed to do, and close it, exactly as it does with CTRL/C ?
User avatar
mk-soft
Always Here
Always Here
Posts: 5216
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Console network application crashes when closed

Post by mk-soft »

No complete code to find out what your problem is.

The network server should be processed in a thread.
To terminate the programme, the thread must first terminate its server.
The main programme must wait until the thread of the server is terminated.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Console network application crashes when closed

Post by Oso »

I will try to reduce it down to a bare example as I said. I should point out here that it only appears to be crashing because the routine writes to a log file and that PB may have closed the log file, but I need to investigate further. I tend to see it as the question — should the user's code under Case #PB_NetworkEvent_Disconnect, suddenly be executed as the consequence of clicking close. If that's normal, then we have to work around that, but I'm surprised that it does it. :D
Post Reply