What would be the right way(tm) to kill a hanging thread as far as proper garbage collection goes?

There is no right way to kill a thread.

Even at the OS level, the TerminateThread() api--if your OS has one--is designated an action of extreme last resort:

Remarks

TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code. DLLs attached to the thread are not notified that the thread is terminating. The system frees the thread's initial stack.

Windows Server 2003 and Windows XP/2000: The target thread's initial stack is not freed, causing a resource leak.

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination.

And that's from C. From Perl, you simply have no way of meeting those requirements.

Not sure if that's the best thing but it seems to work better than setting an LWP timeout.

The LWP timeout is a generic setting that it tries to apply across a wide range of protocols and phases of the communication process. But during socket operations there are many phases. The initial connection; the ack/nak responses; the transmission of blocks of content and the associated handshaking; the shutdown etc.

If, for example, you are talking to a slow or busy server, it is quite possible for each phase to take just less than the timeout value you set. With the result that the overall time for the communication can be several time longer than the timeout value. Hence, you are left wondering why did it continue for 30 or 40 seconds when I set a 10 seconds timeout? And the answer is because no individual stage of the communication broached the 10 second limit.

If your use of alarm is meeting your requirements, then it is as good as any other way. I've used it myself successfully, but I have found that there appear to be some points in the process of communicating with a server that it won't successfully interrupt due to "safe signals".

If you find that you sometimes get servers that cause your threads to hang around blocked for longer than is desirable, despite your use of alarm, then you might want to try adding Perl::Unsafe::Signals to your eval block. See the module's synopsis for how.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re^7: threads: work crew memory leak by BrowserUk
in thread threads: work crew memory leak by rakzer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.