qemu-char: Do not disconnect when there's data for reading
PATCH 1/1 qemu-char: Do not disconnect when there's data for reading
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.
--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-char.c +1 -1
@@ -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;
Signature · 1 line
2.1.0
Friendly ping.
Also CC to qemu-trivial.
Quoted 38 lines · Zifei Tong
After commit
812c1057f6175ac9a9829fa2920a2b5783814193(HandleG_IO_HUPintcp_chr_readfor tcp chardev), the connection is disconnected when inG_IO_HUPcondition.However, it's possible that the channel is in
G_IO_INcondition 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-monitorthe monitor won't not run the 'quit' command.
CC Kirill Batuzov <[email protected]>CC Nikolay Nikolaev <[email protected]>CC Anthony Liguori <[email protected]>Signed-off-by Zifei Tong <[email protected]>--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-char.c +1
-2@@ -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; --2.1.0
Quoted 3 lines · Zifei Tong
Friendly ping.
Also CC to qemu-trivial.
There was at least 2 subsequent versions of this down the line, a v2 and a v3. (Just to show why I haven't applied it to -trivial).
Thanks,
/mjt
Quoted 40 lines · Zifei Tong
Quoted 38 lines
After commit
812c1057f6175ac9a9829fa2920a2b5783814193(HandleG_IO_HUPintcp_chr_readfor tcp chardev), the connection is disconnected when inG_IO_HUPcondition.However, it's possible that the channel is in
G_IO_INcondition 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-monitorthe monitor won't not run the 'quit' command.
CC Kirill Batuzov <[email protected]>CC Nikolay Nikolaev <[email protected]>CC Anthony Liguori <[email protected]>Signed-off-by Zifei Tong <[email protected]>--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-char.c +1
-2@@ -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; --2.1.0
Quoted 11 lines · Michael Tokarev
Quoted 3 lines · Zifei Tong
Friendly ping.
Also CC to qemu-trivial.
There was at least 2 subsequent versions of this down the line, a v2 and a v3. (Just to show why I haven't applied it to -trivial).
Thanks,
/mjt
It's also far from trivial.
I don't see why it applies there.
Quoted 41 lines
Quoted 40 lines · Zifei Tong
Quoted 38 lines
After commit
812c1057f6175ac9a9829fa2920a2b5783814193(HandleG_IO_HUPintcp_chr_readfor tcp chardev), the connection is disconnected when inG_IO_HUPcondition.However, it's possible that the channel is in
G_IO_INcondition 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-monitorthe monitor won't not run the 'quit' command.
CC Kirill Batuzov <[email protected]>CC Nikolay Nikolaev <[email protected]>CC Anthony Liguori <[email protected]>Signed-off-by Zifei Tong <[email protected]>--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-char.c +1
-2@@ -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; --2.1.0
Cc'ing Gerd for additional chardev expertise.
Quoted 8 lines · Zifei Tong
After commit
812c1057f6175ac9a9829fa2920a2b5783814193(HandleG_IO_HUPintcp_chr_readfor tcp chardev), the connection is disconnected when inG_IO_HUPcondition.However, it's possible that the channel is in
G_IO_INcondition at the same time, meaning there is data for reading. In that case, the remaining data is not handled.
Quoted 5 lines
I saw a related bug when running socat in write-only mode, with
$ echo "quit" | socat -u - UNIX-CONNECT:qemu-monitorthe monitor won't not run the 'quit' command.
Reproduced.
Is this a regression caused by command 812c105?
Quoted 13 lines
CC Kirill Batuzov <[email protected]>CC Nikolay Nikolaev <[email protected]>CC Anthony Liguori <[email protected]>Signed-off-by Zifei Tong <[email protected]>--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-char.c +0
-0
qemu-char.c static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) · line 2712
Quoted 8 lines
qemu-char.c +1
-1@@ -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;
Kirill, you added the code being changed. Could you review the patch?
Quoted 14 lines · Markus Armbruster
Cc'ing Gerd for additional chardev expertise.
Quoted 10 lines · Zifei Tong
After commit
812c1057f6175ac9a9829fa2920a2b5783814193(HandleG_IO_HUPintcp_chr_readfor tcp chardev), the connection is disconnected when inG_IO_HUPcondition.However, it's possible that the channel is in
G_IO_INcondition 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
Quoted 7 lines
Quoted 3 lines
$ echo "quit" | socat -u - UNIX-CONNECT:qemu-monitorthe monitor won't not run the 'quit' command.
Reproduced.
Is this a regression caused by command 812c105?
The first bad commit is cdaa86 ("Add G_IO_HUP handler for socket chardev"), which is reverted and reimplemented by 812c105.
Quoted 23 lines
Quoted 13 lines
CC Kirill Batuzov <[email protected]>CC Nikolay Nikolaev <[email protected]>CC Anthony Liguori <[email protected]>Signed-off-by Zifei Tong <[email protected]>--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-char.c +0
-0qemu-char.c static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) · line 2712
Quoted 8 lines
qemu-char.c +1
-1@@ -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;Kirill, you added the code being changed. Could you review the patch?
Quoted 3 lines · Markus Armbruster
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.
Quoted 26 lines · Zifei Tong
After commit
812c1057f6175ac9a9829fa2920a2b5783814193(HandleG_IO_HUPintcp_chr_readfor tcp chardev), the connection is disconnected when inG_IO_HUPcondition.However, it's possible that the channel is in
G_IO_INcondition 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-monitorthe monitor won't not run the 'quit' command.
CC Kirill Batuzov <[email protected]>CC Nikolay Nikolaev <[email protected]>CC Anthony Liguori <[email protected]>Signed-off-by Zifei Tong <[email protected]>--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-char.c +0
-0
qemu-char.c static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) · line 2712
Quoted 8 lines
qemu-char.c +1
-1@@ -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;
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.
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.
Signature · 1 line
Kirill
Quoted 40 lines · Kirill Batuzov
Quoted 3 lines · Markus Armbruster
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.
Quoted 29 lines · Zifei Tong
After commit
812c1057f6175ac9a9829fa2920a2b5783814193(HandleG_IO_HUPintcp_chr_readfor tcp chardev), the connection is disconnected when inG_IO_HUPcondition.However, it's possible that the channel is in
G_IO_INcondition 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-monitorthe monitor won't not run the 'quit' command.
CC Kirill Batuzov <[email protected]>CC Nikolay Nikolaev <[email protected]>CC Anthony Liguori <[email protected]>Signed-off-by Zifei Tong <[email protected]>--- qemu-char.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qemu-char.c +0
-0@@ -2706,7 +2706,7 @@ static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)uint8_t buf[READ_BUF_LEN ];int len, size;
Quoted 5 lines
qemu-char.c static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) · line 2712
Quoted 5 lines
qemu-char.c +1
-1-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.
Quoted 14 lines
I've tried running the above code and watching in debugger what is happening. I wanted to know that
tcp_chr_disconnectis 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_HUPandG_IO_ERR) conditions are asserted, sometimes only two of them (G_IO_INandG_IO_HUP).
-G_IO_INis *never* de-asserted. Even after all data is depleted it is still up.
- WhenG_IO_ERRis asserted and all data have been read one call totcp_chr_recvreturns -1. Subsequent calls return 0.
Quoted 8 lines
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
Quoted 10 lines
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_HUPis 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