pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection
6+ messages / 4 participants
[nested] [flat]

* BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection
@ 2013-12-02 19:30  valgog@gmail.com
  0 siblings, 1 reply; 6+ messages in thread

From: valgog@gmail.com @ 2013-12-02 19:30 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      8647
Logged by:          Valentine Gogichashvili
Email address:      valgog@gmail.com
PostgreSQL version: 9.2.4
Operating system:   Linux 3.2.0-48-generic Ubuntu x86_64
Description:        

If a TCP connection of the SSL client (psql in our case) is gone, during
execution of a query, that is preparing a big dataset, the process on the
server side (the backend process) becomes unkillable, and as soon as the OS
cleans the TCP connection away, the process starts to consume up to 60% of
system time for it's processor and is still unkillable.  


We can reproduce the problem on all the versions from 9.0 up to 9.2. On the
9.3 the problem is unreproducible though.


If needed I could try to do some more experiments and collect dbg and strace
information about that hanging processes.



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection
@ 2013-12-05 09:59  Andres Freund <andres@2ndquadrant.com>
  parent: valgog@gmail.com
  0 siblings, 0 replies; 6+ messages in thread

From: Andres Freund @ 2013-12-05 09:59 UTC (permalink / raw)
  To: valgog@gmail.com; +Cc: pgsql-bugs

On 2013-12-02 19:30:15 +0000, valgog@gmail.com wrote:
> If a TCP connection of the SSL client (psql in our case) is gone, during
> execution of a query, that is preparing a big dataset, the process on the
> server side (the backend process) becomes unkillable, and as soon as the OS
> cleans the TCP connection away, the process starts to consume up to 60% of
> system time for it's processor and is still unkillable.  

> If needed I could try to do some more experiments and collect dbg and strace
> information about that hanging processes.

Yes, that would be good. Possibly also a profile showing where time is
spent.

Greetings,

Andres Freund

-- 
 Andres Freund	                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection
@ 2013-12-05 11:32  Alexander Kukushkin <cyberdemn@gmail.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Alexander Kukushkin @ 2013-12-05 11:32 UTC (permalink / raw)
  To: pgsql-bugs

Hi all,

I am sorry, but previously I was not subscribed to this maillist and can't
directly reply to original message.

In this  message you will find all needed information about hanging
backends when SSL enabled.

Recently we had enabled ssl on our postgres servers and started observe
some hanging (unkillable) postgres processes.
They appears usually when client has died or connection for some reason has
gone. I am able reproduce such problem on 9.0, 9.1 and 9.2, but not on 9.3.
To do this I am running relatively big query which produce big enough data
to fill system buffers, then I am stopping psql with Ctrl+Z and with
iptables forbidding all communication between server and client.
After that you could even kill psql, but on server side connection will be
still in state ESTABLISHED and server stuck on sendto system call.

After some time 15-30 minutes server changes his state start doing strange
things:
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
sendto(10,
"\0\346\203\300\350a6\326G\206\266\237q\220\6\20\217\234\344D\232S\234\234=\306\255=[\342\225o"...,
7750, 0, NULL, 0) = -1 EPIPE (Broken pipe)

Definitely it retry send data to client in infinite loop without proper
handling of errors.
After analysing source code of postgres and openssl I've found (at least I
think) the reason why it is happening:
----------------------------------------------------------------------------------------------------------------------------
static int
my_sock_write(BIO *h, const char *buf, int size)
{
        int                     res = 0;

        res = send(h->num, buf, size, 0);
        if (res <= 0)
        {
                if (errno == EINTR)
                {
                        BIO_set_retry_write(h);
                }
        }

        return res;
}

static BIO_METHOD *
my_BIO_s_socket(void)
{
        if (!my_bio_initialized)
        {
                memcpy(&my_bio_methods, BIO_s_socket(), sizeof(BIO_METHOD));
                my_bio_methods.bread = my_sock_read;
                my_bio_methods.bwrite = my_sock_write;
                my_bio_initialized = true;
        }
        return &my_bio_methods;
}
----------------------------------------------------------------------------------------------------------------------------

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Re: BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection
@ 2013-12-05 14:54  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alexander Kukushkin <cyberdemn@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Tom Lane @ 2013-12-05 14:54 UTC (permalink / raw)
  To: Alexander Kukushkin <cyberdemn@gmail.com>; +Cc: pgsql-bugs

Alexander Kukushkin <cyberdemn@gmail.com> writes:
> After analysing source code of postgres and openssl I've found (at least I
> think) the reason why it is happening:
> ----------------------------------------------------------------------------------------------------------------------------
> static int
> my_sock_write(BIO *h, const char *buf, int size)
> {
>         int                     res = 0;

>         res = send(h->num, buf, size, 0);
>         if (res <= 0)
>         {
>                 if (errno == EINTR)
>                 {
>                         BIO_set_retry_write(h);
>                 }
>         }

>         return res;
> }

BTW, I was looking at that function the other day and thinking it was
wrong: if the send() returns exactly zero, it hasn't set errno has it?
But perhaps that case never arises.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Re: BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection
@ 2013-12-05 16:07  Alexander Kukushkin <cyberdemn@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Alexander Kukushkin @ 2013-12-05 16:07 UTC (permalink / raw)
  To: pgsql-bugs

2013/12/5 Tom Lane <tgl@sss.pgh.pa.us>

> Alexander Kukushkin <cyberdemn@gmail.com> writes:
> > After analysing source code of postgres and openssl I've found (at least
> I
> > think) the reason why it is happening:
> >
> ----------------------------------------------------------------------------------------------------------------------------
> > static int
> > my_sock_write(BIO *h, const char *buf, int size)
> > {
> >         int                     res = 0;
>
> >         res = send(h->num, buf, size, 0);
> >         if (res <= 0)
> >         {
> >                 if (errno == EINTR)
> >                 {
> >                         BIO_set_retry_write(h);
> >                 }
> >         }
>
> >         return res;
> > }
>
> BTW, I was looking at that function the other day and thinking it was
> wrong: if the send() returns exactly zero, it hasn't set errno has it?
> But perhaps that case never arises.
>
>                         regards, tom lane
>

According to documentation, "On success, these calls return the number of
characters sent.  On error, -1 is returned, and errno is set appropriately."
So, return zero is not an error and errno should not be set.
I guess you could get zero only in case when you are sending an empty
buffer or in nonblocking mode, which obviously is not the case.

But, returning to my original message... I noticed that I've made stupid
copy&paste error


static int
my_sock_write(BIO *h, const char *buf, int size)
{
        int                     res = 0;

        res = send(h->num, buf, size, 0);
+        BIO_clear_retry_flags(h); // <-- here, should be "h" not "b" as in
original message.

        if (res <= 0)
        {
                if (errno == EINTR)
                {
                        BIO_set_retry_write(h);
                }

Best regards,
Alexander Kukushkin

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Re: BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection
@ 2013-12-05 17:33  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alexander Kukushkin <cyberdemn@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Tom Lane @ 2013-12-05 17:33 UTC (permalink / raw)
  To: Alexander Kukushkin <cyberdemn@gmail.com>; +Cc: pgsql-bugs

Alexander Kukushkin <cyberdemn@gmail.com> writes:
> For me fix of this issue should look like following:
> ----------------------------------------------------------------------------------------------------------------------------
> static int
> my_sock_write(BIO *h, const char *buf, int size)
> {
>         int                     res = 0;

>         res = send(h->num, buf, size, 0);
> +        BIO_clear_retry_flags(b);
>         if (res <= 0)
>         {
>                 if (errno == EINTR)
>                 {
>                         BIO_set_retry_write(h);
>                 }
>         }

>         return res;
> }

I looked into the source code of current openssl (1.0.1e) and confirmed
that there's a BIO_clear_retry_flags step in the code that we're copying
(sock_write in crypto/bio/bss_sock.c).  Possibly we screwed up when we
made that copy, but my money is on the theory that this is a bug fix
that appeared in upstream openssl sometime after we cloned the function.
(There is a BIO_clear_retry_flag step in sock_read, both our version
and theirs.)

Anyway, it seems clear that we want to adopt this change, particularly
now that you've demonstrated a user-visible bug from not resetting the
flags here.

Will commit, thanks for the report!

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2013-12-05 17:33 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-12-02 19:30 BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection valgog@gmail.com
2013-12-05 09:59 ` Andres Freund <andres@2ndquadrant.com>
2013-12-05 11:32 Re: BUG #8647: Backend process hangs and becomes unkillable when SSL client looses connection Alexander Kukushkin <cyberdemn@gmail.com>
2013-12-05 14:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2013-12-05 16:07   ` Alexander Kukushkin <cyberdemn@gmail.com>
2013-12-05 17:33 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox