From owner-freebsd-hackers@freebsd.org Mon Jan 8 16:13:24 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 162EFE74F25 for ; Mon, 8 Jan 2018 16:13:24 +0000 (UTC) (envelope-from eric@vangyzen.net) Received: from smtp.vangyzen.net (hotblack.vangyzen.net [199.48.133.146]) by mx1.freebsd.org (Postfix) with ESMTP id F089282BCA; Mon, 8 Jan 2018 16:13:23 +0000 (UTC) (envelope-from eric@vangyzen.net) Received: from sweettea.beer.town (unknown [76.164.8.130]) by smtp.vangyzen.net (Postfix) with ESMTPSA id 940355646F; Mon, 8 Jan 2018 10:13:16 -0600 (CST) Subject: Re: Is it considered to be ok to not check the return code of close(2) in base? To: Eugene Grosbein , Brooks Davis , Alan Somers Cc: Yuri , Ian Lepore , Freebsd hackers list References: <24acbd94-c52f-e71a-8a96-d608a10963c6@rawbw.com> <1514572041.12000.7.camel@freebsd.org> <20180105221330.GD95035@spindle.one-eyed-alien.net> <5A4FF989.1040709@grosbein.net> <7b977409-96ee-5acb-60d0-3b0e124495f0@vangyzen.net> <5A5002D9.9080205@grosbein.net> From: Eric van Gyzen Message-ID: Date: Mon, 8 Jan 2018 10:13:15 -0600 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <5A5002D9.9080205@grosbein.net> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Jan 2018 16:13:24 -0000 On 01/05/2018 16:57, Eugene Grosbein wrote: > 06.01.2018 5:26, Eric van Gyzen wrote: > >>>>> I would argue the opposite. There are very few reasons why close(s) would >>>>> ever fail, and the most likely is EBADF. EBADF indicates a programming >>>>> bug, like a double close or use of an uninitialized variable. Those could >>>>> easily turn into worse bugs in the future. So I think the best course of >>>>> action is to check the return code, assert() on EBADF, and ignore, or >>>>> possibly log, other errors. >>>> >>>> For this specific case, I think there would be value in an option to >>>> have the kernel kill any process that calls close(fd) where fd != -1 >>>> where EBADF would be returned. >>> >>> A medicine should not be worse worse than the disease, imho. >> >> In a multi-threaded application, a double-close can close completely >> unrelated file descriptors, which can be a nightmare to diagnose. In >> that case, death by signal is far better than the disease. > > A kernel cannot know if close() was called for "unrealated" but correct file descriptor. Right, which is the reason such bugs are hard to diagnose. Optionally killing the process on close->EBADF would help find buggy code when another thread did NOT re-open the file descriptor between the two close calls. Eric