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 1ntRYc-0007S5-SO for pgsql-bugs@arkaria.postgresql.org; Tue, 24 May 2022 10:14:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ntRYb-0002SS-E6 for pgsql-bugs@arkaria.postgresql.org; Tue, 24 May 2022 10:14:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ntRYb-0002SJ-4Q for pgsql-bugs@lists.postgresql.org; Tue, 24 May 2022 10:14:29 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ntRYY-00078B-4W for pgsql-bugs@lists.postgresql.org; Tue, 24 May 2022 10:14:28 +0000 Received: by mail-pj1-x1035.google.com with SMTP id oe17-20020a17090b395100b001df77d29587so1678029pjb.2 for ; Tue, 24 May 2022 03:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=tBS/yonJwIHYegnESIpDM2Qr3AMPumlQscQIZBVgtzw=; b=hKyYykdTcok9yARrGRs3zvlRlYWODeMieyZzlgpzkQ2C2GcFOIP2xbIGXv1qckH/dM Up9TxIttkPgj3YsGbRPP5xj7tvCfu3AxtHFwDaXhI+nQBVhNR+OUnGYzk/3qyr7GwvMo 0PsMPAwPDh0pFW086mPCzx9EsPfb96jIUlsFUSuSxILWSWxwyr6jRVyMgKE0cmjv1sVG NYnqLD+L7y7RfAkmCrHaSrXZTpVd/3IWmibG1MuC9ori5mobuFalKDAOUNcGIm5f/0Bh kFQWN/Bw4F75qyt6vp4E/+g0QYBrm3UGAnQn6jtRKcMF2S7NBWsdnpAZ4ptiw5qhi1Os xmIw== 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; bh=tBS/yonJwIHYegnESIpDM2Qr3AMPumlQscQIZBVgtzw=; b=uQFsMJu77Ml7kv6AsET0Q7zHTU5ZFggOO65D+aAEgU93PnEd+7Jeq5lNyidg1mkYwq W8+YBiPsjB1H7reRjDGCcFDEr+IW2Il6Ahnv9F60nRupioZnMNrQ4Z8axWCKToqvc1pI 4mjXYHeWZ0/8aP6SQZH4/Ob/5qOpCo2rsafyXOPg9cD2e/hIZDCOFfbo/6xBZCqA2VkF E4l5AqjFelvqqbNHq2X3kQP6hXg9KHpN4iofLjdkfch5EggQSJAqXwIJVOBrMlnu60i3 EbYG8I7NzFnsEN5IB9QMOrMsPr+AiD2NZlVInu8nwvW0/4kPgH4lN+lipkPN/BxejexF McqQ== X-Gm-Message-State: AOAM531fDV0jaK68Gh8PDoc/7ost3oJ0DfH7vlh4HxLpELMRv8F5zYLS c00y4SQWua2s4F6MdIlltf8= X-Google-Smtp-Source: ABdhPJyG4ImkEti8IZfKpLm7cixWfmgG7mEBFTd7wcpjvr9Gew/6QA9ZPRqTCqk39kO6qh7o68sBuw== X-Received: by 2002:a17:902:eb90:b0:162:6a4:e76 with SMTP id q16-20020a170902eb9000b0016206a40e76mr15368157plg.111.1653387263990; Tue, 24 May 2022 03:14:23 -0700 (PDT) Received: from jrouhaud (2001-b011-1005-f8ab-0409-e419-8b11-6ced.dynamic-ip6.hinet.net. [2001:b011:1005:f8ab:409:e419:8b11:6ced]) by smtp.gmail.com with ESMTPSA id z17-20020a170903019100b00161c3fc7a44sm6958050plg.108.2022.05.24.03.14.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 03:14:23 -0700 (PDT) Date: Tue, 24 May 2022 18:14:18 +0800 From: Julien Rouhaud To: Mats Kindahl Cc: pgsql-bugs@lists.postgresql.org Subject: Re: Use of signal-unsafe functions from signal handlers Message-ID: <20220524101418.7pp65powa362vzln@jrouhaud> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Tue, May 24, 2022 at 11:42:35AM +0200, Mats Kindahl wrote: > > Typically, signal-unsafe functions should not be called from signal > handlers. In particular, calling malloc() directly or indirectly can cause > deadlocks, making PostgreSQL unresponsive to signals. > > Unless I am missing something, bgworker_die uses ereport, which indirectly > calls printf-like functions, which are not signal-safe since they use > malloc(). In rare cases, this can lead to deadlocks with stacks that look > like this (from https://github.com/timescale/timescaledb/issues/4200): > > #0 0x00007f0e4d1040eb in __lll_lock_wait_private () from > target:/lib/x86_64-linux-gnu/libc.so.6 > [...] > #3 malloc (size=53) > [...] > #7 0x000055b9212235b1 in errmsg () > #8 0x00007f0e27bf27a8 in handle_sigterm (postgres_signal_arg=15) at > /build/timescaledb/src/bgw/scheduler.c:841 > #9 > [...] > #13 free (ptr=) > #14 0x00007f0e4db12cb4 in OPENSSL_LH_free () from > target:/lib/x86_64-linux-gnu/libcrypto.so.1.1 > [...] As far as I can see the problem comes from your handle_sigterm: static void handle_sigterm(SIGNAL_ARGS) { /* * do not use a level >= ERROR because we don't want to exit here but * rather only during CHECK_FOR_INTERRUPTS */ ereport(LOG, (errcode(ERRCODE_ADMIN_SHUTDOWN), errmsg("terminating TimescaleDB job scheduler due to administrator command"))); die(postgres_signal_arg); } As mentioned in the topmost comment of elog.c, there's an escape hatch for out of memory situations, to make sure that a small enough message can be displayed without trying to allocate memory. But this is only for ERROR or higher levels. Using an ereport(LOG, ...) level in a sigterm handler indeed isn't safe.