Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dQZSC-0007e6-Ou for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Jun 2017 13:25:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dQZSC-0006jn-3R for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Jun 2017 13:25:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dQZQ5-0006VY-58 for pgsql-hackers@postgresql.org; Thu, 29 Jun 2017 13:23:41 +0000 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dQZQ1-0001KF-I2 for pgsql-hackers@postgresql.org; Thu, 29 Jun 2017 13:23:40 +0000 Received: by mail-wm0-x230.google.com with SMTP id b184so14519289wme.1 for ; Thu, 29 Jun 2017 06:23:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greycortex-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=JRsAVvAzoeJHZdoSbwstrHEyGa+rRmjAJiYNl1nTX/s=; b=xKiqdPmMwknFcxAM5Rwu+JGqFcgxhPdqScKUXAzbLodYUVJCQ5QGQqolgMKfm8zgGW /A0uPm2G/QGVpgzLwro6Aq0yT55e5ctb+bNqJXcUaOeULnqixNKCIDvzJ2L6xz9F56bn Mx8483pgaa/u2Odzl/X2dVjspEG4ZU+f8Vilczl+BbUwVc/eY0LkmftgDdYzpVewUA2u 3QiLh6UzzDsi1gJL+ZyRnzHC0Ht26jmX/AJFj6HAoBXVYblKnCIiorhWxy66k8Tuh9IG +XbSImvl+Mee1F1QM/CJF+XJ90/K6bxtDTcVduEY+6oDG5A/Uk9Tk0rHiK2vDJwYiE4D +Mtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=JRsAVvAzoeJHZdoSbwstrHEyGa+rRmjAJiYNl1nTX/s=; b=GP4YKdf2tSe5VnEs+cYOvStznlTHmdKyi0lLAYIjMc6SpTFnjeYsas/zaWyeyBS4uQ 8r9nxnWdue/mTCqjSpzqdc0h5sLf+LQycaYJwvRJLgdy4AEU7ux1StrLy2h/EEYCEeIs DCK1F14mybedKaVJlkBohewRJb0AwSC4QWyLIJk/4rZROZSV1mwWMhfddoguP3cXdxzk 2YTAW/9wyolQAyhJ0HZYQfpcxR6o6yCzXfFQzlR9lpoVP/uGBTsjYL1zsJ2v7Wev8Xqx z6s32UT8UWxg9SQLTIcalj3ENbPTU/g8+3rPGG2kpJZmAiDjS1H5mP6ofmzQPDTlEdqm oTwg== X-Gm-Message-State: AKS2vOwIQn8xWpyrqHGD5St3EA//1I2DlByByJY5JKCKQ/U8273Aw47Z Kv5v73dtukPySYSgTqEdcg== X-Received: by 10.28.18.145 with SMTP id 139mr11202611wms.2.1498742616021; Thu, 29 Jun 2017 06:23:36 -0700 (PDT) Received: from [172.16.9.180] ([195.113.243.75]) by smtp.gmail.com with ESMTPSA id p87sm13358657wma.2.2017.06.29.06.23.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Jun 2017 06:23:35 -0700 (PDT) Subject: Re: Segmentation fault in libpq To: Merlin Moncure Cc: pgsql-bugs , PostgreSQL-development References: From: Michal Novotny Message-ID: <2a0f743f-4d96-30b1-8b5d-86541e8bcb34@greycortex.com> Date: Thu, 29 Jun 2017 15:23:34 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org Hi, comments inline ... On 06/29/2017 03:08 PM, Merlin Moncure wrote: > On Thu, Jun 29, 2017 at 4:01 AM, Michal Novotny > wrote: >> Hi all, >> >> we've developed an application using libpq to access a table in the PgSQL >> database but we're sometimes experiencing segmentation fault on >> resetPQExpBuffer() function of libpq called from PQexecParams() with >> prepared query. >> >> PostgreSQL version is 9.6.3 and the backtrace is: >> >> Core was generated by `/usr/ti/bin/status-monitor2 -m >> /usr/lib64/status-monitor2/modules'. >> Program terminated with signal 11, Segmentation fault. >> #0 resetPQExpBuffer (str=str@entry=0x9f4a28) at pqexpbuffer.c:152 >> 152 str->data[0] = '\0'; >> >> Thread 1 (Thread 0x7fdf68de3840 (LWP 3525)): >> #0 resetPQExpBuffer (str=str@entry=0x9f4a28) at pqexpbuffer.c:152 >> No locals. >> #1 0x00007fdf66e0333d in PQsendQueryStart (conn=conn@entry=0x9f46d0) at >> fe-exec.c:1371 >> No locals. >> #2 0x00007fdf66e044b9 in PQsendQueryParams (conn=conn@entry=0x9f46d0, >> command=command@entry=0x409a98 "SELECT min, hour, day, month, dow, sensor, >> module, params, priority, rt_due FROM sm.cron WHERE sensor = $1 ORDER BY >> priority DESC", nParams=nParams@entry=1, paramTypes=paramTypes@entry=0x0, >> paramValues=paramValues@entry=0xa2b7b0, paramLengths=paramLengths@entry=0x0, >> paramFormats=paramFormats@entry=0x0, resultFormat=resultFormat@entry=0) at >> fe-exec.c:1192 >> No locals. >> #3 0x00007fdf66e0552b in PQexecParams (conn=0x9f46d0, command=0x409a98 >> "SELECT min, hour, day, month, dow, sensor, module, params, priority, rt_due >> FROM sm.cron WHERE sensor = $1 ORDER BY priority DESC", nParams=1, >> paramTypes=0x0, paramValues=0xa2b7b0, paramLengths=0x0, paramFormats=0x0, >> resultFormat=0) at fe-exec.c:1871 >> No locals. >> >> Unfortunately we didn't have more information from the crash, at least for >> now. >> >> Is this a known issue and can you help me with this one? > Is your application written in C? We would need to completely rule > out your code (say, by double freeing result or something else nasty) > before assuming problem was withing libpq itself, particularly in this > area of the code. How reproducible is the problem? > > merlin The application is written in plain C. The issue is it happens just sometimes - sometimes it happens and sometimes it doesn't. Once it happens it causes the application crash but as it's systemd unit with Restart=on-failure flag it's automatically being restarted. What's being done is: 1) Ensure connection already exists and create a new one if it doesn't exist yet 2) Run PQexecParams() with specified $params that has $params_cnt elements: res = PQexecParams(conn, prepared_query, params_cnt, NULL, (const char **)params, NULL, NULL, 0); 3) Check for result and report error and exit if "PQresultStatus(res) != PGRES_TUPLES_OK" 4) Do some processing with the result 5) Clear result using PQclear() It usually works fine but sometimes it's crashing and I don't know how to investigate further. Could you please help me based on information provided above? Thanks, Michal -- Michal Novotny System Development Lead michal.novotny@greycortex.com GREYCORTEX s.r.o. Purkynova 127, 61200 Brno Czech Republic www.greycortex.com -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers