All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zifei Tong <zifeitong@gmail.com>
To: Kirill Batuzov <batuzovk@ispras.ru>
Cc: Gerd Hoffmann <kraxel@redhat.com>,
	Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>,
	Markus Armbruster <armbru@redhat.com>,
	Anthony Liguori <aliguori@amazon.com>,
	qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] qemu-char: Do not disconnect when there's data for reading
Date: Wed, 17 Sep 2014 18:44:23 +0800	[thread overview]
Message-ID: <CAJ7wx-FQSFowYROPo3DB5fkwH7DaxjvQm0L6NPMkJvZNyE-1Ww@mail.gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.02.1409161836130.6072@bulbul.intra.ispras.ru>

On Tue, Sep 16, 2014 at 11:30 PM, Kirill Batuzov <batuzovk@ispras.ru> wrote:
> On Tue, 16 Sep 2014, Markus Armbruster wrote:
>
>>
>> Kirill, you added the code being changed.  Could you review the patch?
>>
>
> I'll try but this is more about GIOConditions which I do not understand
> well. See below.
>
> Zifei Tong <zifeitong@gmail.com> writes:
>
>> After commit 812c1057f6175ac9a9829fa2920a2b5783814193 (Handle G_IO_HUP
>> in tcp_chr_read for tcp chardev), the connection is disconnected when in
>> G_IO_HUP condition.
>>
>> However, it's possible that the channel is in G_IO_IN condition at the
>> same time, meaning there is data for reading. In that case, the
>> remaining data is not handled.
>>
>> I saw a related bug when running socat in write-only mode, with
>>
>>   $ echo "quit" | socat -u - UNIX-CONNECT:qemu-monitor
>>
>> the monitor won't not run the 'quit' command.
>> CC: Kirill Batuzov <batuzovk@ispras.ru>
>> CC: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>
>> CC: Anthony Liguori <aliguori@amazon.com>
>> Signed-off-by: Zifei Tong <zifeitong@gmail.com>
>> ---
>>  qemu-char.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/qemu-char.c b/qemu-char.c
>> index 1a8d9aa..5018c3a 100644
>> --- a/qemu-char.c
>> +++ b/qemu-char.c
>> @@ -2706,7 +2706,7 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
>>      uint8_t buf[READ_BUF_LEN];
>>      int len, size;
>>
>> -    if (cond & G_IO_HUP) {
>> +    if (!(cond & G_IO_IN) && (cond & G_IO_HUP)) {
>>          /* connection closed */
>>          tcp_chr_disconnect(chr);
>>          return TRUE;

Hi Kirill,

Thanks for your review and detailed analysis.

> I've tried running the above code and watching in debugger what is
> happening. I wanted to know that tcp_chr_disconnect is called properly
> so I replaced the 'quit' command with 'help'. From the way code works I
> have a feeling that we are using some undocumented linux-specific
> behaviour here.
>
> What I saw:
>  - Sometimes all three (G_IO_IN, G_IO_HUP and G_IO_ERR) conditions are
>    asserted, sometimes only two of them (G_IO_IN and G_IO_HUP).
>  - G_IO_IN is *never* de-asserted. Even after all data is depleted it is
>    still up.
>  - When G_IO_ERR is asserted and all data have been read one call to
>    tcp_chr_recv returns -1. Subsequent calls return 0.
>
> GIOCondition behaviour in corner cases is puzzling and can differ from OS
> to OS (commit 812c105 is an example, there also were freebsd-specific
> bugfixes if I remember correctly).
>
> I suggest we remove condition checks completely and use more reliable
> and better documented source of information - return value of
> tcp_chr_recv (which is just return value of recvmsg). All we need to do
> is to handle 'size < 0' and not forget about EAGAIN case.

This sounds good to me. I'll send a V2 patch based on this.

Thanks,
Zifei

> Currently we have a mix of GLib conditions and POSIX return values to
> handle all cases and we can not do it with GLib alone (I do not know a
> way to tell if there is data in channel or not when G_IO_HUP is asserted).
>
> All these problems were before this patch, but I think it is better to
> fix it once than add patch over patch fighting GIOCondition's weird
> behaviour.
>
> --
> Kirill

      reply	other threads:[~2014-09-17 10:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-07 12:24 [Qemu-devel] [PATCH] qemu-char: Do not disconnect when there's data for reading Zifei Tong
2014-09-16  5:31 ` Zifei Tong
2014-09-24  7:52   ` [Qemu-devel] [Qemu-trivial] " Michael Tokarev
2015-09-10  8:03     ` Michael S. Tsirkin
2014-09-16  6:06 ` [Qemu-devel] " Markus Armbruster
2014-09-16  7:04   ` Zifei Tong
2014-09-16 15:30   ` Kirill Batuzov
2014-09-17 10:44     ` Zifei Tong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJ7wx-FQSFowYROPo3DB5fkwH7DaxjvQm0L6NPMkJvZNyE-1Ww@mail.gmail.com \
    --to=zifeitong@gmail.com \
    --cc=aliguori@amazon.com \
    --cc=armbru@redhat.com \
    --cc=batuzovk@ispras.ru \
    --cc=kraxel@redhat.com \
    --cc=n.nikolaev@virtualopensystems.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.