Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'explicitly': 0.05; 'level,': 0.07; 'subject:file': 0.07; 'exec': 0.09; 'pep': 0.09; 'restart': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '2.7': 0.14; '10:59': 0.16; 'backward': 0.16; 'descriptors': 0.16; 'fds': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'number?': 0.16; 'underlying': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; 'library': 0.18; '(the': 0.22; 'cc:addr:python.org': 0.22; 'skip': 0.24; 'url:dev': 0.24; 'cc:2**0': 0.24; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'file': 0.32; 'stuff': 0.32; 'open': 0.33; 'url:python': 0.33; 'bugs': 0.33; 'basic': 0.35; "can't": 0.35; 'possible.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'dance': 0.36; 'url:org': 0.36; 'files': 0.38; 'pm,': 0.38; 'sure': 0.39; 'changed': 0.39; 'how': 0.40; 'even': 0.60; 'manually': 0.60; 'break': 0.61; 'new': 0.61; 'simply': 0.61; "you'll": 0.62; 'skip:n 10': 0.64; 'hours': 0.66; 'due': 0.66; 'close': 0.67; 'subject: & ': 0.68; 'default': 0.69; 'discover': 0.82; '2015': 0.84; '3.4': 0.84; 'to:none': 0.92 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=HBBNW358FMNiU4dU4wgzz5hfR/RKefdWIgdav3sE+Zc=; b=aWtFnmBQ4B3dNgOQIiAOnjDu5xfAOMWMx5ShXfqFTUJ1/CK6C95x3Wb/qOwgQuBNGl qOZ6tQnxnxkYCGWADp9pGOIiaQhIW3ROSV1CGGDHz1KeDaoW+iZcFSE35tteMNt4fn31 XoBWfQTPxRlyhdKf94yVVgIsl9dPwPqxsEqN4U9bJb7LofDjW0gRC3ejwaFqGiPBMLB8 +EgBmRu1PHVrGueuSj9/bf+M4LU3T6Cc9Mt+Dp2tycvZX5BK/DGdsXKowICNYnE9HaMI QjCnif+G1qmLSOJ3bTZr3bwUpOcUv6cvknhsNKsU6BKEBXn1qbARXlVEC5NGPaMNSJ2N T9Ow== MIME-Version: 1.0 X-Received: by 10.107.16.32 with SMTP id y32mr35832313ioi.53.1432042413568; Tue, 19 May 2015 06:33:33 -0700 (PDT) In-Reply-To: References: Date: Tue, 19 May 2015 23:33:33 +1000 Subject: Re: fork/exec & close file descriptors From: Chris Angelico Cc: Python 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432042416 news.xs4all.nl 2894 [2001:888:2000:d::a6]:58050 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90850 On Tue, May 19, 2015 at 10:59 PM, Skip Montanaro wrote: > Due to presumed bugs in an underlying library over which I have no control, > I'm considering a restart in the wee hours of the morning. The basic > fork/exec dance is not a problem, but how do I discover all the open file > descriptors in the new child process to make sure they get closed? Do I > simply start at fd 3 and call os.close() on everything up to some largish fd > number? Some of these file descriptors will have been opened by stuff well > below the Python level, so I don't know them a priori. What Python version are you targeting? Are you aware of PEP 446? https://www.python.org/dev/peps/pep-0446/ tl;dr: As of Python 3.4, the default is to close file descriptors on exec automatically - and atomically, where possible. I'm not sure if there's a 2.7 backport available, though even if there is, you'll need to manually set your files non-inheritable; AIUI the default in 2.7 can't be changed for backward compatibility reasons (the change in 3.4 *will* break code that was relying on automatic inheritability of FDs - they now have to be explicitly tagged). ChrisA