forked from aniani/vim
patch 7.4.1249
Problem: Crash when the process a channel is connected to exits. Solution: Use the file descriptor properly. Add a test. (Damien) Also add a test for eval().
This commit is contained in:
@@ -698,10 +698,14 @@ channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
typval_T *tv = eval_expr(arg, NULL);
|
typval_T *tv;
|
||||||
typval_T err_tv;
|
typval_T err_tv;
|
||||||
char_u *json;
|
char_u *json;
|
||||||
|
|
||||||
|
/* Don't pollute the display with errors. */
|
||||||
|
++emsg_skip;
|
||||||
|
tv = eval_expr(arg, NULL);
|
||||||
|
--emsg_skip;
|
||||||
if (is_eval)
|
if (is_eval)
|
||||||
{
|
{
|
||||||
if (tv == NULL)
|
if (tv == NULL)
|
||||||
@@ -714,7 +718,8 @@ channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
|
|||||||
channel_send(idx, json, "eval");
|
channel_send(idx, json, "eval");
|
||||||
vim_free(json);
|
vim_free(json);
|
||||||
}
|
}
|
||||||
free_tv(tv);
|
if (tv != &err_tv)
|
||||||
|
free_tv(tv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (p_verbose > 2)
|
else if (p_verbose > 2)
|
||||||
@@ -1119,7 +1124,8 @@ channel_read_json_block(int ch_idx, int id, typval_T **rettv)
|
|||||||
|
|
||||||
/* Wait for up to 2 seconds.
|
/* Wait for up to 2 seconds.
|
||||||
* TODO: use timeout set on the channel. */
|
* TODO: use timeout set on the channel. */
|
||||||
if (channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
|
if (channels[ch_idx].ch_fd < 0
|
||||||
|
|| channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
|
||||||
break;
|
break;
|
||||||
channel_read(ch_idx);
|
channel_read(ch_idx);
|
||||||
}
|
}
|
||||||
|
@@ -52,7 +52,6 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
|
|||||||
decoded = [-1, '']
|
decoded = [-1, '']
|
||||||
|
|
||||||
# Send a response if the sequence number is positive.
|
# Send a response if the sequence number is positive.
|
||||||
# Negative numbers are used for "eval" responses.
|
|
||||||
if decoded[0] >= 0:
|
if decoded[0] >= 0:
|
||||||
if decoded[1] == 'hello!':
|
if decoded[1] == 'hello!':
|
||||||
# simply send back a string
|
# simply send back a string
|
||||||
@@ -65,9 +64,27 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
|
|||||||
print("sending: {}".format(cmd))
|
print("sending: {}".format(cmd))
|
||||||
thesocket.sendall(cmd.encode('utf-8'))
|
thesocket.sendall(cmd.encode('utf-8'))
|
||||||
response = "ok"
|
response = "ok"
|
||||||
|
elif decoded[1] == 'eval-works':
|
||||||
|
# Send an eval request. We ignore the response.
|
||||||
|
cmd = '["eval","\\"foo\\" . 123", -1]'
|
||||||
|
print("sending: {}".format(cmd))
|
||||||
|
thesocket.sendall(cmd.encode('utf-8'))
|
||||||
|
response = "ok"
|
||||||
|
elif decoded[1] == 'eval-fails':
|
||||||
|
# Send an eval request that will fail.
|
||||||
|
cmd = '["eval","xxx", -2]'
|
||||||
|
print("sending: {}".format(cmd))
|
||||||
|
thesocket.sendall(cmd.encode('utf-8'))
|
||||||
|
response = "ok"
|
||||||
|
elif decoded[1] == 'eval-result':
|
||||||
|
# Send back the last received eval result.
|
||||||
|
response = last_eval
|
||||||
elif decoded[1] == '!quit!':
|
elif decoded[1] == '!quit!':
|
||||||
# we're done
|
# we're done
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
elif decoded[1] == '!crash!':
|
||||||
|
# Crash!
|
||||||
|
42 / 0
|
||||||
else:
|
else:
|
||||||
response = "what?"
|
response = "what?"
|
||||||
|
|
||||||
@@ -75,6 +92,10 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
|
|||||||
print("sending: {}".format(encoded))
|
print("sending: {}".format(encoded))
|
||||||
thesocket.sendall(encoded.encode('utf-8'))
|
thesocket.sendall(encoded.encode('utf-8'))
|
||||||
|
|
||||||
|
# Negative numbers are used for "eval" responses.
|
||||||
|
elif decoded[0] < 0:
|
||||||
|
last_eval = decoded
|
||||||
|
|
||||||
thesocket = None
|
thesocket = None
|
||||||
|
|
||||||
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
@@ -18,25 +18,14 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
func s:start_server()
|
func s:start_server()
|
||||||
|
" The Python program writes the port number in Xportnr.
|
||||||
|
call delete("Xportnr")
|
||||||
|
|
||||||
if has('win32')
|
if has('win32')
|
||||||
silent !start cmd /c start "test_channel" py test_channel.py
|
silent !start cmd /c start "test_channel" py test_channel.py
|
||||||
else
|
else
|
||||||
silent !python test_channel.py&
|
silent !python test_channel.py&
|
||||||
endif
|
endif
|
||||||
endfunc
|
|
||||||
|
|
||||||
func s:kill_server()
|
|
||||||
if has('win32')
|
|
||||||
call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
|
|
||||||
else
|
|
||||||
call system("pkill --full test_channel.py")
|
|
||||||
endif
|
|
||||||
endfunc
|
|
||||||
|
|
||||||
func Test_communicate()
|
|
||||||
call delete("Xportnr")
|
|
||||||
" The Python program writes the port number in Xportnr.
|
|
||||||
call s:start_server()
|
|
||||||
|
|
||||||
" Wait for up to 2 seconds for the port number to be there.
|
" Wait for up to 2 seconds for the port number to be there.
|
||||||
let cnt = 20
|
let cnt = 20
|
||||||
@@ -57,10 +46,28 @@ func Test_communicate()
|
|||||||
if len(l) == 0
|
if len(l) == 0
|
||||||
" Can't make the connection, give up.
|
" Can't make the connection, give up.
|
||||||
call s:kill_server()
|
call s:kill_server()
|
||||||
return
|
call assert_false(1, "Can't start test_channel.py")
|
||||||
|
return -1
|
||||||
endif
|
endif
|
||||||
let port = l[0]
|
let port = l[0]
|
||||||
|
|
||||||
let handle = ch_open('localhost:' . port, 'json')
|
let handle = ch_open('localhost:' . port, 'json')
|
||||||
|
return handle
|
||||||
|
endfunc
|
||||||
|
|
||||||
|
func s:kill_server()
|
||||||
|
if has('win32')
|
||||||
|
call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
|
||||||
|
else
|
||||||
|
call system("pkill --full test_channel.py")
|
||||||
|
endif
|
||||||
|
endfunc
|
||||||
|
|
||||||
|
func Test_communicate()
|
||||||
|
let handle = s:start_server()
|
||||||
|
if handle < 0
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
" Simple string request and reply.
|
" Simple string request and reply.
|
||||||
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
|
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
|
||||||
@@ -73,8 +80,29 @@ func Test_communicate()
|
|||||||
call assert_equal('added1', getline(line('$') - 1))
|
call assert_equal('added1', getline(line('$') - 1))
|
||||||
call assert_equal('added2', getline('$'))
|
call assert_equal('added2', getline('$'))
|
||||||
|
|
||||||
|
" Send an eval request that works.
|
||||||
|
call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
|
||||||
|
call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
|
||||||
|
|
||||||
|
" Send an eval request that fails.
|
||||||
|
call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
|
||||||
|
call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
|
||||||
|
|
||||||
" make the server quit, can't check if this works, should not hang.
|
" make the server quit, can't check if this works, should not hang.
|
||||||
call ch_sendexpr(handle, '!quit!', 0)
|
call ch_sendexpr(handle, '!quit!', 0)
|
||||||
|
|
||||||
call s:kill_server()
|
call s:kill_server()
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
" Test that a server crash is handled gracefully.
|
||||||
|
func Test_server_crash()
|
||||||
|
let handle = s:start_server()
|
||||||
|
if handle < 0
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
call ch_sendexpr(handle, '!crash!')
|
||||||
|
|
||||||
|
" kill the server in case if failed to crash
|
||||||
|
sleep 10m
|
||||||
|
call s:kill_server()
|
||||||
|
endfunc
|
||||||
|
@@ -742,6 +742,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
1249,
|
||||||
/**/
|
/**/
|
||||||
1248,
|
1248,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user