alogrep 0 Posted May 2, 2023 Hello. This is a hard one for me to understand/solve. Using synapse on delphi 11. I have a main form that starts a thread (TTCPHttpDaemon) defined in a used Unit; TTCPHttpDaemon.create; The initialization section of the the blcksock.pas of sinapse, runs at the very beginning of the App seems to successfully create the socket synsock.WSAStartup(WinsockLevel, WsaDataOnce); I create the thread (log_it() is a function to log messages). TTCPHttpDaemon.Create; TRY inherited create(false); sock:=TTCPBlockSocket.create; FreeOnTerminate:=true; except on E:SysUtils.Exception do begin log_it('TTCPHttpDaemon.Create '+e.message); end; end; end; No errors. Then right here i get the inexplicable behaviour procedure TTCPHttpDaemon.Execute; begin with sock do begin try CreateSocket; except on E:SysUtils.Exception do begin log_it(e.message); end; end; bind(serverip,PORTNUMBER); //portno listen; repeat try b:= canread (1000); RIGHT HERE!!!!! except on E:SysUtils.Exception do begin log_it('****** Canread error: '+e.message); end; end; if b then ClientSock:=sock.accept; ................... on the canread() function the error is 'winsock not initialized' code:10093 And the main form is destroyed, by-passing the except block. What goes wrong, where? This happened suddenly without any change to the App that I could tell. Could it be one of the lates windows 10 updates? Share this post Link to post
Fr0sT.Brutal 900 Posted May 2, 2023 Probably you have the port occupied. You're not catching errors that could rise inside .listen Share this post Link to post
Remy Lebeau 1394 Posted May 2, 2023 16 hours ago, alogrep said: No errors. Then right here i get the inexplicable behaviour ... on the canread() function the error is 'winsock not initialized' code:10093 The WinSock library is reference-counted internally. WSAStartup() increments the refcount, and WSACleanup() decrements it. The library internals are initialized only on the 1st call to WSAStartup(), and cleaned up only on the last call to WSACleanup(). So, every successful call to WSAStartup() must be balanced with a call to WSACleanup(). The code you have shown looks fine (well, mostly ... your error handling needs work), which means that what you have described can occur only if SOME OTHER THREAD is making an unbalanced call to WSACleanup(), thus decrementing the refcount too much and unloading the library even though you are still using it. That is not the fault of the code you have shown, the problem is elsewhere in your project. What else is your project doing that uses WinSock outside of this TTCPHttpDaemon class? 16 hours ago, alogrep said: And the main form is destroyed, by-passing the except block. What do you mean? 16 hours ago, alogrep said: This happened suddenly without any change to the App that I could tell. Something had to have changed, behavior like this doesn't just change for no reason. Unless, this bug was likely always present in your project to begin with, and you just never happened to hit on it until now. 16 hours ago, alogrep said: Could it be one of the lates windows 10 updates? Doubtful. Share this post Link to post
Remy Lebeau 1394 Posted May 2, 2023 (edited) 9 hours ago, Fr0sT.Brutal said: Probably you have the port occupied. You're not catching errors that could rise inside .listen That would not cause error 10093 (WSANOTINITIALISED). That error can only happen if a WinSock function is called either before the 1st call to WSAStartup() or after the last call to WSACleanup(). If bind() or listen() failed, they should be raising their own exception, which would terminate the daemon thread before it reaches the marked line of code, since it is not catching them. Edited May 2, 2023 by Remy Lebeau Share this post Link to post
alogrep 0 Posted May 3, 2023 Thanlks Remy. Ah! Fool that I am!. I caused the problem. It is true that the App had not changed at all when this started to happen. But the condition of the data that the app reads had changed in these last few occasions. In short, I have a check function that if the result is negative it goes Application.terminate. This a couple of lines before the Thread>create(); Obviously the app continued for a few microseconds and the thread was created BUT when it got to the Execute part the application was terminated and the socket was closed. I rewrote the lines if not checked then begin Application.terminate; exit; end; Share this post Link to post
Fr0sT.Brutal 900 Posted May 3, 2023 18 hours ago, Remy Lebeau said: That would not cause error 10093 (WSANOTINITIALISED). That error can only happen if a WinSock function is called either before the 1st call to WSAStartup() or after the last call to WSACleanup(). If bind() or listen() failed, they should be raising their own exception, which would terminate the daemon thread before it reaches the marked line of code, since it is not catching them. Yeah, you're right. I misread the message as 'socket not initialized' Share this post Link to post