Merge #5463 from justinmk/te-skip-writes

term_write(): Skip writes if stream was closed.
This commit is contained in:
Justin M. Keyes 2016-10-12 08:45:30 +02:00 committed by GitHub
commit 0190b9fb92
3 changed files with 34 additions and 10 deletions

View File

@ -405,7 +405,7 @@ typedef struct {
LibuvProcess uv; LibuvProcess uv;
PtyProcess pty; PtyProcess pty;
} proc; } proc;
Stream in, out, err; Stream in, out, err; // Initialized in common_job_start().
Terminal *term; Terminal *term;
bool stopped; bool stopped;
bool exited; bool exited;
@ -21739,7 +21739,7 @@ static inline TerminalJobData *common_job_init(char **argv,
if (!pty) { if (!pty) {
proc->err = &data->err; proc->err = &data->err;
} }
proc->cb = on_process_exit; proc->cb = eval_job_process_exit_cb;
proc->events = data->events; proc->events = data->events;
proc->detach = detach; proc->detach = detach;
proc->cwd = cwd; proc->cwd = cwd;
@ -21923,7 +21923,7 @@ static void on_job_output(Stream *stream, TerminalJobData *data, RBuffer *buf,
rbuffer_consumed(buf, count); rbuffer_consumed(buf, count);
} }
static void on_process_exit(Process *proc, int status, void *d) static void eval_job_process_exit_cb(Process *proc, int status, void *d)
{ {
TerminalJobData *data = d; TerminalJobData *data = d;
if (data->term && !data->exited) { if (data->term && !data->exited) {
@ -21947,9 +21947,15 @@ static void on_process_exit(Process *proc, int status, void *d)
static void term_write(char *buf, size_t size, void *d) static void term_write(char *buf, size_t size, void *d)
{ {
TerminalJobData *data = d; TerminalJobData *job = d;
if (job->in.closed) {
// If the backing stream was closed abruptly, there may be write events
// ahead of the terminal close event. Just ignore the writes.
ILOG("write failed: stream is closed");
return;
}
WBuffer *wbuf = wstream_new_buffer(xmemdup(buf, size), size, 1, xfree); WBuffer *wbuf = wstream_new_buffer(xmemdup(buf, size), size, 1, xfree);
wstream_write(&data->in, wbuf); wstream_write(&job->in, wbuf);
} }
static void term_resize(uint16_t width, uint16_t height, void *d) static void term_resize(uint16_t width, uint16_t height, void *d)

View File

@ -366,10 +366,10 @@ void terminal_resize(Terminal *term, uint16_t width, uint16_t height)
void terminal_enter(void) void terminal_enter(void)
{ {
buf_T *buf = curbuf; buf_T *buf = curbuf;
assert(buf->terminal); // Should only be called when curbuf has a terminal.
TerminalState state, *s = &state; TerminalState state, *s = &state;
memset(s, 0, sizeof(TerminalState)); memset(s, 0, sizeof(TerminalState));
s->term = buf->terminal; s->term = buf->terminal;
assert(s->term && "should only be called when curbuf has a terminal");
// Ensure the terminal is properly sized. // Ensure the terminal is properly sized.
terminal_resize(s->term, 0, 0); terminal_resize(s->term, 0, 0);

View File

@ -13,13 +13,19 @@ describe(':terminal', function()
clear() clear()
screen = Screen.new(50, 4) screen = Screen.new(50, 4)
screen:attach({rgb=false}) screen:attach({rgb=false})
-- shell-test.c is a fake shell that prints its arguments and exits.
nvim('set_option', 'shell', nvim_dir..'/shell-test') nvim('set_option', 'shell', nvim_dir..'/shell-test')
nvim('set_option', 'shellcmdflag', 'EXE') nvim('set_option', 'shellcmdflag', 'EXE')
end) end)
-- Invokes `:terminal {cmd}` using a fake shell (shell-test.c) which prints
-- the {cmd} and exits immediately .
local function terminal_run_fake_shell_cmd(cmd)
execute("terminal "..(cmd and cmd or ""))
end
it('with no argument, acts like termopen()', function() it('with no argument, acts like termopen()', function()
execute('terminal') terminal_run_fake_shell_cmd()
wait() wait()
screen:expect([[ screen:expect([[
ready $ | ready $ |
@ -30,7 +36,7 @@ describe(':terminal', function()
end) end)
it('executes a given command through the shell', function() it('executes a given command through the shell', function()
execute('terminal echo hi') terminal_run_fake_shell_cmd('echo hi')
wait() wait()
screen:expect([[ screen:expect([[
ready $ echo hi | ready $ echo hi |
@ -41,7 +47,7 @@ describe(':terminal', function()
end) end)
it('allows quotes and slashes', function() it('allows quotes and slashes', function()
execute([[terminal echo 'hello' \ "world"]]) terminal_run_fake_shell_cmd([[echo 'hello' \ "world"]])
wait() wait()
screen:expect([[ screen:expect([[
ready $ echo 'hello' \ "world" | ready $ echo 'hello' \ "world" |
@ -58,4 +64,16 @@ describe(':terminal', function()
-- Verify that BufNew actually fired (else the test is invalid). -- Verify that BufNew actually fired (else the test is invalid).
eq('foo', eval('&shell')) eq('foo', eval('&shell'))
end) end)
it('ignores writes if the backing stream closes', function()
terminal_run_fake_shell_cmd()
helpers.feed('iiXXXXXXX')
wait()
-- Race: Though the shell exited (and streams were closed by SIGCHLD
-- handler), :terminal cleanup is pending on the main-loop.
-- This write should be ignored (not crash, #5445).
helpers.feed('iiYYYYYYY')
wait()
end)
end) end)