in reply to cleanly exiting threads

On the basis of what you've said, it seems likely that the problem arises because you are using IPC::Open2 in conjunction with threads. IPC::Open2 "forks" the command to be processed. Under Win32, fork is implemented under the covers using a pseudo-process--which is a thread!

It seems likely that if you are sure that all your threads are ending cleanly, that the extra thread is that created under the covers by the call to open2().

A few possibilities come to mind, but are untested.

The 'pid' returned from open2() is actually a thread id. Use $threadObj = threads->object( $pid ); to obtain a handle to that thread and then either:

prior to exiting the program.

Or, if your code is otherwise working correctly except for the exit time warning, accept it is just a warning and ignore it.

Other possibilities that might work, but without more information (code to look at and try) are just speculation, include:

Really, the first thing you need to determine is whether the extra running thread is one you have created or one started by open2. The best way to determine that is by using something like ProcessExplorer to determine what threads are still running when the program s about to exit. ProcessExplorer allows you to view the process and examine the threads including their system-assigned thread IDs (which are different from those used by the threads!).

The problem here is that there are four identifiers for every Perl created thread:

  1. The system assigned ThreadID. A system-wide unique non-zero integer.
  2. The system assigned thread handle. An opaque object handle to system internal data structures.
  3. The threads object handle. A Perl assigned blessed reference.
  4. The Perl-assigned, process-unique integer.

threads has a private method:_handle() which will give you the system assigned thread handle associated with a given (or current) Perl threads object. However, the identifier in ProcessExplorer is the system assigned thread ID. In order to discover which thread is still running at termination, it will be necessary to inspect the process (with ProcessExplorer) and note the thread IDs of the still running threads and relate them to the threads you've started and finished.

The only way I know of doing that is to use the system API GetCurrentThread(), but that will involve using Inline C, or XS, or Win32::API.

If you can determine for sure what thread is still running, then you have a start on working out why that is the case. If you can produce a runnable, cut-down version that demonstrates the problem, I might have a go at trying to come up with a solution.


  1. 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.

Replies are listed 'Best First'.
Re^2: cleanly exiting threads
by JoeKamel (Acolyte) on Aug 14, 2008 at 01:57 UTC

    Okay, I'm running under linux so my understanding that an open2 is actually a fork, correct?

    I have verified that my

    $kpid = waitpid ($pid, 0);
    at the end of the while loop is returning with $kpid always matching $pid -- which i believe means that the process is actually done.

    Having said that, I have no idea what the binaries that I am running with the open command may be doing. they maybe not quite finishing, so, maybe doing an explicit kill would be better than just waiting...or perhaps send the sigint, (which they all handle), then leave the waitpid in place. may be more reliable

    So thanks for the thoughts, i'll have to dig in again tomorrow.
      I'm running under linux

      Whoops! I completely missed that detail. Which kinda renders most of what i wrote redundant.

      In that case I can't offer much information, only an inate suspicion of mixing threads and fork. I did have some more to say about that, but reviewing it, I cannot find anything to back up my suspicions, so I'll just back out of this and wish you good luck.


      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.