Bug #62578 mysql client aborts connection on terminal resize
Submitted: 30 Sep 2011 1:33 Modified: 5 Aug 2013 14:26
Reporter: Jervin R Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S3 (Non-critical)
Version:5.5 OS:Any
Assigned to: CPU Architecture:Any
Tags: MySQL, ssh, terminal

[30 Sep 2011 1:33] Jervin R
Description:
The mysql client that comes with 5.5.16 aborts a connection when terminal is resized. It appears the SIGWINCH signal is not being handled properly by the client.

Here is a test on 5.5.16 using iTerm2 on OSX (aborts with Gnome Terminal on Fedora Core 14 too)

[viiin@zoooe employees_db]$ strace ~/sandboxes/msb_5_5_16/use employees < employees.sql
execve("/home/viiin/sandboxes/msb_5_5_16/use", ["/home/viiin/sandboxes/msb_5_5_16", "employees"], [/* 23 vars */]) = 0
brk(0)                                  = 0x6630000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b037d16c000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b037d16d000
...
LOADING dept_emp
0x7ffffc07ef14, 0, NULL)      = ? ERESTARTSYS (To be restarted)
--- SIGWINCH (Window changed) @ 0 (0) ---
wait4(-1, 0x7ffffc07ef14, 0, NULL)      = ? ERESTARTSYS (To be restarted)
--- SIGWINCH (Window changed) @ 0 (0) ---
wait4(-1, 0x7ffffc07ef14, 0, NULL)      = ? ERESTARTSYS (To be restarted)
--- SIGWINCH (Window changed) @ 0 (0) ---
wait4(-1, 0x7ffffc07ef14, 0, NULL)      = ? ERESTARTSYS (To be restarted)
--- SIGWINCH (Window changed) @ 0 (0) ---
wait4(-1, 0x7ffffc07ef14, 0, NULL)      = ? ERESTARTSYS (To be restarted)
--- SIGWINCH (Window changed) @ 0 (0) ---
wait4(-1, 0x7ffffc07ef14, 0, NULL)      = ? ERESTARTSYS (To be restarted)
--- SIGWINCH (Window changed) @ 0 (0) ---
wait4(-1, 0x7ffffc07ef14, 0, NULL)      = ? ERESTARTSYS (To be restarted)
ERROR--- SIGWINCH (Window changed) @ 0 (0) ---
 2013 (HY000) at line 9 in file: 'load_dept_emp.dump': Lost connection to MySQL server during query
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 1}], 0, NULL) = 13974
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
--- SIGCHLD (Child exited) @ 0 (0) ---
wait4(-1, 0x7ffffc07eaa4, WNOHANG, NULL) = -1 ECHILD (No child processes)
rt_sigreturn(0xffffffffffffffff)        = 0
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x3df5c302d0}, {0x436f40, [], SA_RESTORER, 0x3df5c302d0}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
read(255, "", 1476)                     = 0
exit_group(1)                           = ?

How to repeat:
Import the employees database (employees.sql) and resize the Terminal while doing it.

Suggested fix:
None - not familiar with mysql client code.
[30 Sep 2011 1:35] Jervin R
Full tests output.

Attachment: bug-62578-tests.txt (text/plain), 29.85 KiB.

[3 Oct 2011 8:13] Valeriy Kravchuk
Thank you for the bug report. Verified with 5.5.17 on Fedora Core 14, using default Gnome terminal.
[23 May 2012 22:19] Omar Mehmood
This is still an issue on MySQL version 5.5.22.  Is there a workaround or patch available ?  I would think this is a critical issue for people who execute long-running SQLs in the background (e.g. a large database restore from a mysqldump).  Using screen doesn't help either as once you resume the screen session, if the terminal window size is different than the original, the MySQL connection dies.  I'd rather not wrap every single SQL or database restore that we execute in python (or equivalent) code as it's quite onerous.

