Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vH1BZ-00B6iW-PR for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Nov 2025 14:42:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vH1BY-008RPW-FN for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Nov 2025 14:41:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vH1BY-008RPN-5C for pgsql-hackers@lists.postgresql.org; Thu, 06 Nov 2025 14:41:59 +0000 Received: from mail-yx1-xb12c.google.com ([2607:f8b0:4864:20::b12c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vH1BU-006K30-2m for pgsql-hackers@postgresql.org; Thu, 06 Nov 2025 14:41:58 +0000 Received: by mail-yx1-xb12c.google.com with SMTP id 956f58d0204a3-63f94733d6cso890487d50.3 for ; Thu, 06 Nov 2025 06:41:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762440115; x=1763044915; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=nriKz2SYEjFt5fMe6qLfzVZg/ihHTZvxN/uz0zJdTxo=; b=Z5mDGZLm/XXc06Am1GRYv8O6OgfKF90K5W2IOY0egVYJPytutaCxqKkN3Uzg3VAOfI mfveorXIzL0vCC8heEhN3mi0igP9Jci1w2ITdCDve/cZlPB2WErwdl/aafCJE8Fjkg8o gTKj2X9O6aX5ySxCYQ4WNqXxLGVdXwSJcdRmoc31RruDRj8ulX6CF9+Gb2l6eWRrl1D/ HemrRqmd/y9LQavV6nhBY0WtXDCj412jyCYP+DASSOLai0e8u+yY+4t+Sst6R17VeFXm wrnINyM44Z+Kcouttht7+fG+D0MpuTVW6xUJUjFyRM1898huZxGOBJHNu7dcAGwtv3w8 jOsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762440115; x=1763044915; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nriKz2SYEjFt5fMe6qLfzVZg/ihHTZvxN/uz0zJdTxo=; b=m8pk//GQ+9a1+Il2tye4SxgfyemtZe8cEm/FJ/zdZeTuT3uRBV2O0bDhSwYCEgpAk2 kRI9yjwJPQQfxdV2z8o8AzSJ5OLudEyfI58Tbmvb1//9dZQSlgviNG+50KBXgQkNbMhA w1GSgEqj/f/n03EUwSpztwkKSjEgJeNeePrp9+xoJ/Gx12GmPRJBfpqJkej1vSk4qCEA zOzApMcZF6EIlX2dA0hEsQ974IMZDgYA7t9fjgU+PoBsFGjb7Z5PUIWNdaP5U/F2QAlZ kbFpNi4dp5Q4Nu7AdPxLYfRyUWjDgUJt6lKwJNQ5TNuT8sXdmFWTsqAT4oDHnsv0r3Tl Cy1Q== X-Gm-Message-State: AOJu0YygnbSfU+MUBGd0mMTjcfosuP6jsGL5wcvZ1QES3FIQ+0apmsky 8WqyJ8CTaOPtR5NKxdMaFVirTYiK01c1eMpJFoPKh7c5FcUOfUJoX8YHILR09RyW9cjh2Q== X-Gm-Gg: ASbGncvYKmWqJ9kirgiYgxCWxDZ9+Modnjw93hMhHuHVIptbgb2bCNzhJa0d7ly8ntI 8Mxp57fSng6pZ+E507rrIsvEvu2uwEB5DDMxKAAsO2YFlVsVTjeMZeKoMH4VIkQPbMbHD7yKRMj MW3ECbePZpvX4NxtS3AJq/1xCwuBp4NFUZz6YYB6pKzSSPIZe9PimQNymQ370QYxwhRY+xj2J2s TS/sd7o37oIyFtoeHcgQfm2EtVKCAeem0pRFpcDcHHgd6kK2bL+WHhsi6Nbm4ij6gMe2v00iip5 +ELP/G9JCpCjMQ405Nndl2akyKu672iVMcwSxDVwlfHN3brUOGa1Rjt3wkAyLgDjMTuN+BIwfjB 0HVxsN/RTeI3lmkiXkKEmnhjBawXrqDkZCpEkepMY5i6nv3IoJrC2aaUfNmF2sqYNIfGCX5ulpc NdClynC779pRLk2DFl9EBc8Z5/eu3ChRuRaurRHolBwWly8T4BYlJUa7vynNGy6HkF X-Google-Smtp-Source: AGHT+IFiDegU+d2nyOyOeE1KXJthZ80T00k4b//iuotKMDg6rMX2zUaPgdgNcTtr37Aqx2z081b4XQ== X-Received: by 2002:a05:690c:7005:b0:786:9774:a3b8 with SMTP id 00721157ae682-786a41bf159mr113269027b3.35.1762440115114; Thu, 06 Nov 2025 06:41:55 -0800 (PST) Received: from ?IPV6:2600:1700:8952:80:d4ac:40e9:5ae8:a25b? ([2600:1700:8952:80:d4ac:40e9:5ae8:a25b]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-640b5d93948sm817719d50.19.2025.11.06.06.41.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 06 Nov 2025 06:41:54 -0800 (PST) Message-ID: <7f6779d5-7a0e-44c5-a85f-b4ef265db766@gmail.com> Date: Thu, 6 Nov 2025 08:42:14 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] O_CLOEXEC not honored on Windows - handle inheritance chain To: Thomas Munro Cc: pgsql-hackers References: Content-Language: en-US From: Bryan Green In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/6/2025 7:43 AM, Thomas Munro wrote: > On Sat, Nov 1, 2025 at 6:16 AM Bryan Green wrote: >> Hello, > > Catching up with all your emails, and I must say it's great to see > some solid investigation of PostgreSQL-on-Windows problems. There are > ... more. > >> Commit 1da569ca1f (March 2023) added O_CLOEXEC to many call sites >> throughout the backend with a comment saying "Our open() replacement >> does not create inheritable handles, so it is safe to ignore >> O_CLOEXEC." But that doesn't appear to match what the code actually >> does. I'm wondering if I've misunderstood something about how handle >> inheritance works on Windows, or if the comment was based on a >> misunderstanding of the code path. > > Yeah, it looks like I was just wrong. Oops. Your analysis looks good to me. > >> The fix would be straightforward if this is indeed wrong. Define >> O_CLOEXEC to a non-zero value like 0x80000 (in the private use range >> for open() flags), and then honor it in pgwin32_open() by setting >> sa.bInheritHandle based on whether the flag is present: >> >> sa.bInheritHandle = (fileFlags & O_CLOEXEC) ? FALSE : TRUE; > > Looking at fcntl.h, that's the next free bit, but also the one they'll > presumably define next (I guess "private use range" is just a turn of > phrase and not a real thing?), so why not use the highest free bit > after O_DIRECT? We have three fake open flags, one of which > cybersquats a real flag from fcntl.h, ironically the one that actually > means O_CLOEXEC. We can't change existing values in released > branches, so that'd give: > > #define O_DIRECT 0x80000000 > #define O_CLOEXEC 0x04000000 > #define O_DSYNC _O_NO_INHERIT > > Perhaps in master we could rearrange them: > > #define O_DIRECT 0x80000000 > #define O_DSYNC 0x04000000 > #define O_CLOEXEC _O_NO_INHERIT > >> So my questions are: Am I correct that both conditions for handle >> inheritance are met, meaning handles really are being inherited by >> archive_command children? Is there something in Windows that prevents >> inheritance that I don't know about? If this is a real bug, would it >> make sense to backpatch to v16 where O_CLOEXEC was added? I'm happy to >> provide my test code or do additional testing if that would help. > > Yeah, seems like it, and like we should back-patch this. I vote for > doing that after the upcoming minor releases. Holding files open on > Windows unintentionally is worse on Windows than on Unix (preventing > directories from being unlinked etc). Of course we've done that for > decades so I doubt it's really a big deal, but we should clean up this > mistake. Thanks for reviewing this and confirming the analysis. Good to know I wasn't missing something about Windows handle inheritance. Your point about the bit value makes sense - using 0x04000000 (highest free bit after O_DIRECT) is definitely safer than 0x80000 which could collide with future fcntl.h additions. I also appreciate the irony you pointed out - we're currently using _O_NO_INHERIT (which literally prevents handle inheritance on Windows) for O_DSYNC instead of O_CLOEXEC. The rearrangement in master to use _O_NO_INHERIT for what it actually means semantically makes a lot of sense. So the plan would be: Backport branches (v16+): #define O_DIRECT 0x80000000 #define O_CLOEXEC 0x04000000 #define O_DSYNC _O_NO_INHERIT Master: #define O_DIRECT 0x80000000 #define O_DSYNC 0x04000000 #define O_CLOEXEC _O_NO_INHERIT And then in pgwin32_open(): sa.bInheritHandle = (fileFlags & O_CLOEXEC) ? FALSE : TRUE; I will prepare a new version of the patch that implements the suggested change for master. -- Bryan Green EDB: https://www.enterprisedb.com