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 1nMjlL-0007jB-OF for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Feb 2022 05:00:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nMjlK-0005LH-LR for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Feb 2022 05:00:26 +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 1nMjlK-0005L0-5U for pgsql-bugs@lists.postgresql.org; Wed, 23 Feb 2022 05:00:26 +0000 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nMjlD-0005QE-73 for pgsql-bugs@lists.postgresql.org; Wed, 23 Feb 2022 05:00:25 +0000 Received: by mail-oi1-x232.google.com with SMTP id a6so17001852oid.9 for ; Tue, 22 Feb 2022 21:00:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=tk+qBgdpMnekmnlo27pOFg4TGV6F9UQldiSHKnz6SfQ=; b=MInfRNFRywniFLTyRkpPKdgQxPF0id4XHqolGX7AeGl+s7xdHn/0gFn5u/fF4lJaUw TuiR64utwxzj2eVPlAm+2g8CkIpmBFFc/mDS0YNKFu0pj9o3W5/n6OaDvn7RQ8Ypjsjw +DXPtjsy2JW0lsBiG6UComWtsemT6NtVRNEZU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=tk+qBgdpMnekmnlo27pOFg4TGV6F9UQldiSHKnz6SfQ=; b=2dCa+FkXws8K4gEFqsrAWs0/xM7tsDBvL2qW8/dAYEX6A/53/84mBi1RqhVpKwyDuZ p82IOi4uDHiXdn5MsNbPYIb/6E+3uE8JXMegXujaqhpLZ/DEf64m7vO08/dC/MgWR5mz zakCmMFC26Cw7YZar6H3qLc/aZSEqMKkKwQDQSrtpMrcvn3sYhX2Uqb6pQLj4qCTNpLs o6yog4bHgSCMRVgi/8yjqkl45HcDZugti/f9mS2PJsoxgTZj5q495Kid0NBlXxhGoG4u Fa42CFqrV07PDzPrbgR/pFaPZXQ+J5MlIG4CvIqYsybNeAmSz4nsXguBe0he2AV+QmRn mf+Q== X-Gm-Message-State: AOAM533jThcox7eKn0Ytsz3/GsPaq3wdYhE0EZvhUnqADYF0Cx5PYfJB ffPrRYIk91QLbbSEMPNXk+nydQ== X-Google-Smtp-Source: ABdhPJyjbtRmssJc7S1VHg1QB9i21RJG/uHDbsqsTT4wPEWzDCboUjGfjQvouHmtQWnwTBh/sEIfJg== X-Received: by 2002:a05:6808:179b:b0:2d4:6241:4b61 with SMTP id bg27-20020a056808179b00b002d462414b61mr3808861oib.220.1645592418280; Tue, 22 Feb 2022 21:00:18 -0800 (PST) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id a18sm7297907oos.4.2022.02.22.21.00.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Feb 2022 21:00:17 -0800 (PST) Date: Tue, 22 Feb 2022 21:00:15 -0800 From: Noah Misch To: "egashira.yusuke@fujitsu.com" Cc: "'pgsql-bugs@lists.postgresql.org'" Subject: Re: Reconnect a single connection used by multiple threads in embedded SQL in C application causes error. Message-ID: <20220223050015.GC3838703@rfd.leadboat.com> References: <20220221212928.GB3799825@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Tue, Feb 22, 2022 at 12:06:23PM +0000, egashira.yusuke@fujitsu.com wrote: > > > And, is there limitation that we can't CONNECT or DISCONNECT the DEFAULT connection inside a thread in multithreaded application? > > > > For now, yes. This is a bug. The documentation you quoted is out of date > > with respect to the code. That documentation comes from commit 8f61184 > > (2003-12). The behavior of connections shared across threads changed in > > commit 757fb0e (2004-03). Since that change, there's a per-thread record of > > the default connection, in addition to a global notion of the default > > connection. I suspect a few use cases work well today: > > > > - One thread does all the CONNECT and DISCONNECT commands. Other threads just > > use the default connection, with mutual exclusion. > > - Each thread does its own CONNECT, DISCONNECT, and other commands. Each > > thread has its own default connection. No sharing at all. > > The documentation from commit 8f61184 seems to have been updated by the commit 0f33ee0 (2017-06). > However, there is still lacking of information about the DEFAULT connection on per-thread, I understood. Yes. > > Your example does the CONNECT to establish the default connection in one > > thread, and it does the DISCONNECT of the default connection in another > > thread. That's buggy today. > > > > Another behavior that may qualify as a bug: ECPGsetconn() updates only the > > thread-specific connection, while ECPGconnect() updates both the > > thread-specific and global connections. > > I also understood that my example is impossible by the implementation bugs. > > I think it would take some time to fix these bugs. Yes. I suspect the hard part will be picking a specification that makes your case work better without breaking other important cases that work today. > If it takes long time to fix it, will this limitation(*) be added to the documentation? > > [*] Based on your explanation: > "If we use the default connection inside a thread in multithreaded application, > CONNECT and DISCONNECT commands are allowed only following cases. > - One thread does all the CONNECT and DISCONNECT commands. > Other threads just use the default connection, with mutual exclusion. > - Each thread does its own CONNECT, DISCONNECT, and other commands. > Each thread has its own default connection. No sharing at all." Would you like to propose a patch? > An additional report: > I also ran my reproduction application built with rh-postgresql12 libraries (installed from Red Hat Software Collections), > and it crashed instead of an error in step 5 in my first email. > > * Actually, the error message in my first email came from postgresql built by myself (./configure --prefix=xxx). > > The backtrace is following. > (gdb) bt > #0 0x00007fa11a4ddd0f in ecpg_find_prepared_statement () from /opt/rh/rh-postgresql12/root/usr//lib64/libecpg.so.rh-postgresql12-6 > #1 0x00007fa11a4de11b in ecpg_prepared () from /opt/rh/rh-postgresql12/root/usr//lib64/libecpg.so.rh-postgresql12-6 > #2 0x00007fa11a4d8b65 in ecpg_do_prologue () from /opt/rh/rh-postgresql12/root/usr//lib64/libecpg.so.rh-postgresql12-6 > #3 0x00007fa11a4d8c33 in ecpg_do () from /opt/rh/rh-postgresql12/root/usr//lib64/libecpg.so.rh-postgresql12-6 > #4 0x00007fa11a4d8d5b in ECPGdo () from /opt/rh/rh-postgresql12/root/usr//lib64/libecpg.so.rh-postgresql12-6 > #5 0x0000000000401143 in thread_main2 () > #6 0x00007fa11a2bdea5 in start_thread () from /lib64/libpthread.so.0 > #7 0x00007fa119fe68cd in clone () from /lib64/libc.so.6 > > I think this happened because the PGconn referenced by Thread#1 was destroyed in step 4 and then overwritten with invalid data. That makes sense.