Hi Monks,

I'm having a problem with a script which opens many filehandles. I'm on linux and my /proc/sys/fs/file-max is set to 340,000 and the system has only 10,000 files open when starting the script. I've cut it down to a small reproducable example:
#!/usr/bin/perl -w use strict; use Socket; use BSD::Resource; setrlimit(RLIMIT_NOFILE, 100000, 100000) or die "Cannot set rlimit - $ +!"; open OLDOUT, ">&STDOUT"; for my $max qw/1000 2000 4000 8000 16000/ { open STDOUT, ">&", \*OLDOUT or die "reset STDOUT - $!"; my @fh; my $count = 0; while ($count < $max){ open my $fh, ">/tmp/temp.$count" or die "open $count - + $!"; push @fh, $fh; $count++; } print "opened $count files\n"; my ($in, $out); my $buf; open STDOUT, ">/dev/null" or die "redirect - $!"; socketpair( $in, $out, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die + $!; open STDOUT, ">&=", fileno($out) or die ">&= fileno(fd) - $!"; open STDOUT, ">&=", $out or die ">&= fd - $!"; open STDOUT, ">&", \*OLDOUT or die "reset2 STDOUT - $!"; print "no problem with $count!\n"; } exit;


It generates the following output for me:

opened 1000 files no problem with 1000! opened 2000 files no problem with 2000! opened 4000 files >&= fd - Illegal seek at lotsoffiles.pl line 23.


I can't see any difference in the relevant snippet from strace perl lotsoffiles.pl 2>&1 |perl -ne 'print if /\((1|2),|dup/', which says:
write(1, "opened 2000 files\n", 18opened 2000 files dup2(2004, 1) = 1 fcntl(1, F_SETFD, 0) = 0 ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff2b62da20) = -1 ENOTTY (I +nappropriate ioctl for device) lseek(1, 0, SEEK_CUR) = 0 dup2(2005, 1) = 1 dup(2005) = 2006 dup2(2006, 2005) = 2005 fcntl(1, F_SETFD, 0) = 0 ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff2b62da20) = -1 EINVAL (I +nvalid argument) lseek(1, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) dup2(2005, 1) = 1 fcntl(1, F_SETFD, 0) = 0 ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff2b62da20) = -1 EINVAL (I +nvalid argument) lseek(1, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) dup(3) = 2006 dup2(2006, 1) = 1 fcntl(1, F_SETFD, 0) = 0 dup(3) = 4 write(1, "no problem with 2000!\n", 22no problem with 2000! dup2(4, 1) = 1 fcntl(1, F_SETFD, 0) = 0 fstat(4004, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 write(1, "opened 4000 files\n", 18opened 4000 files dup2(4004, 1) = 1 fcntl(1, F_SETFD, 0) = 0 ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff2b62da20) = -1 ENOTTY (I +nappropriate ioctl for device) lseek(1, 0, SEEK_CUR) = 0 dup2(4005, 1) = 1 dup(4005) = 4006 dup2(4006, 4005) = 4005 fcntl(1, F_SETFD, 0) = 0 ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff2b62da20) = -1 EINVAL (I +nvalid argument) lseek(1, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) write(2, ">&= fileno(fd) - Illegal seek at"..., 57>&= fileno(fd) - Ill +egal seek at lotsoffiles.pl line 23.

Is this a bug?? This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi on Debian etch

Thanks!!

In reply to perl bug? illegal seek with dup and many filehandles by flipper

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.