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 1okOj7-0002mo-Eb for psycopg@arkaria.postgresql.org; Mon, 17 Oct 2022 11:56:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1okOj5-0008Tj-DC for psycopg@arkaria.postgresql.org; Mon, 17 Oct 2022 11:56:11 +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 1okOj5-0008TY-4B for psycopg@lists.postgresql.org; Mon, 17 Oct 2022 11:56:11 +0000 Received: from mail-qk1-x72f.google.com ([2607:f8b0:4864:20::72f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1okOiz-00084a-JP for psycopg@postgresql.org; Mon, 17 Oct 2022 11:56:10 +0000 Received: by mail-qk1-x72f.google.com with SMTP id o2so6395843qkk.10 for ; Mon, 17 Oct 2022 04:56:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=lz06KaeaJep4zmqO3VATNfivltERid6eoELvqFCGeUc=; b=lWI6FbS/0IZuogzOTbkga1X4fFm4KM0Hfv9D/MfXDg9r/NTmFEyNmjkhaws8zJiIBx AenaER6aSt6Wukxn4jpoqp0ZYA8VS/T+1H2bUFfh1N100Z8Tqae0xaoGASTmw0m1kdoZ FZKs45vqRjjOXxkEQ1FcWqsql/wa+tL1pFk6QiRt6YOFKazLHqVbaf9hVZmwIv8s0/uB 7FomlQ/yLPHrRhPhEWOfTLu/2mD93crr2WbH/to2Pk8oc/Jo+R3mlAMRqyTzSCCKx/kP TteQhzN/qCHO4EE9llrQ6HDAlMP2Zr0wG+Oae8VNPPkMTC49XN2wFwIerJ+WdibgRkFX 1d1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lz06KaeaJep4zmqO3VATNfivltERid6eoELvqFCGeUc=; b=50OCZEzEzcwma/R8xoPGlecDTnWoP7RhqcTADav7tKawLIS3Nrdo5a8B2GqJe1GdiG WlrEoVw7Yy4b4D1c03j5/kKhNcfRVVziI1l8GfjuzsiZMSWKrdRSp7CfD14IyGYz/KGU 1Qx0nUSh468u45q0nSJCKr/7Zi6EtReO95sqCJAu94iagkTXCWR+UIXY/b7uzkmf6lFE CoUmEgWyK9MWK2jhDNcgwPE2HbJLg/Bx10AmJseSvzITJ1CfMkP8ZxpvNc7Dp7r5lVwU tu4kyCrVeN792DOGoq8fGm/q0Uu7a0l2+naStqQSXyZC80IguxK3D8Lkjztvfbv3hvkW 82fw== X-Gm-Message-State: ACrzQf0m7WMHrc0v+0cc1G1eg52xnUPElf4VdETcB4UiltHlgVwP5POd iriPkUwrUu4Jtm7nypAaDenhnchS6KX7zLI2dK0= X-Google-Smtp-Source: AMsMyM7pG6K4rfj48UTTUx5Nu/w0NLD2/We8A87jsY6Cnsg7FpZa4/63eDJo4LxoLtGK6rRkGW5Cd7LNL5DKeFa+jBs= X-Received: by 2002:a05:620a:4546:b0:6ee:bf43:a8f4 with SMTP id u6-20020a05620a454600b006eebf43a8f4mr7429872qkp.673.1666007764654; Mon, 17 Oct 2022 04:56:04 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Daniele Varrazzo Date: Mon, 17 Oct 2022 12:55:53 +0100 Message-ID: Subject: Re: Puzzling situation with threads and psycopg2 on RDS To: Stefan Knecht Cc: psycopg@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Stefan, On Mon, 17 Oct 2022 at 12:25, Stefan Knecht wrote: > > Hello > > I have this situation now, that I can't wrap my head around and I'm hoping someone can shed some light onto what is going on, before I spend hours trying to reproduce this by taking the code apart into a test case. > > A few facts: > > - I've got a piece of code split into several classes. > - It initializes a dynamic number of threads. For the sake of this topic, I'll stick to just 2 threads to illustrate. > - Each thread creates its own database connection (psycopg2.connect()) when it's started > - Each thread, for each task it does, prints out its own name and the PID of the database session. ... > Perhaps you can already see the problem. One thread randomly disconnects. I understand this happens on RDS. Does it happen on normal Postgres too, from a normal process (not some serverless thing?) Are there processes/forks involved? Which version of psycopg are you using? Some old one have problems with closing connections in multiprocess environments. That was fixed in psycopg 2.8 (#829). Cheers -- Daniele