Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

rmtree() is failing after some time

by KristinaBurden (Initiate)
on Apr 23, 2009 at 07:08 UTC ( #759482=perlquestion: print w/replies, xml ) Need Help??

KristinaBurden has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a perl program that keeps running on server. it creates a temp directory and this directory is supposed to be deleted after the it is done with its job. I am deleting the temp dir using rmtree() function. rmtree works well for initial 10-12 jobs after that it fails to delete the temp folder with the error - "failed to fetch initial working directory". Any help is appreciated. code: if(-e $tempDir) { File::Path::rmtree($tempDir,0,0); }

Replies are listed 'Best First'.
Re: rmtree() is failing after some time
by cdarke (Prior) on Apr 23, 2009 at 07:21 UTC
    Could be a permissions issue. Check $!, or you might have to run your script under strace or truss (if you have them) to get the real error.
      I am a newbie in Perl. What is strace or truss? Point here is that rmtree() is able to delete temp folders initially but after some time it starts giving this error. And I restart my perl program then also, initially it is able to delete them but after 20-25 folders it starts giving error. My perl program creates threads(8-10), each thread creates a temp folder, processes it and then deletes it. Initially all threads behave well. After some time, error starts appearing.

        See strace/truss. It's a runtime call monitor for Perl, to find out where the problem is.

        failed to fetch initial working directory

        Is this the exact error message you get? I have a feeling that it's an issue with threading (you didn't mention in your post you use threads). Maybe you should post more code so we can take a look at it.

Re: rmtree() is failing after some time
by Fletch (Chancellor) on Apr 23, 2009 at 13:05 UTC

    And while not directly germane to your problem your code has a problem:

    You should be aware that checking for the existence of a file (or directory) and then doing something to/with that same file can introduce a race condition which can cause (at best) intermittent bugs or (worse) have security implications, so in general you shouldn't write code that way. It's better to go ahead and take the action and deal with a failure or exception if it occurs.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Maybe eventually its using same temp directory among two threads
Re: rmtree() is failing after some time
by gwadej (Chaplain) on Apr 23, 2009 at 13:25 UTC

    I know it will sound ridiculous, but make certain your current directory is not within the directory tree you are deleting.

    I had this happen in a previous system where an in-house module was changing the directory on me. Under Linux, this did not cause an immediate problem (you can delete your current directory). On Windows, it caused an immediate failure.

    G. Wade
      Before calling rmtree() I am already making sure that I change current directory to a directory that always exists and is not within the directory I am trying to delete.
Re: rmtree() is failing after some time
by Anonymous Monk on Apr 23, 2009 at 09:34 UTC
Re: rmtree() is failing after some time
by spx2 (Deacon) on Apr 24, 2009 at 15:32 UTC
    you can just do if(-e $tempDir) { `rm -rf $tempDir` } and you will fully benefit :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://759482]
Approved by ELISHEVA
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2021-12-09 14:41 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (36 votes). Check out past polls.