Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91912

Re: fork/exec & close file descriptors

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '(even': 0.05; '(so': 0.07; 'apis': 0.07; 'socket': 0.07; 'subject:file': 0.07; 'alain': 0.09; 'cc:addr:python-list': 0.10; 'wed,': 0.15; '(there': 0.16; 'child.': 0.16; 'closed).': 0.16; 'descriptors': 0.16; 'descriptors,': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'socket.': 0.16; 'wrote:': 0.16; 'duplicate': 0.18; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'terminate': 0.22; 'am,': 0.23; '2015': 0.23; 'split': 0.23; "i've": 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.25; 'idea': 0.26; 'linux': 0.26; 'message-id:@mail.gmail.com': 0.28; 'subject:/': 0.29; "i'm": 0.29; '(maybe': 0.29; 'fork': 0.29; 'parent': 0.29; 'restart': 0.29; 'allows': 0.30; "can't": 0.32; 'open': 0.33; 'file': 0.34; 'received:google.com': 0.34; 'programming.': 0.35; 'really': 0.35; 'handle': 0.36; 'but': 0.36; 'there': 0.36; 'child': 0.36; 'closing': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'sure': 0.40; 'why': 0.40; 'some': 0.40; 'close': 0.61; 'hang': 0.63; 'subject: & ': 0.73; 'child,': 0.84; 'chrisa': 0.84; 'common,': 0.84; "op's": 0.84; 'to:none': 0.90; 'notable': 0.91; 'imagine': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=u4I+QGE3iHDaoZ45PhM+h6JewiTd2sYkgYWkupEJLHg=; b=He5ITAW4A7V/XrP0p72U47LUqZuzeG7ss9mgwLlPsTj5ClPQiz1465FRqL/ICsNv07 ORoJnVrnyffeNB1BL4w0r4psTYWtwJiQk0dHaSKhLplxzKK3M8td8iPmoc9ILuVZTFdL uDsgbLmyd2Z6x+Yl85E2hQagBcx5uVRnEnf3U+FtNllT3h8tEndJ8Z9z4hHbEODc/NPo Wuo07dnKM6Sd1lG1EJxdgdJPOTfdkUYwc3H8+vXTRNLeuIk8F10YRdUXmt2cglMkPBFI uRxZBfaAYVtYYnmc/behe5SZHG0a4Hp3GVeDqirsC9YyGT3ahGJmJonLa5CwIkEaurCD ckvQ==
MIME-Version 1.0
X-Received by 10.50.141.164 with SMTP id rp4mr23741272igb.2.1433289246098; Tue, 02 Jun 2015 16:54:06 -0700 (PDT)
In-Reply-To <87lhg1lt00.fsf@universite-de-strasbourg.fr.invalid>
References <CANc-5UyeaWqThGFSAuGskz+S2Lrgq0ia9W9DHAie28t9GY+sww@mail.gmail.com> <mailman.59.1433253538.13271.python-list@python.org> <87pp5eksnc.fsf@universite-de-strasbourg.fr.invalid> <87eglt7u5i.fsf@elektro.pacujo.net> <87lhg1lt00.fsf@universite-de-strasbourg.fr.invalid>
Date Wed, 3 Jun 2015 09:54:06 +1000
Subject Re: fork/exec & close file descriptors
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.84.1433289254.13271.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433289254 news.xs4all.nl 2861 [2001:888:2000:d::a6]:36279
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91912

Show key headers only | View raw


On Wed, Jun 3, 2015 at 7:06 AM, Alain Ketterlin
<alain@universite-de-strasbourg.fr.invalid> wrote:
> I've no idea what the OP's program was doing, so I'm not going to split
> hairs. I can't imagine why one would like to mass-close an arbitrary set
> of file descriptors, and I think APIs like os.closerange() are toxic and
> an appeal to sloppy programming.

When you fork, you get a duplicate referent to every open file in both
parent and child. Closing them all in the child is very common, as it
allows the parent to continue owning those file descriptors (so that
when you close it in the parent, the resource is really closed). One
notable example is with listening sockets; bind/listen in the parent,
then fork (maybe to handle a client), then terminate the parent
process. You now cannot restart the parent without aborting the child,
as the child now owns that listening socket (even if it never wants to
use it). There are some specific ways around this, but not on all OSes
(eg Linux only added support for SO_REUSEPORT in 3.9), and the best
way has always been to make sure the children don't hang onto the
listening socket. (There are other good reasons for doing this, too.)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: fork/exec & close file descriptors Skip Montanaro <skip.montanaro@gmail.com> - 2015-06-02 08:58 -0500
  Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-02 18:28 +0300
    Re: fork/exec & close file descriptors Skip Montanaro <skip.montanaro@gmail.com> - 2015-06-02 10:42 -0500
      Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-02 19:24 +0300
        Re: fork/exec & close file descriptors Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-06-02 16:37 +0000
    Re: fork/exec & close file descriptors MrJean1 <MrJean1@gmail.com> - 2015-06-12 13:43 -0700
  Re: fork/exec & close file descriptors Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-02 17:59 +0200
    Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-02 23:05 +0300
      Re: fork/exec & close file descriptors Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-02 23:06 +0200
        Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 01:24 +0300
          Re: fork/exec & close file descriptors Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-03 09:21 +0200
            Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 10:41 +0300
              Re: fork/exec & close file descriptors alister <alister.nospam.ware@ntlworld.com> - 2015-06-03 09:38 +0000
                Re: fork/exec & close file descriptors Steven D'Aprano <steve@pearwood.info> - 2015-06-03 22:07 +1000
                Re: fork/exec & close file descriptors alister <alister.nospam.ware@ntlworld.com> - 2015-06-03 12:18 +0000
                Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 15:27 +0300
                Re: fork/exec & close file descriptors alister <alister.nospam.ware@ntlworld.com> - 2015-06-03 13:05 +0000
                Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 17:49 +0300
                Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 15:25 +0300
                Re: fork/exec & close file descriptors random832@fastmail.us - 2015-06-03 08:54 -0400
                Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 16:08 +0300
                Re: fork/exec & close file descriptors random832@fastmail.us - 2015-06-03 09:21 -0400
                Re: fork/exec & close file descriptors Chris Angelico <rosuav@gmail.com> - 2015-06-03 23:32 +1000
                Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 16:33 +0300
                Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-03 17:43 +0300
                Re: fork/exec & close file descriptors random832@fastmail.us - 2015-06-03 16:08 -0400
                Re: fork/exec & close file descriptors Marko Rauhamaa <marko@pacujo.net> - 2015-06-04 00:09 +0300
                Re: fork/exec & close file descriptors random832@fastmail.us - 2015-06-03 16:07 -0400
                Re: fork/exec & close file descriptors Chris Angelico <rosuav@gmail.com> - 2015-06-04 07:54 +1000
        Re: fork/exec & close file descriptors Chris Angelico <rosuav@gmail.com> - 2015-06-03 09:54 +1000
          Re: fork/exec & close file descriptors Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-03 09:11 +0200
            Re: fork/exec & close file descriptors random832@fastmail.us - 2015-06-03 08:21 -0400
              Re: fork/exec & close file descriptors Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2015-06-03 15:16 +0200
        Re: fork/exec & close file descriptors Skip Montanaro <skip.montanaro@gmail.com> - 2015-06-02 20:21 -0500

csiph-web