ezthreads icon indicating copy to clipboard operation
ezthreads copied to clipboard

The test TestForceKill and undelaying KillThread function does not kill the thread as experienced before under Linux

Open bigopensky opened this issue 2 years ago • 3 comments

Hi, thank you for your great units. The code is easy to use.

I'm running Ubuntu with FreePascal 3.2.0 and Lazarus 2.0.12 and I use the matter it to check if the KillThread(ThreadID) behavior of the cthreads has common issues.

I have the issue that the KillThread(ThreadID) won't kill anything despite of giving a positive call result of 0, as written in the handbook. The thread continues the Execute part (..see attached script TestAsync.zip).

I have used your test program ezthreads_tester.lpr, extract the relevant part and added some timestamps (...see TestEzAsync.zip), to get a "second opinion" and to see if the KillThread issue is a "feature" under Linux. It seems to me, that there are the same issues in both programs (TestAsync and TestEzAsync). As you see I set the sleep time of the thread to 10 s, the program should be killed after 50 ms but the the program is running through the complete 10 s defined by the sleep time (..see CONFIG TestEzAsync and OUTPUT TestEzAsync).

I got similar results with my clunky command loop test when i call kill the execution process remains and jumps back to the next input prompt (OUTPUT TestAsync).

** Question 1** Is this behavior for cthread Linux specific and KillThread(...) malfunctioned?

** Question 2** Is there a possibility, to solve that issue BEYOND the permanent commented common solution, to flag the Execution procedure to stop at a certain point (in a loop for example)? I'd like to use the timeout-kill feature in a socket connection context (SQL request for example).

CONFIG TestEzAsync

  LThread:=TEZThreadImpl.Create;
  LThread
    .AddArg('sleep',10000)  // < -------------- HERE
    .AddArg('start',Now)
    .Settings
      .UpdateMaxRuntime(50) // <------------ HERE
      .UpdateForceTerminate(True)  //non-graceful termination, but guarantees no dangling threads after timeout
      .Thread
    .Events
      .UpdateOnStopNestedCallback(@CheckElapsed)
      .Thread
    .Setup(@Setup)
    .Start;

OUTPUT TestEzAsync

TestForceKill: force kill start 30-12-99 13:27:07
TestForceKill::CheckElapsed::elapsed time - 51
TestForceKill::force kill finished 30-12-99 13:27:17 <------- 10 s left

OUTPUT TestAsync

------- START --------
PROMPT> kill
KILL.REQUEST: 30-12-99 13:46:18
KILL.RESULT:  0 0 - means OK 30-12-99 13:46:18 <------- should stop here 
PROMPT> exit
----- READY ----------
```

TestAsync.zip TestEzAsyn.zip

bigopensky avatar Nov 12 '22 13:11 bigopensky

Hey @bigopensky 👋

Thanks for the kind words on my code. I'll look into your example within a day or two and write back when I've had some time to test.

mr-highball avatar Nov 13 '22 05:11 mr-highball

Without looking on my computer or opening an Ubuntu instance this is just a guess, but could you try and add a Yield(); after the sleep; function call on this line (in ezthreads.pas of your local copy of the library)

https://github.com/mr-highball/ezthreads/blob/master/src/ezthreads.pas#L705

And after this sleep

https://github.com/mr-highball/ezthreads/blob/master/src/ezthreads.pas#L718

If I don't hear back from you I'll still be looking into it and will reply but this may be quicker since you already have the tests / OS

mr-highball avatar Nov 13 '22 05:11 mr-highball

Hm ... Yield is unknown in that part of these functions.

bigopensky avatar Nov 14 '23 16:11 bigopensky