From nobody Fri Jun 23 20:02:59 2023 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Qnp7w2YD1z4gcsK for ; Fri, 23 Jun 2023 20:03:08 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4Qnp7v5yC3z3NMJ; Fri, 23 Jun 2023 20:03:07 +0000 (UTC) (envelope-from kostikbel@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.17.1/8.17.1) with ESMTPS id 35NK2xFC011037 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Fri, 23 Jun 2023 23:03:02 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 35NK2xFC011037 Received: (from kostik@localhost) by tom.home (8.17.1/8.17.1/Submit) id 35NK2xHa011036; Fri, 23 Jun 2023 23:02:59 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Fri, 23 Jun 2023 23:02:59 +0300 From: Konstantin Belousov To: Alan Somers Cc: FreeBSD Hackers Subject: Re: Should close() release locks atomically? Message-ID: References: List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=4.0.0 X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-14) on tom.home X-Rspamd-Queue-Id: 4Qnp7v5yC3z3NMJ X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N On Fri, Jun 23, 2023 at 12:00:36PM -0700, Alan Somers wrote: > The close() syscall automatically releases locks. Should it do so > atomically or is a delay permitted? I can't find anything in our man > pages or the open group specification that says. > > The distinction matters when using O_NONBLOCK. For example: > > fd = open(..., O_DIRECT | O_EXLOCK | O_NONBLOCK); //succeeds > // do some I/O > close(fd); > fd = open(..., O_DIRECT | O_EXLOCK | O_NONBLOCK); //fails with EAGAIN! > > I see this error frequently on a heavily loaded system. It isn't a > typical thread race though; ktrace shows that only one thread tries to > open the file in question. From the ktrace, I can see that the final > open() comes immediately after the close(), with no intervening > syscalls from that thread. It seems that close() doesn't release the > lock right away. I wouldn't notice if I weren't using O_NONBLOCK. > > Should this be considered a bug? If so I could try to come up with a > minimal test case. But it's somewhat academic, since I plan to > refactor the code in a way that will eliminate the duplicate open(). What type of the object is behind fd? O_NONBLOCK affects open itself. We release flock after object close method, but before close(2) returns.