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.96) (envelope-from ) id 1wB5y0-000jLT-1C for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 07:07:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wB5xy-00A2zp-1X for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 07:07:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wB5xy-00A2zg-09 for pgsql-hackers@lists.postgresql.org; Fri, 10 Apr 2026 07:07:47 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wB5xw-00000000I4c-2AUh for pgsql-hackers@postgresql.org; Fri, 10 Apr 2026 07:07:46 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2b2503753efso14739445ad.0 for ; Fri, 10 Apr 2026 00:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775804864; x=1776409664; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=e/Iw0yQbEVgl6c6bstimtazWyAVisBRDUfqlcocsr1U=; b=R7JtTguoCzAzkOU+m+Y1ebxjS+Z1JJ13iBhLZW0DR4eiru5U7Ixy+6kpkcEgNqYmTs Xj0b8ghhKeqDV4ne2P5f1cte9owwyJv8uvmE9nl0S4/uJOrVJyLaizUnAgaR0SDX6avy 05s461g8MVVedsFhERmMNr2IulUxnoqPUYG83fUFwJPSWAdAg1OFGqDqeUd6C7tQhBku DyccfX28O/j9xgsz07uUCQwMhxwMqRJgDdWZaIuGCLGeYQgE4lCRdsYPGjT4ELyUqsEn x7rGwOA7geUL+Cq5rlf/e4CAYOdoweGFT7NstzheZPWqdB5VlLCdjMyiM9Xf9933BnEt wXwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775804864; x=1776409664; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=e/Iw0yQbEVgl6c6bstimtazWyAVisBRDUfqlcocsr1U=; b=ewt7PJ3MuR7bKqlBjJ/3KtCnIT5/PMqP6OtM+SVmvRAMuPc/DTeAiG93x8S/72kNmy yqHwfUnP9a4o3+X8NHKDi7oMUUP2g5kzwggBzirajDGHDWV5Vgnft/ewApOzhvM95bXn eLrNCH5QAoRL1PSmcf+fygaEmrdrXfZaEV75rs70fqX5kUu0g2k9qro0SLPamRAEt8LF lPhm3h3i0AJNdtp2Edm2dPkgo8BC0Mdoe4GSsKrcuH1rrfAY1T19Am8SaJTlwmPphJZ/ w+LgHswZ2QSGghBsJCJFWDqbtmrVYVs7cMCZKAFH1Ee7Z2Cm4DoaEG+YaATfF63pNGOW 5Syg== X-Forwarded-Encrypted: i=1; AJvYcCUD+/UQikWdfQsGAjHAN6zWQyazSoqbz0s12oXx2i6W2wqb3NnP95Ldukg/b38+gqO4lBxoevzVJSphIJmB@postgresql.org X-Gm-Message-State: AOJu0YyoaFEFfmHkIaO4eMhAMUyVVUfWKbh1jiNiB+EItb25Ia+KpEnc QalyFTiUED/sw8Agl1Gmx7bIed6SD9Is5IYKS6STtYwEcCzKLUA544Jn X-Gm-Gg: AeBDieszbs62zis3+MknXes29KUzAHD/YwOCUgC3g8JNsEUynTHZ2MSZMF/L+W4hwN8 ccn46dfGxfjcHfebFnWoC1esXyHtfHLrms0upEY4ldsiHIFBdwH7atc1pVFE7ZvnuSjpdo+T9xR Esf0osi2xqbThAIINJdnEkkBkZj9W9H3fmbV7LYffBr/t61afgciwLq5b7dxhyQCWfiExwl2U5O bMvcu9Vdp6WnZBHos/djVSSgVpIOy7wpnOAp2NIiPf0pF78rF1wvQFG29PZuy2oWBXckCLJ44k5 ESC1jBoJ9F4OXJwCfnr8V4F6WtcyEpPpXmxw28CLYVeZ6Yxq9rmKA7/IFI6PJHi1T1AXAH8Hnaj 9kPUmwvGibVRtExRxnvTqg+Ac8nrxE2Bjr08GWdQnqoMNvl92QFdqgYW0BamEgcPNGv9tEZZx+5 ZZAELY9TUga93DAL9/y2sdQd7k3X02FfYtifWu0JjT8g== X-Received: by 2002:a17:902:8211:b0:2b2:d126:4e77 with SMTP id d9443c01a7336-2b2d5975bdfmr16202635ad.11.1775804863602; Fri, 10 Apr 2026 00:07:43 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2d4f25df6sm18338305ad.56.2026.04.10.00.07.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Apr 2026 00:07:42 -0700 (PDT) From: Chao Li Message-Id: <0D17D4E1-919F-4412-8EFE-BEB80211321D@gmail.com> Content-Type: multipart/mixed; boundary="Apple-Mail=_9A1B61B4-570E-4FD1-9D61-AEAD603494F9" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: Use proc_exit() in WalRcvWaitForStartPosition Date: Fri, 10 Apr 2026 15:07:03 +0800 In-Reply-To: Cc: Xuneng Zhou , Andreas Karlsson , PostgreSQL-development To: Fujii Masao References: <74381238-4E8A-4621-B794-57025DCCE0BA@gmail.com> <6a42c40e-eb81-4212-9bca-8c0eb02d47d1@proxel.se> X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_9A1B61B4-570E-4FD1-9D61-AEAD603494F9 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Apr 10, 2026, at 14:16, Fujii Masao wrote: >=20 > On Thu, Apr 9, 2026 at 10:09=E2=80=AFAM Xuneng Zhou = wrote: >>=20 >> On Thu, Apr 9, 2026 at 5:00=E2=80=AFAM Andreas Karlsson = wrote: >>>=20 >>> On 4/8/26 11:08 AM, Chao Li wrote: >>>> While working on another patch, I happened to notice that = WalRcvWaitForStartPosition() calls raw exit(1). I think this should use = proc_exit(1) instead, so that the normal cleanup machinery is not = bypassed. >>>>=20 >>>> This tiny patch just replaces exit(1) with proc_exit(1) in = WalRcvWaitForStartPosition(). >>>=20 >>> This looks likely to be correct since when we exit in = WalReceiverMain() >>> (on WALRCV_STOPPING and WALRCV_STOPPED) we call proc_exit(1). I feel = we >>> should exit the same way in WalRcvWaitForStartPosition() as we do in >>> WalReceiverMain() and if not I would like a comment explaining why = those >>> two cases are different. >>=20 >> +1 >=20 > +1 >=20 >=20 >> WalRcvWaitForStartPosition, WALRCV_STOPPING before entering wait loop >> uses proc_exit(0) for WALRCV_STOPPING, while this path should = probably >> use proc_exit(0) as well (not proc_exit(1)), since the stop was a >> requested shutdown, not an error. Using exit code 1 for a clean >> stop-on-request seems inconsistent. >=20 > The requested shutdown is handled in ShutdownWalRcv(), which sets the = state to > WALRCV_STOPPING and sends SIGTERM to the walreceiver. >=20 > Although this might be considered a normal shutdown (suggesting exit = code 0), > when the walreceiver receives SIGTERM it exits via ereport(FATAL), = resulting > in exit code 1. In contrast, if it exits early in = WalRcvWaitForStartPosition() > due to the WALRCV_STOPPING state, it uses exit code 0, as you noted. = So > there seems to be some inconsistency in exit codes. >=20 > That said, the exit code (0 vs 1) does not affect behavior, since > the postmaster treats both as non-crash exits. >=20 > For consistency, I would prefer using exit code 1 in proc_exit() in > WalRcvWaitForStartPosition(), to match the ereport(FATAL) path. But = I'm fine > with other approaches as well. >=20 > Also, the comment at the top of walreceiver.c may need updating: >=20 > * Normal termination is by SIGTERM, which instructs the walreceiver to > * exit(0). Emergency termination is by SIGQUIT; like any postmaster = child > * process, the walreceiver will simply abort and exit on SIGQUIT. A = close > * of the connection and a FATAL error are treated not as a crash but = as > * normal operation. >=20 > Regards, >=20 > --=20 > Fujii Masao PFA v2 - updated header comment of walreceive.c. I tried to avoid = mentioning the exact exit value in the comment, so I just changed = =E2=80=9Cexit(0)=E2=80=9D to =E2=80=9Cterminate=E2=80=9D. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_9A1B61B4-570E-4FD1-9D61-AEAD603494F9 Content-Disposition: attachment; filename=v2-0001-Use-proc_exit-in-WalRcvWaitForStartPosition.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v2-0001-Use-proc_exit-in-WalRcvWaitForStartPosition.patch" Content-Transfer-Encoding: quoted-printable =46rom=20e1070dbd77f251774fcddee163f16fbd89b70be5=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Wed,=208=20Apr=202026=2017:02:28=20+0800=0ASubject:=20[PATCH=20= v2]=20Use=20proc_exit()=20in=20WalRcvWaitForStartPosition=0A=0AAuthor:=20= Chao=20Li=20=0AReviewed-by:=20Fujii=20Masao=20= =0AReviewed-by:=20Andreas=20Karlsson=20= =0AReviewed-by:=20Xuneng=20Zhou=20= =0ADiscussion:=20= https://postgr.es/m/74381238-4E8A-4621-B794-57025DCCE0BA@gmail.com=0A---=0A= =20src/backend/replication/walreceiver.c=20|=204=20++--=0A=201=20file=20= changed,=202=20insertions(+),=202=20deletions(-)=0A=0Adiff=20--git=20= a/src/backend/replication/walreceiver.c=20= b/src/backend/replication/walreceiver.c=0Aindex=20= 09fde92bfd7..0941c9c4674=20100644=0A---=20= a/src/backend/replication/walreceiver.c=0A+++=20= b/src/backend/replication/walreceiver.c=0A@@=20-30,7=20+30,7=20@@=0A=20=20= *=20a=20new=20one.=0A=20=20*=0A=20=20*=20Normal=20termination=20is=20by=20= SIGTERM,=20which=20instructs=20the=20walreceiver=20to=0A-=20*=20exit(0).=20= Emergency=20termination=20is=20by=20SIGQUIT;=20like=20any=20postmaster=20= child=0A+=20*=20terminate.=20Emergency=20termination=20is=20by=20= SIGQUIT;=20like=20any=20postmaster=20child=0A=20=20*=20process,=20the=20= walreceiver=20will=20simply=20abort=20and=20exit=20on=20SIGQUIT.=20A=20= close=0A=20=20*=20of=20the=20connection=20and=20a=20FATAL=20error=20are=20= treated=20not=20as=20a=20crash=20but=20as=0A=20=20*=20normal=20= operation.=0A@@=20-710,7=20+710,7=20@@=20= WalRcvWaitForStartPosition(XLogRecPtr=20*startpoint,=20TimeLineID=20= *startpointTLI)=0A=20=09=09=09=20*=20to=20die,=20but=20might=20as=20well=20= check=20it=20here=20too.=0A=20=09=09=09=20*/=0A=20=09=09=09= SpinLockRelease(&walrcv->mutex);=0A-=09=09=09exit(1);=0A+=09=09=09= proc_exit(1);=0A=20=09=09}=0A=20=09=09SpinLockRelease(&walrcv->mutex);=0A= =20=0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_9A1B61B4-570E-4FD1-9D61-AEAD603494F9--