Skip to content

Commit fcb1e3d

Browse files
committed
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().
1 parent f92591f commit fcb1e3d

4 files changed

Lines changed: 76 additions & 19 deletions

File tree

src/channel.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,14 @@ channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
698698
}
699699
else
700700
{
701-
typval_T *tv = eval_expr(arg, NULL);
701+
typval_T *tv;
702702
typval_T err_tv;
703703
char_u *json;
704704

705+
/* Don't pollute the display with errors. */
706+
++emsg_skip;
707+
tv = eval_expr(arg, NULL);
708+
--emsg_skip;
705709
if (is_eval)
706710
{
707711
if (tv == NULL)
@@ -714,7 +718,8 @@ channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3)
714718
channel_send(idx, json, "eval");
715719
vim_free(json);
716720
}
717-
free_tv(tv);
721+
if (tv != &err_tv)
722+
free_tv(tv);
718723
}
719724
}
720725
else if (p_verbose > 2)
@@ -1119,7 +1124,8 @@ channel_read_json_block(int ch_idx, int id, typval_T **rettv)
11191124

11201125
/* Wait for up to 2 seconds.
11211126
* TODO: use timeout set on the channel. */
1122-
if (channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
1127+
if (channels[ch_idx].ch_fd < 0
1128+
|| channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL)
11231129
break;
11241130
channel_read(ch_idx);
11251131
}

src/testdir/test_channel.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def handle(self):
5252
decoded = [-1, '']
5353

5454
# Send a response if the sequence number is positive.
55-
# Negative numbers are used for "eval" responses.
5655
if decoded[0] >= 0:
5756
if decoded[1] == 'hello!':
5857
# simply send back a string
@@ -65,16 +64,38 @@ def handle(self):
6564
print("sending: {}".format(cmd))
6665
thesocket.sendall(cmd.encode('utf-8'))
6766
response = "ok"
67+
elif decoded[1] == 'eval-works':
68+
# Send an eval request. We ignore the response.
69+
cmd = '["eval","\\"foo\\" . 123", -1]'
70+
print("sending: {}".format(cmd))
71+
thesocket.sendall(cmd.encode('utf-8'))
72+
response = "ok"
73+
elif decoded[1] == 'eval-fails':
74+
# Send an eval request that will fail.
75+
cmd = '["eval","xxx", -2]'
76+
print("sending: {}".format(cmd))
77+
thesocket.sendall(cmd.encode('utf-8'))
78+
response = "ok"
79+
elif decoded[1] == 'eval-result':
80+
# Send back the last received eval result.
81+
response = last_eval
6882
elif decoded[1] == '!quit!':
6983
# we're done
7084
sys.exit(0)
85+
elif decoded[1] == '!crash!':
86+
# Crash!
87+
42 / 0
7188
else:
7289
response = "what?"
7390

7491
encoded = json.dumps([decoded[0], response])
7592
print("sending: {}".format(encoded))
7693
thesocket.sendall(encoded.encode('utf-8'))
7794

95+
# Negative numbers are used for "eval" responses.
96+
elif decoded[0] < 0:
97+
last_eval = decoded
98+
7899
thesocket = None
79100

80101
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):

src/testdir/test_channel.vim

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,14 @@ else
1818
endif
1919

2020
func s:start_server()
21+
" The Python program writes the port number in Xportnr.
22+
call delete("Xportnr")
23+
2124
if has('win32')
2225
silent !start cmd /c start "test_channel" py test_channel.py
2326
else
2427
silent !python test_channel.py&
2528
endif
26-
endfunc
27-
28-
func s:kill_server()
29-
if has('win32')
30-
call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
31-
else
32-
call system("pkill --full test_channel.py")
33-
endif
34-
endfunc
35-
36-
func Test_communicate()
37-
call delete("Xportnr")
38-
" The Python program writes the port number in Xportnr.
39-
call s:start_server()
4029

4130
" Wait for up to 2 seconds for the port number to be there.
4231
let cnt = 20
@@ -57,10 +46,28 @@ func Test_communicate()
5746
if len(l) == 0
5847
" Can't make the connection, give up.
5948
call s:kill_server()
60-
return
49+
call assert_false(1, "Can't start test_channel.py")
50+
return -1
6151
endif
6252
let port = l[0]
53+
6354
let handle = ch_open('localhost:' . port, 'json')
55+
return handle
56+
endfunc
57+
58+
func s:kill_server()
59+
if has('win32')
60+
call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
61+
else
62+
call system("pkill --full test_channel.py")
63+
endif
64+
endfunc
65+
66+
func Test_communicate()
67+
let handle = s:start_server()
68+
if handle < 0
69+
return
70+
endif
6471

6572
" Simple string request and reply.
6673
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
@@ -73,8 +80,29 @@ func Test_communicate()
7380
call assert_equal('added1', getline(line('$') - 1))
7481
call assert_equal('added2', getline('$'))
7582

83+
" Send an eval request that works.
84+
call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
85+
call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
86+
87+
" Send an eval request that fails.
88+
call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
89+
call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
90+
7691
" make the server quit, can't check if this works, should not hang.
7792
call ch_sendexpr(handle, '!quit!', 0)
7893

7994
call s:kill_server()
8095
endfunc
96+
97+
" Test that a server crash is handled gracefully.
98+
func Test_server_crash()
99+
let handle = s:start_server()
100+
if handle < 0
101+
return
102+
endif
103+
call ch_sendexpr(handle, '!crash!')
104+
105+
" kill the server in case if failed to crash
106+
sleep 10m
107+
call s:kill_server()
108+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ static char *(features[]) =
742742

743743
static int included_patches[] =
744744
{ /* Add new patch number below this line */
745+
/**/
746+
1249,
745747
/**/
746748
1248,
747749
/**/

0 commit comments

Comments
 (0)