# Dir.foreach() crash

**URL:** <https://www.ruby-forum.com/t/dir-foreach-crash/159465>\
**Category:** Ruby\
**Created:** [February 4, 2009, 8:05pm UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465 "2009-02-04T20:05:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 4, 2009, 8:05pm UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465/1 "2009-02-04T20:05:42Z")

</div>

I continue to have a crash with a script I’ve written that recurses  
through a disk calling foreach() as needed on directories.

I am getting the following error:

D:/BackupScripts/trimMachines.rb:130:in `open': Invalid argument - x:\. \data\dubinin\vmap\v1180\soamafr\lib_180\trans\k\f\g\e (Errno::EINVAL) from D:/BackupScripts/trimMachines.rb:130:in`foreach’  
from D:/BackupScripts/trimMachines.rb:130:in `trimDirectory' from D:/BackupScripts/trimMachines.rb:135:in`trimDirectory’  
from D:/BackupScripts/trimMachines.rb:130:in `foreach' from D:/BackupScripts/trimMachines.rb:130:in`trimDirectory’  
from D:/BackupScripts/trimMachines.rb:135:in `trimDirectory' from D:/BackupScripts/trimMachines.rb:130:in`foreach’  
from D:/BackupScripts/trimMachines.rb:130:in `trimDirectory' ... 31 levels... from D:/BackupScripts/trimMachines.rb:193:in`trimMachines’  
from D:/BackupScripts/trimMachines.rb:178:in `foreach' from D:/BackupScripts/trimMachines.rb:178:in`trimMachines’  
from D:/BackupScripts/trimMachines.rb:244

I’ve checked and the directory mentioned does exist, the user account  
has full access to the directory, the directory is not empty, the  
directory name is \< 255 chars long, etc.

This script works for millions of files with directories that have  
much longer names and many more subdirectories than this example  
failure. The failures happen routinely after a few days of running the  
script and always on different directories.

I am running Ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] on  
Windows Server 2003 64 bit.

Does anyone have a clue what the problem could be? Is this a Ruby bug?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2009, 3:01am UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465/2 "2009-02-05T03:01:06Z")

</div>

You mean backslashes. Yeah, use File::SEPARATOR or just use explicit  
strings - ie single quoted

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2009, 2:19am UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465/3 "2009-02-05T02:19:37Z")

</div>

unknown wrote:

> I am getting the following error:
> 
> D:/BackupScripts/trimMachines.rb:130:in `open’: Invalid argument - x:.  
> \data\dubinin\vmap\v1180\soamafr\lib\_180\trans\k\f\g\e (Errno::EINVAL)
> 
> I’ve checked and the directory mentioned does exist, the user account  
> has full access to the directory, the directory is not empty, the  
> directory name is \< 255 chars long, etc.
> 
> This script works for millions of files with directories that have  
> much longer names and many more subdirectories than this example  
> failure. The failures happen routinely after a few days of running the  
> script and always on different directories.
> 
> Does anyone have a clue what the problem could be? Is this a Ruby bug?

Forward slashes are dangerous in strings because things like \v are  
escape characters?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2009, 4:40am UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465/4 "2009-02-05T04:40:09Z")

</div>

Julian L. wrote:

> You mean backslashes.

Whoops.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465/5 "2009-02-05T16:42:28Z")

</div>

unknown wrote:

> I am running Ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] on  
> Windows Server 2003 64 bit.
> 
> Does anyone have a clue what the problem could be? Is this a Ruby bug?

It sounds like it may be a window’s bug:

[http://74.125.95.132/search?q=cache:\_COKWkzEXhUJ:pullmonkey.com/2008/2/19/errno-einval-invalid-argument+Errno::EINVAL&hl=en&ct=clnk&cd=1&gl=us&client=safari](http://74.125.95.132/search?q=cache:_COKWkzEXhUJ:pullmonkey.com/2008/2/19/errno-einval-invalid-argument+Errno::EINVAL&hl=en&ct=clnk&cd=1&gl=us&client=safari)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2009, 4:10pm UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465/6 "2009-02-05T16:10:22Z")

</div>

On Feb 4, 8:00 pm, Julian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > much longer names and many more subdirectories than this example  
> > > Posted viahttp://www.ruby-forum.com/.

All 's in the script are entered as “\”. Like I said this script  
works for millions of files. Its doubtful only one of them has a \v in  
it. Also this script will not always fail on this directory which says  
this is not the problem.

This is not the first problem I have had with Ruby on this (64 bit)  
machine. If I run the two backup/trim scripts I have written at system  
startup they eventually quit and I can’t see the error messages  
because they have no visible console. If I run them from consoles  
explicitly the backup script never terminates and the trim script dies  
after a few days.

Is it possible that the two scripts are accessing the directory at the  
same time and somehow that is causing some kind of interference? The  
backup script would be creating directories and putting files in them.  
So the trim should not be thrown off. If in the trim script a previous  
Dir.foreach in the parent directory found this directory then the  
Dir.foreach in the child should certainly find the directory. Again  
this seems like a Ruby bug.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 5, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/dir-foreach-crash/159465/7 "2009-02-05T19:45:36Z")

</div>

On Feb 5, 9:41 am, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted viahttp://www.ruby-forum.com/.  
> Thanks for the pointer. I am not running Rails/Mongrel. Just ruby  
> scripts that manipulate files on a server on Windows. So I don’t know  
> if this is the problem but it does give me testable solutions.

FYI the described problem at that URL is not a Windows bug. Its a Ruby  
(or Mongrel) on Windows bug.
