Forum > General

Killing off stucked thread

(1/4) > >>

TCH:
I have a thread with a HTTP GET request inside. The problem is, when the request does not returns (i had set IOTimeout to 3000 for TFPHttpClient, but no avail, it does not returns after 3 seconds), the thread stucks. Even if i set the Terminated property to true, it does no good, since the thread never gets to that part when it can exit if it's terminated. (Or maybe it does, but only after a lot of time...)

The question is: how can i kill this stucked thread safely? Without causing crashes or freezes.

Or maybe i'm doing wrong the timeout setting? I just have to set the IOTimeout to 3000 for 3 seconds, or aren't i?

marcov:
Killing threads is never safe. Usually you can get away with it, but it leaks memory and risks deadlocks.

The best thing you could try to do is to force some error on the socket handle (by calling some socket function on it), and hope it gets out whatever it is stuck in and honors TERMINATED.

Such tricks, like killing threads however, are not very portable, since it depends on implementation details.

TCH:
I see, thanks. Well, this rarely happens and there is no allocated memory or opened files in the thread, when the GET stucks, so if there is no crash, then it's okay.

How can i call a socket function on a local TFPHttpClient from a different thread? Because if it snaps out from the GET, then it does not need to be terminated at all.

Any idea, why TFPHttpClient ignores IOTimeout?

balazsszekely:

--- Quote ---Any idea, why TFPHttpClient ignores IOTimeout?
--- End quote ---
IOTimeout is a connection timeout, it won't help if the server is online, but busy with something else(slow maybe). However there is a solution, in the latest versions of fpc(3.1.1 +) there is a Terminate method which will force FPHttpClient to abort immediately after the current chunk is received/send. If switching to FPC trunk is not an option for you, just extract the attached fphttpclient.pp unit to your project folder and you're good to go. Please do the following:
1. Measure your own time
2. After a predefined number of seconds, just call FPHttpClient.Terminate

PS: I tested this method extensively in my applications and it works well.

TCH:
Thanks for the tip, but still, how could i call FPHttpClient.Terminate on a locally declared FPHttpClient? Maybe i should pass it's pointer to a global variable and handle it from a TTimer? Can i do that?

Navigation

[0] Message Index

[#] Next page

Go to full version