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 1mtcpf-00059u-6L for pgsql-general@arkaria.postgresql.org; Sat, 04 Dec 2021 21:44:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mtcp6-00006B-TR for pgsql-general@arkaria.postgresql.org; Sat, 04 Dec 2021 21:44:00 +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 1mtcp6-000061-CU for pgsql-general@lists.postgresql.org; Sat, 04 Dec 2021 21:44:00 +0000 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mtcoy-0005mY-NV for pgsql-general@lists.postgresql.org; Sat, 04 Dec 2021 21:43:58 +0000 Received: by mail-wr1-x42c.google.com with SMTP id a18so13670284wrn.6 for ; Sat, 04 Dec 2021 13:43:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=JEyIQO9a3L43WrafSUUuUkekjamdt6QAc6qnK+LrhX4=; b=v6Kmr/twNLe0/tduWV48mEvhrBPXi5EZQjjuwvxRCJ+py5B7UFVaAK18sy0pgzf3fR acphEKYCk6mOGvq8E+NZzNzlJ6OYenpoxYlQotlxf9cadrnrbmgVyf0kNtZ8BlCrXH5Z hrkhChqolD1LUsacc2ssXiq2w2KK93xkTylygFzyfyGmzD9IcX+ENmiAhgFvUp9DGkri 9J3v3Du8VRdMQU79QFN1+JOn1yNUioxxW+G7ODS+77eJr9y12r7E8tOnkCHpkHgX8cCR 4dEa+g4x+hl9xNCseWSNFiaM2Q9f6lMA/Rd8ezFQ4cWspdZpWxk35f3Xnr4MDn4ewlO1 Uydw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=JEyIQO9a3L43WrafSUUuUkekjamdt6QAc6qnK+LrhX4=; b=bmpWHbISu8VS7s+V5nW/yAjDmlfYjwsVZDD6YohzgEAdYEVwCihwNbFR0xr5b7Gtgw a/50lPiTRXAvPKSeKoshNG9yIJH+KHy0veUNAc2BE6Pa0xwKUh5BtznHeOX6Dtd9PyRj 8ZYKrSrh9++fHdNtkTH6110OKwcrl4MvSJuJBmUSU2fFrZn7okh+EvXw56mInPc9IdyI lOvpvEFbYxB3lTP0kQcaB7WbPnrKHw3ZVp8R2FyIZN0BqzuW4HjeUVmdHbYa/Zj6Vlxl QBkIdJifsx2zYM9PJ84cvtkrcvCFy63+nMVdvi3AOXpXN9sgKyJF3eSaUSLZemWU/Jrq CNtQ== X-Gm-Message-State: AOAM533d6j3v6V8K2MNHus1DdQwe0K9vy55DYUoUPwjr5EC5UI0o10Cy oywCjktTXMsdlPUDb3fWSXEIhw== X-Google-Smtp-Source: ABdhPJxzUDFLPJ6bY2zMju1U8fdNoDZtCa5BV4BRQ5SMYDRX0Voe5q6fOQ11mK/GeJKkE1b3rW0zuQ== X-Received: by 2002:a05:6000:18a3:: with SMTP id b3mr31730278wri.343.1638654230353; Sat, 04 Dec 2021 13:43:50 -0800 (PST) Received: from localhost.localdomain (62-46-249-46.adsl.highway.telekom.at. [62.46.249.46]) by smtp.gmail.com with ESMTPSA id 10sm8395328wrb.75.2021.12.04.13.43.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Dec 2021 13:43:49 -0800 (PST) Message-ID: <36a1a59fcf09deec95b3abdec979d4d6b64a1a4a.camel@cybertec.at> Subject: Re: libpq: Which functions may hang due to network issues? From: Laurenz Albe To: Daniel Frey , Tom Lane Cc: pgsql-general Date: Sat, 04 Dec 2021 22:43:48 +0100 In-Reply-To: References: <2B29B70E-2A78-49D3-9861-5DD289DC30DD@gmx.de> <0F84CF07-A88E-41A3-B41D-984EA6385AA2@gmx.de> <2311007.1638551661@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-2.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2021-12-03 at 21:33 +0100, Daniel Frey wrote: > But the real issue, at least for me, is PQfinish(). Considering that my application is not > allowed to hang (or crash, leak, ...), what should I do in case of a timeout? I am tempted to say that you shouldn't use TCP with the requirement that it should not hang. > I have existing > connections and at some point the network connections stop working (e.g. due to a firewall > issue/reboot), etc. If I don't want a resource leak, I *must* call PQfinish(), correct? > But I have no idea whether it might hang. If you don't want to guarantee that PQfinish() > will not hang, then please advise how to use libpq properly in this situation. If there > some asynchronous version of PQfinish()? Or should I handle hanging connections differently? You could start a separate process that has your PostgreSQL connection and kill it if it times out. But then you'd have a similar problem communicating with that process. A normal thing to do when your database call times out or misbehaves in other ways is to give up, report an error and die (after some retries perhaps). Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com