aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/event
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2016-06-26 14:36:24 -0400
committerGitHub <noreply@github.com>2016-06-26 14:36:24 -0400
commite9061117a5b8f195c3f26a5cb94e18ddd7752d86 (patch)
tree047ee719b18c2b80a2a1b2acec2de1c08a0e5aaf /src/nvim/event
parentae9cb1fe07d34289ebe4a929bfca8a07aab85b9f (diff)
parentbfc823f972f82d71848fbf66d247557904f873c5 (diff)
downloadrneovim-e9061117a5b8f195c3f26a5cb94e18ddd7752d86.tar.gz
rneovim-e9061117a5b8f195c3f26a5cb94e18ddd7752d86.tar.bz2
rneovim-e9061117a5b8f195c3f26a5cb94e18ddd7752d86.zip
Merge #4646 from oni-link/fix.issue.4569.3
Fix for missing output (#4569, ...)
Diffstat (limited to 'src/nvim/event')
-rw-r--r--src/nvim/event/process.c63
-rw-r--r--src/nvim/event/rstream.c8
-rw-r--r--src/nvim/event/stream.c1
-rw-r--r--src/nvim/event/stream.h1
4 files changed, 64 insertions, 9 deletions
diff --git a/src/nvim/event/process.c b/src/nvim/event/process.c
index 1fffdbf957..1c4c9737c3 100644
--- a/src/nvim/event/process.c
+++ b/src/nvim/event/process.c
@@ -333,9 +333,61 @@ static void process_close(Process *proc)
}
}
+/// Flush output stream.
+///
+/// @param proc Process, for which an output stream should be flushed.
+/// @param stream Stream to flush.
+static void flush_stream(Process *proc, Stream *stream)
+ FUNC_ATTR_NONNULL_ARG(1)
+{
+ if (!stream || stream->closed) {
+ return;
+ }
+
+ // Maximal remaining data size of terminated process is system
+ // buffer size.
+ // Also helps with a child process that keeps the output streams open. If it
+ // keeps sending data, we only accept as much data as the system buffer size.
+ // Otherwise this would block cleanup/teardown.
+ int system_buffer_size = 0;
+ int err = uv_recv_buffer_size((uv_handle_t *)&stream->uv.pipe,
+ &system_buffer_size);
+ if (err) {
+ system_buffer_size = (int)rbuffer_capacity(stream->buffer);
+ }
+
+ size_t max_bytes = stream->num_bytes + (size_t)system_buffer_size;
+
+ // Read remaining data.
+ while (!stream->closed && stream->num_bytes < max_bytes) {
+ // Remember number of bytes before polling
+ size_t num_bytes = stream->num_bytes;
+
+ // Poll for data and process the generated events.
+ loop_poll_events(proc->loop, 0);
+ if (proc->events) {
+ queue_process_events(proc->events);
+ }
+
+ // Stream can be closed if it is empty.
+ if (num_bytes == stream->num_bytes) {
+ if (stream->read_cb) {
+ // Stream callback could miss EOF handling if a child keeps the stream
+ // open.
+ stream->read_cb(stream, stream->buffer, 0, stream->data, true);
+ }
+ break;
+ }
+ }
+}
+
static void process_close_handles(void **argv)
{
Process *proc = argv[0];
+
+ flush_stream(proc, proc->out);
+ flush_stream(proc, proc->err);
+
process_close_streams(proc);
process_close(proc);
}
@@ -350,11 +402,12 @@ static void on_process_exit(Process *proc)
uv_timer_stop(&loop->children_kill_timer);
}
- // Process handles are closed in the next event loop tick. This is done to
- // give libuv more time to read data from the OS after the process exits(If
- // process_close_streams is called with data still in the OS buffer, we lose
- // it)
- CREATE_EVENT(proc->events, process_close_handles, 1, proc);
+ // Process has terminated, but there could still be data to be read from the
+ // OS. We are still in the libuv loop, so we cannot call code that polls for
+ // more data directly. Instead delay the reading after the libuv loop by
+ // queueing process_close_handles() as an event.
+ Queue *queue = proc->events ? proc->events : loop->events;
+ CREATE_EVENT(queue, process_close_handles, 1, proc);
}
static void on_process_stream_close(Stream *stream, void *data)
diff --git a/src/nvim/event/rstream.c b/src/nvim/event/rstream.c
index 9f3fbc25ff..a520143064 100644
--- a/src/nvim/event/rstream.c
+++ b/src/nvim/event/rstream.c
@@ -100,6 +100,10 @@ static void read_cb(uv_stream_t *uvstream, ssize_t cnt, const uv_buf_t *buf)
{
Stream *stream = uvstream->data;
+ if (cnt > 0) {
+ stream->num_bytes += (size_t)cnt;
+ }
+
if (cnt <= 0) {
if (cnt != UV_ENOBUFS
// cnt == 0 means libuv asked for a buffer and decided it wasn't needed:
@@ -185,10 +189,6 @@ static void read_event(void **argv)
static void invoke_read_cb(Stream *stream, size_t count, bool eof)
{
- if (stream->closed) {
- return;
- }
-
// Don't let the stream be closed before the event is processed.
stream->pending_reqs++;
diff --git a/src/nvim/event/stream.c b/src/nvim/event/stream.c
index 71582ab357..33404158cf 100644
--- a/src/nvim/event/stream.c
+++ b/src/nvim/event/stream.c
@@ -71,6 +71,7 @@ void stream_init(Loop *loop, Stream *stream, int fd, uv_stream_t *uvstream,
stream->closed = false;
stream->buffer = NULL;
stream->events = NULL;
+ stream->num_bytes = 0;
}
void stream_close(Stream *stream, stream_close_cb on_stream_close)
diff --git a/src/nvim/event/stream.h b/src/nvim/event/stream.h
index c6baac0db7..ad4e24775b 100644
--- a/src/nvim/event/stream.h
+++ b/src/nvim/event/stream.h
@@ -49,6 +49,7 @@ struct stream {
size_t curmem;
size_t maxmem;
size_t pending_reqs;
+ size_t num_bytes;
void *data, *internal_data;
bool closed;
Queue *events;