Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pJGXx-00030r-F3 for pgsql-hackers@arkaria.postgresql.org; Sat, 21 Jan 2023 16:16:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pJGXw-00046v-Ao for pgsql-hackers@arkaria.postgresql.org; Sat, 21 Jan 2023 16:16:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pJGXw-00046d-1x for pgsql-hackers@lists.postgresql.org; Sat, 21 Jan 2023 16:16:48 +0000 Received: from mail-oa1-x2a.google.com ([2001:4860:4864:20::2a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pJGXt-0004dP-UU for pgsql-hackers@postgresql.org; Sat, 21 Jan 2023 16:16:46 +0000 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-1433ef3b61fso9586239fac.10 for ; Sat, 21 Jan 2023 08:16:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=3DCcRqLiy+kG8mdubN7zA4qzeFisgrYL2i+qL98bX7U=; b=GY7z3x0ndhrr5+Muz1yfnhZ3opI+/yAknibszDMhnlP8vUog3wH7U8Z0ED0a2pO1LH vhQhrY8hkIrQoCmhbEls9kFyb0H7sCYlURJHJ+ZWSYLbowcYTeYy/5OQW1fPuLdJq0PK rGVaaMyXR8Vv8JOTOeMGA2XEd/1AwOkh5dJMk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3DCcRqLiy+kG8mdubN7zA4qzeFisgrYL2i+qL98bX7U=; b=3CjpCaPdqMeuvntVh5TRhmlkeOe50gHbyUsWE6LP3svbkTWfCE8DPoDzI43Xp45fVP eDe0SI4bJOIx9VNYcTMIyQmtodw6RBIGexPvihQ9nWOfMANulrdMJAY47WbpY3RkiITw ahXcsE+P9DyqoqgUEAN6Wb6Sdh21J3ZEhZq/FcFcBgbBcuznsqBDLLOHl24bWC+lyZTr kxdfND7fEBza5jdIhTIjan6lmulL+QR+L2uVvruES46yDFI2DLiq05V/mE8sfkvfGkrZ r9gJRhtJLugLSVBcC/5t4HO1gsp3t6giO8WKSphdp68oe7sfx0iOtbt7o8aDI3iV9hW3 UURw== X-Gm-Message-State: AFqh2krPnnWiEMsU9uCbEGRvHmwaY6Iorduf/NkJ8+hT93xIVeVEPKPw GADzNpI5ERnmSy+8SwtF70EuHA== X-Google-Smtp-Source: AMrXdXuvuuOuzFJVshhMCBMjmA1rD3mK0WK9p5ZF4X+wkwWc0juiRNhKr96U2Thb+TpdAhJR+kbu9Q== X-Received: by 2002:a05:6870:c1cb:b0:15f:13ea:3ba9 with SMTP id i11-20020a056870c1cb00b0015f13ea3ba9mr12248153oad.3.1674317805258; Sat, 21 Jan 2023 08:16:45 -0800 (PST) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id r10-20020a05687032ca00b0014ff15936casm23357627oac.40.2023.01.21.08.16.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 Jan 2023 08:16:44 -0800 (PST) Date: Sat, 21 Jan 2023 08:16:42 -0800 From: Noah Misch To: Andres Freund Cc: pgsql-hackers@postgresql.org Subject: Re: libpqrcv_connect() leaks PGconn Message-ID: <20230121161642.GD1356297@rfd.leadboat.com> References: <20230121011237.q52apbvlarfv6jm6@awork3.anarazel.de> <20230121025037.mildbmgylv4ybp4a@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230121025037.mildbmgylv4ybp4a@awork3.anarazel.de> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jan 20, 2023 at 06:50:37PM -0800, Andres Freund wrote: > On 2023-01-20 17:12:37 -0800, Andres Freund wrote: > > We have code like this in libpqrcv_connect(): > > > > conn = palloc0(sizeof(WalReceiverConn)); > > conn->streamConn = PQconnectStartParams(keys, vals, > > /* expand_dbname = */ true); > > if (PQstatus(conn->streamConn) == CONNECTION_BAD) > > { > > *err = pchomp(PQerrorMessage(conn->streamConn)); > > return NULL; > > } > > > > [try to establish connection] > > > > if (PQstatus(conn->streamConn) != CONNECTION_OK) > > { > > *err = pchomp(PQerrorMessage(conn->streamConn)); > > return NULL; > > } > > > > > > Am I missing something, or are we leaking the libpq connection in case of > > errors? > > > > It doesn't matter really for walreceiver, since it will exit anyway, but we > > also use libpqwalreceiver for logical replication, where it might? > > > > > > Seems pretty clear that we should do a PQfinish() before returning NULL? I > > lean towards thinking that this isn't worth backpatching given the current > > uses of libpq, but I could easily be convinced otherwise. > > > > It's bit worse than I earlier thought: We use walrv_connect() during CREATE > SUBSCRIPTION. One can easily exhaust file descriptors right now. So I think > we need to fix this. > > I also noticed the following in libpqrcv_connect, added in 11da97024abb: > The attached patch fixes both issues. Looks good. I'm not worried about a superuser hosing their own session via CREATE SUBSCRIPTION failures in a loop. At the same time, this fix is plenty safe to back-patch. > I seems we don't have any tests for creating a subscription that fails during > connection establishment? That doesn't seem optimal - I guess there may have > been concern around portability of the error messages? Perhaps. We have various (non-subscription) tests using "\set VERBOSITY sqlstate" for that problem. If even the sqlstate varies, a DO block is the next level of error swallowing. > I think we can control > for that in a tap test, by failing to connect due to a non-existant database, > then the error is under our control. Whereas e.g. an invalid hostname would > contain an error from gai_strerror(). That sounds fine.