Any suggestions would be greatly appreciated.
[23 Jul 2012 16:52] Vojtech Kurka
This affects us seriously on Centos 6. Really annoying, makes all work in terminal more than difficult - what's more, it reconnects automatically by default, so all session variables (i.e. SQL_LOG_BIN=0) are lost and can dramatically affect our production environment.
[15 Jan 2013 19:05] Eric McCormick
This is still happening in 5.5.27. Definitely very annoying as we use screen a lot. Any progress on this bug?
[22 Jan 2013 6:51] Thomas Lecomte
Still here on 5.5.28 (Debian 7.0).
[10 May 2013 3:09] Nate Schmoll
Still here in 5.5.30. Why is this not being fixed?
[11 May 2013 0:06] John O'Leary
Here's the 1 line patch that has worked well for us for almost a year:

=== modified file 'vio/viosocket.c'
--- vio/viosocket.c	2013-02-26 05:35:17 +0000
+++ vio/viosocket.c	2013-05-10 23:51:47 +0000
@@ -53,7 +53,7 @@
   r = recv(vio->sd, buf, size,0);
 #else
   errno=0;					/* For linux */
-  r = read(vio->sd, buf, size);
+  while((r = read(vio->sd, buf, size)) == (size_t) -1 && errno == EINTR);
 #endif /* __WIN__ */
 #ifndef DBUG_OFF
   if (r == (size_t) -1)

I made this change, rebuilt and reinstalled just the client, and the problem goes away.
[24 Jul 2013 18:40] MySQL Verification Team
Hello folks,

I am unable to repeat this with 5.6.12 on iTerm on OS X Snow Leopard.

I ran both very large `source` command and changed terminal size as crazy.

Then I tried a SELECT with very, very large Cartesian product with, again, changing terminal size a lot. Nothing happened. I repeated it for 20 minutes. Nothing. Rock - solid.

Can anyone check with 5.6.12 ???
[25 Jul 2013 7:35] Valeriy Kravchuk
I can not repeat this with 5.6.12 using putty connected to a remote Ubuntu 13.04 running Oracle MySQL binaries.

But this is still easily repeatable with 5.5.32:

mysql> create temporary table tt as select t1.* from mysql.user t1, mysql.user t2, mysql.user t3, mysql.user t4, mysql.user t5, mysql.user t6, mysql.user t7, mysql.user t8;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> show variables like 'version%';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    2
Current database: test

+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| version                 | 5.5.32                       |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | linux2.6                     |
+-------------------------+------------------------------+
4 rows in set (0.00 sec)

I've just tried to re-size putty window on Windows 7 while that Cartesian join was running. So, please, fix mysql client in 5.5, there is no risk and near zero efforts needed...
[25 Jul 2013 12:35] MySQL Verification Team
Valeriy Kravtchuk,

It was important for us to know whether this is fixed in later 5.5 or in 5.6. 

This info is important for further processing of this bug.
[30 Jul 2013 10:06] Przemyslaw Malkowski
I can confirm the problem still exists in 5.5.32 but not in 5.6.12. Tested on Linux. This is easy reproducible like below:

5.5.32 MySQL Community Server (GPL):

mysql [localhost] {msandbox} ((none)) > select sleep(10);
*(resize your terminal window)
ERROR 2013 (HY000): Lost connection to MySQL server during query

5.6.12-log MySQL Community Server (GPL):

mysql [localhost] {msandbox} ((none)) > select sleep(10);
*(resize your terminal window)
+-----------+
| sleep(10) |
+-----------+
|         0 |
+-----------+
1 row in set (10.00 sec)
[30 Jul 2013 10:13] Przemyslaw Malkowski
Also, I would like to mention that this bug has been already fixed in Percona Server since 5.5.31. This is corresponding bug report:
https://bugs.launchpad.net/percona-server/+bug/925343
[5 Aug 2013 14:26] MySQL Verification Team
it is dangerous to back-port the framework code needed from 5.6 to 5.5. Dealing with such low level code is always prone to problems.

A framework developed for 5.6 is so large that porting it back to 5.5 would generate definitely some regression bugs.