syswrite "Bad file descriptor" after successfully writing to thatfile handle

Discussion in 'Perl Misc' started by Robert Jacobson, Jan 7, 2008.

  1. I'm having some trouble figuring out an error that I'm getting.
    syswrite is failing to write; $! is "Bad file descriptor". What
    boggles my mind is that this is after some data had already been
    written successfully to the file handle!

    This problem seems to be linked to a recent change I made to my
    program. My program is basically a network socket data collector,
    running on Windows with ActivePerl 5.8.8 (build 819)

    - open data file (sysopen)
    - connect to server socket
    - while read from server socket OK
    - write data to file
    - if X time elapsed:
    - close file
    - open new file

    I recently changed my program to add "use threads", and add a
    conversion program in that thread

    ....
    - if X time elapsed:
    - close file
    - new thread for &convert_file(). I call $thread->detach
    immediately after creation
    - open new file

    After I made the above change, syswrite started *occassionally*
    returning undef, and setting $! to "Bad file descriptor". Again, most
    of the time it works. When it fails, it does seem to be while
    convert_file() is still running. It takes about 10 seconds to convert
    the file; that's why I'm using a separate thread. The program writes
    about 400 kB to the file, then I get the "Bad file descriptor" error.

    For kicks, I tried changing syswrite() to print(), but it also seems
    to be having trouble.

    So, what should my next troubleshooting steps be?
    Robert Jacobson, Jan 7, 2008
    #1
    1. Advertising

  2. On Jan 7, 10:31 am, Robert Jacobson <> wrote:

    > - if X time elapsed:
    > - close file
    > - new thread for &convert_file(). I call $thread->detach


    I think I have fixed the problem, though I still don't know why it was
    occurring.

    In convert_file(), I was using a system() call to start an external
    perl script. I basically copied the perl code from that external
    script into convert_file(), and the problem seems to have gone away.
    At least, I didn't have any problems in the past 16 hours or so.

    If anyone can tell me -- is using system() inside a thread "bad" for
    some reason? If so, why?
    Robert Jacobson, Jan 8, 2008
    #2
    1. Advertising

  3. Robert Jacobson

    Ben Morrow Guest

    Quoth Robert Jacobson <>:
    > On Jan 7, 10:31 am, Robert Jacobson <> wrote:
    >
    > > - if X time elapsed:
    > > - close file
    > > - new thread for &convert_file(). I call $thread->detach

    >
    > I think I have fixed the problem, though I still don't know why it was
    > occurring.
    >
    > In convert_file(), I was using a system() call to start an external
    > perl script. I basically copied the perl code from that external
    > script into convert_file(), and the problem seems to have gone away.
    > At least, I didn't have any problems in the past 16 hours or so.
    >
    > If anyone can tell me -- is using system() inside a thread "bad" for
    > some reason? If so, why?


    Likely some interaction between system and threads and pseudo-fork. Can
    you post a *minimal* example that reproduces the problem, including your
    versions of perl, threads and threads::shared? Have you tried upgrading
    to the latest threads?

    Ben
    Ben Morrow, Jan 8, 2008
    #3
  4. On Jan 8, 12:00 pm, Ben Morrow <> wrote:
    > Quoth Robert Jacobson <>:
    >
    >
    >
    > > On Jan 7, 10:31 am, Robert Jacobson <> wrote:

    >
    > > > - if X time elapsed:
    > > > - close file
    > > > - new thread for &convert_file(). I call $thread->detach

    >
    > > I think I have fixed the problem, though I still don't know why it was
    > > occurring.

    >
    > > In convert_file(), I was using a system() call to start an external
    > > perl script. I basically copied the perl code from that external
    > > script into convert_file(), and the problem seems to have gone away.
    > > At least, I didn't have any problems in the past 16 hours or so.

    >
    > > If anyone can tell me -- is using system() inside a thread "bad" for
    > > some reason? If so, why?

    >
    > Likely some interaction between system and threads and pseudo-fork. Can
    > you post a *minimal* example that reproduces the problem, including your
    > versions of perl, threads and threads::shared? Have you tried upgrading
    > to the latest threads?
    >
    > Ben


    Thanks for responding.

    I'll see if I can reduce it to a minimal example and post it here.

    I am using ActivePerl 5.8.8 (build 819) (32-bit). The threads version
    is whatever ActivePerl came with. I did not use threads::shared.
    Robert Jacobson, Jan 11, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luc Saffre
    Replies:
    6
    Views:
    554
    Luc Saffre
    Jan 24, 2004
  2. Replies:
    9
    Views:
    701
    Abhijit Soman
    Jan 6, 2005
  3. Vishnu
    Replies:
    0
    Views:
    499
    Vishnu
    Jan 6, 2005
  4. Yary H
    Replies:
    0
    Views:
    120
    Yary H
    Oct 3, 2004
  5. Ben Bullock
    Replies:
    11
    Views:
    490
    Peter J. Holzer
    May 20, 2006
Loading...

Share This Page