perlio 'unix' layer file descriptor handling

Discussion in 'Perl Misc' started by Rainer Weikusat, Jan 20, 2013.

  1. Something (AFAIK) undocumented which bit me recently[*]: The perlio
    'unix' layer maintains reference counts for all file descriptors used
    by 'Perl STREAMS' (yes, I mean STREAMS) and in particular, a close
    call performed on such a stream will only result in the file
    descriptor being closed when the reference count drops to zero because
    of that. This implies that 'nasty things' will happen when perl gets
    access to file descriptors managed by independent code and that

    open($fh, '<&=', $fd);

    should only be used of the file descriptor will henceforth be 'owned'
    by perl.

    [*] The particular problem situation was that a perl stream was
    creating using such an open call and the descriptor returned by the
    DBD::pg 'getfd' implementation specific interface. This was done so
    that the asynchronous interface of libpq could be used together with
    'a file descriptor based I/O multiplexing loop'. The (heavily
    customized) database driver used here would invoke PQFinish on the
    'internal' connection object when an attempt to establish a connection
    asynchronously failed, closing the file descriptor as side
    effect. Since this didn't destroy the 'perl stream' it remained in the
    unix layer file descriptor table with a reference count of one. In
    certain situations, the code would create a pipe using POSIX::pipe and
    turn the write end of that into a perl stream with another 'fdopen'
    open before the 'perl database streams' was destroyed. This pipe call
    would return the descriptor number formerly used by the database
    connection and the perl-internal reference count would thus become 2
    after the 'pipe writing' stream was opened. This pipe stream was used
    to write 'some commands' to an auxiliary program which would wait
    until an EOF on its standard input before performing any actual
    work. Because of the botched reference count, the 'close' call on the
    pipe stream would not close the file descriptor and the end-result
    would be the helper process waiting for the EOF and perl waiting (via
    wait) for the helper process to terminate.
     
    Rainer Weikusat, Jan 20, 2013
    #1
    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. Federico Bari
    Replies:
    2
    Views:
    1,978
    Federico Bari
    Aug 31, 2003
  2. Dhananjay
    Replies:
    1
    Views:
    1,168
    sloan
    Dec 18, 2006
  3. gordon

    perlio problem? redhat 9, perl 5.8.0

    gordon, Jun 24, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    144
    gordon
    Jun 27, 2003
  4. Dave
    Replies:
    0
    Views:
    152
  5. Replies:
    2
    Views:
    168
Loading...

Share This Page