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: 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: <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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Jun 3, 2015 at 7:06 AM, Alain Ketterlin 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