Page 1 of 1

Closing sockets when exiting

Posted: Mon Nov 11, 2013 7:38 am
by WinstonJenks
I have a problem with my standalone not actually exiting. It seems the window goes away, but I still see the process in the Windows 7 Task Manager. The issue has something to do with closing all the sockets. The stack has only one card, and the card script does an

Code: Select all

on opencard
  accept connections on port 7777 with message sClientConnected
end opencard

That all works fine, the socket sends data back and forth. In the closeCard handler of the card, I have

Code: Select all

on closecard 
  repeat for each line tSocket in the opensockets
    --answer "Closing socket" && tSocket
    close socket tSocket
  end repeat
end closecard
So here is the weird thing. When I build my standalone (on Windows), if the answer command is commented out, then the process never quits. If the answer command is uncommented, then it shows the proper message AND the process quits! There is only one socket that is being closed, it is the 7777 socket that was opened for connections initially. ( I have closed all the other sockets in use before we get to this point).
Riddle me that, Batman!

Could it be that the close socket command is asynchronous, and only completes later after trying to fire a socketClosed message to a non-existant card? But since the card is already gone, it just hangs? Seems weird because the answer command is before the close socket command and should not really hold up the application after it is dismissed.

Re: Closing sockets when exiting

Posted: Mon Nov 11, 2013 7:50 am
by WinstonJenks
Forgot to mention this is with LiveCode 6.1.2.

Re: Closing sockets when exiting

Posted: Mon Nov 11, 2013 9:13 pm
by Simon
Hi WinstonJenks,
Sorry about this answer because I don't like it myself.
Try

Code: Select all

on closecard 
  repeat for each line tSocket in the opensockets
    close socket tSocket
wait 10 with messages
  end repeat
end closecard
Sometimes The processor needs time to catch up with itself.
Because your answer dialog paused the events I'm thinking a wait will do the same for you. (maybe it should be above the close?)

Simon

Re: Closing sockets when exiting

Posted: Tue Nov 12, 2013 7:05 am
by WinstonJenks
Tried the "wait 10 with messages" suggestion on version 6.1.1. Bad things happened. I got a script error, "execution error at line 31 (wait aborted), char 1" once while I was exiting the IDE. Most of the time, it exits Ok. I have not seen any issues when the standalone exits. And the suggestion does cause the process to be removed from the Task Manager. <NowTheGuessingBegins>It almost looks like the closestack handler gets fired, but the rest of the application does not wait for it to finish before exiting. When the process (or thread, or something) exited and the wait was still going on, the OS aborted the wait and returned an error which the script processor dutifully noted.</NowTheGuessingBegins>

Re: Closing sockets when exiting

Posted: Tue Nov 12, 2013 7:13 am
by WinstonJenks
I moved the "wait 10 with messages" out of the repeat loop and wrapped it in a try/catch/end try block. That seems to help.