mirror of
https://github.com/neovim/neovim.git
synced 2025-10-17 23:31:51 +00:00

f_jobstop()/f_rpcstop() .. process_stop() .. process_close_in(proc) closes the write-stream of a RPC channel. But there might be a pending RPC notification on the queue, which may get processed just before the channel is closed. To handle that case, check the Stream.closed in channel.c:receive_msgpack(). Before this change, the above scenario could trigger this assert(!stream->closed) in wstream_write(): 0x00007f96e1cd3428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54 0x00007f96e1cd502a in __GI_abort () at abort.c:89 0x00007f96e1ccbbd7 in __assert_fail_base (fmt=<optimized out>, assertion=assertion@entry=0x768f9b "!stream->closed", file=file@entry=0x768f70 "../src/nvim/event/wstream.c", line=line@entry=77, function=function@entry=0x768fb0 <__PRETTY_FUNCTION__.13735> "wstream_write") at assert.c:92 0x00007f96e1ccbc82 in __GI___assert_fail (assertion=0x768f9b "!stream->closed", file=0x768f70 "../src/nvim/event/wstream.c", line=77, function=0x768fb0 <__PRETTY_FUNCTION__.13735> "wstream_write") at assert.c:101 0x00000000004d2c1f in wstream_write (stream=0x7f96e0a35078, buffer=0x7f96e09f9b40) at ../src/nvim/event/wstream.c:77 0x00000000005857b2 in channel_write (channel=0x7f96e0ae5800, buffer=0x7f96e09f9b40) at ../src/nvim/msgpack_rpc/channel.c:551 0x000000000058567d in on_request_event (argv=0x7ffed792efa0) at ../src/nvim/msgpack_rpc/channel.c:523 0x00000000005854c8 in handle_request (channel=0x7f96e0ae5800, request=0x7ffed792f1b8) at ../src/nvim/msgpack_rpc/channel.c:503 0x00000000005850cb in parse_msgpack (channel=0x7f96e0ae5800) at ../src/nvim/msgpack_rpc/channel.c:423 0x0000000000584f90 in receive_msgpack (stream=0x7f96e0a35218, rbuf=0x7f96e0d1d4c0, c=22, data=0x7f96e0ae5800, eof=false) at ../src/nvim/msgpack_rpc/channel.c:389 0x00000000004d0b20 in read_event (argv=0x7ffed792f4a8) at ../src/nvim/event/rstream.c:190 0x00000000004ce462 in multiqueue_process_events (this=0x7f96e18172d0) at ../src/nvim/event/multiqueue.c:150 0x000000000059b630 in nv_event (cap=0x7ffed792f620) at ../src/nvim/normal.c:7908 0x000000000058be69 in normal_execute (state=0x7ffed792f580, key=-25341) at ../src/nvim/normal.c:1137 0x0000000000652463 in state_enter (s=0x7ffed792f580) at ../src/nvim/state.c:61 0x000000000058a1fe in normal_enter (cmdwin=false, noexmode=false) at ../src/nvim/normal.c:467 0x00000000005500c2 in main (argc=2, argv=0x7ffed792f8d8) at ../src/nvim/main.c:554 Alternative approach suggested by bfredl is to use close_cb of the process. My unsuccessful attempt is below. (It seems close_cb is queued too late, which is the similar problem addressed by this commit): commit 75fc12c6ab15711bdb7b18c6d42ec9d157f5145e Author: Justin M. Keyes <justinkz@gmail.com> Date: Fri Aug 18 01:30:41 2017 +0200 rpc: use Stream's close_cb instead of explicit check in receive_msgpack() diff --git a/src/nvim/event/process.c b/src/nvim/event/process.c index 8371d3cd482e..e52da23cdc40 100644 --- a/src/nvim/event/process.c +++ b/src/nvim/event/process.c @@ -416,6 +416,10 @@ static void on_process_exit(Process *proc) static void on_process_stream_close(Stream *stream, void *data) { Process *proc = data; + ILOG("on_process_stream_close"); + if (proc->stream_close_cb != NULL) { + proc->stream_close_cb(stream, proc->stream_close_data); + } decref(proc); } diff --git a/src/nvim/event/process.h b/src/nvim/event/process.h index 5c00e8e7ecd5..34a8d54f6f8c 100644 --- a/src/nvim/event/process.h +++ b/src/nvim/event/process.h @@ -26,6 +26,11 @@ struct process { Stream *in, *out, *err; process_exit_cb cb; internal_process_cb internal_exit_cb, internal_close_cb; + + // Called when any of the process streams (in/out/err) closes. + stream_close_cb stream_close_cb; + void *stream_close_data; + bool closed, detach; MultiQueue *events; }; @@ -50,6 +55,8 @@ static inline Process process_init(Loop *loop, ProcessType type, void *data) .closed = false, .internal_close_cb = NULL, .internal_exit_cb = NULL, + .stream_close_cb = NULL, + .stream_close_data = NULL, .detach = false }; } diff --git a/src/nvim/event/stream.c b/src/nvim/event/stream.c index 7c865bfe1e8c..c8720d1e45d9 100644 --- a/src/nvim/event/stream.c +++ b/src/nvim/event/stream.c @@ -95,7 +95,11 @@ void stream_close(Stream *stream, stream_close_cb on_stream_close, void *data) void stream_close_handle(Stream *stream) FUNC_ATTR_NONNULL_ALL { + ILOG("stream=%d", stream); + // LOG_CALLSTACK(); if (stream->uvstream) { + // problem: this schedules on the queue, but channel.c:receive_msgpack may + // be processed before close_cb is called by libuv. uv_close((uv_handle_t *)stream->uvstream, close_cb); } else { uv_close((uv_handle_t *)&stream->uv.idle, close_cb); @@ -105,6 +109,7 @@ void stream_close_handle(Stream *stream) static void close_cb(uv_handle_t *handle) { Stream *stream = handle->data; + ILOG(">>>>>>>>>>>>>>>>>>>>>>> stream=%p stream->internal_close_cb=%p", stream, stream->internal_close_cb); if (stream->buffer) { rbuffer_free(stream->buffer); } diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c index 782eabe04e4a..dc2b794e366a 100644 --- a/src/nvim/msgpack_rpc/channel.c +++ b/src/nvim/msgpack_rpc/channel.c @@ -128,6 +128,8 @@ uint64_t channel_from_process(Process *proc, uint64_t id, char *source) source); incref(channel); // process channels are only closed by the exit_cb channel->data.proc = proc; + channel->data.proc->stream_close_cb = close_cb2; + channel->data.proc->stream_close_data = channel; wstream_init(proc->in, 0); rstream_init(proc->out, 0); @@ -387,17 +389,6 @@ static void receive_msgpack(Stream *stream, RBuffer *rbuf, size_t c, goto end; } - if ((chan_wstream(channel) != NULL && chan_wstream(channel)->closed) - || (chan_rstream(channel) != NULL && chan_rstream(channel)->closed)) { - char buf[256]; - snprintf(buf, sizeof(buf), - "ch %" PRIu64 ": stream closed unexpectedly. " - "closing channel", - channel->id); - call_set_error(channel, buf, WARN_LOG_LEVEL); - goto end; - } - size_t count = rbuffer_size(rbuf); DLOG("ch %" PRIu64 ": parsing %u bytes from msgpack Stream: %p", channel->id, count, stream); @@ -571,23 +562,6 @@ static Stream *chan_wstream(Channel *chan) abort(); } -/// Returns the Stream that a Channel reads from. -static Stream *chan_rstream(Channel *chan) -{ - switch (chan->type) { - case kChannelTypeSocket: - return &chan->data.stream; - case kChannelTypeProc: - return chan->data.proc->out; - case kChannelTypeStdio: - return &chan->data.std.in; - case kChannelTypeInternal: - return NULL; - } - abort(); -} - - static bool channel_write(Channel *channel, WBuffer *buffer) { bool success = false; @@ -799,6 +773,12 @@ static void close_cb(Stream *stream, void *data) decref(data); } +static void close_cb2(Stream *stream, void *data) +{ + ILOG("close_cb2"); + close_channel(data); +} + /// @param source description of source function, rplugin name, TCP addr, etc static Channel *register_channel(ChannelType type, uint64_t id, MultiQueue *events, char *source)
288 lines
7.2 KiB
C
288 lines
7.2 KiB
C
// This is an open source non-commercial project. Dear PVS-Studio, please check
|
|
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
#include <assert.h>
|
|
#include <inttypes.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <uv.h>
|
|
|
|
#include "nvim/log.h"
|
|
#include "nvim/types.h"
|
|
#include "nvim/os/os.h"
|
|
#include "nvim/os/time.h"
|
|
|
|
#define LOG_FILE_ENV "NVIM_LOG_FILE"
|
|
|
|
/// Cached location of the expanded log file path decided by log_path_init().
|
|
static char log_file_path[MAXPATHL + 1] = { 0 };
|
|
|
|
static uv_mutex_t mutex;
|
|
|
|
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
|
# include "log.c.generated.h"
|
|
#endif
|
|
|
|
#ifdef HAVE_EXECINFO_BACKTRACE
|
|
# include <execinfo.h>
|
|
#endif
|
|
|
|
static bool log_try_create(char *fname)
|
|
{
|
|
if (fname == NULL || fname[0] == '\0') {
|
|
return false;
|
|
}
|
|
FILE *log_file = fopen(fname, "a");
|
|
if (log_file == NULL) {
|
|
return false;
|
|
}
|
|
fclose(log_file);
|
|
return true;
|
|
}
|
|
|
|
/// Initializes path to log file. Sets $NVIM_LOG_FILE if empty.
|
|
///
|
|
/// Tries $NVIM_LOG_FILE, or falls back to $XDG_DATA_HOME/nvim/log. Path to log
|
|
/// file is cached, so only the first call has effect, unless first call was not
|
|
/// successful. Failed initialization indicates either a bug in expand_env()
|
|
/// or both $NVIM_LOG_FILE and $HOME environment variables are undefined.
|
|
///
|
|
/// @return true if path was initialized, false otherwise.
|
|
static bool log_path_init(void)
|
|
{
|
|
if (log_file_path[0]) {
|
|
return true;
|
|
}
|
|
size_t size = sizeof(log_file_path);
|
|
expand_env((char_u *)"$" LOG_FILE_ENV, (char_u *)log_file_path,
|
|
(int)size - 1);
|
|
if (strequal("$" LOG_FILE_ENV, log_file_path)
|
|
|| log_file_path[0] == '\0'
|
|
|| os_isdir((char_u *)log_file_path)
|
|
|| !log_try_create(log_file_path)) {
|
|
// Invalid $NVIM_LOG_FILE or failed to expand; fall back to default.
|
|
char *defaultpath = stdpaths_user_data_subpath("log", 0, true);
|
|
size_t len = xstrlcpy(log_file_path, defaultpath, size);
|
|
xfree(defaultpath);
|
|
// Fall back to .nvimlog
|
|
if (len >= size || !log_try_create(log_file_path)) {
|
|
len = xstrlcpy(log_file_path, ".nvimlog", size);
|
|
}
|
|
// Fall back to stderr
|
|
if (len >= size || !log_try_create(log_file_path)) {
|
|
log_file_path[0] = '\0';
|
|
return false;
|
|
}
|
|
os_setenv(LOG_FILE_ENV, log_file_path, true);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void log_init(void)
|
|
{
|
|
uv_mutex_init(&mutex);
|
|
}
|
|
|
|
void log_lock(void)
|
|
{
|
|
uv_mutex_lock(&mutex);
|
|
}
|
|
|
|
void log_unlock(void)
|
|
{
|
|
uv_mutex_unlock(&mutex);
|
|
}
|
|
|
|
bool do_log(int log_level, const char *func_name, int line_num, bool eol,
|
|
const char* fmt, ...) FUNC_ATTR_UNUSED
|
|
{
|
|
if (log_level < MIN_LOG_LEVEL) {
|
|
return false;
|
|
}
|
|
|
|
log_lock();
|
|
bool ret = false;
|
|
FILE *log_file = open_log_file();
|
|
|
|
if (log_file == NULL) {
|
|
goto end;
|
|
}
|
|
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
ret = v_do_log_to_file(log_file, log_level, func_name, line_num, eol,
|
|
fmt, args);
|
|
va_end(args);
|
|
|
|
if (log_file != stderr && log_file != stdout) {
|
|
fclose(log_file);
|
|
}
|
|
end:
|
|
log_unlock();
|
|
return ret;
|
|
}
|
|
|
|
void log_uv_handles(void *loop)
|
|
{
|
|
uv_loop_t *l = loop;
|
|
log_lock();
|
|
FILE *log_file = open_log_file();
|
|
|
|
if (log_file == NULL) {
|
|
goto end;
|
|
}
|
|
|
|
uv_print_all_handles(l, log_file);
|
|
|
|
if (log_file != stderr && log_file != stdout) {
|
|
fclose(log_file);
|
|
}
|
|
end:
|
|
log_unlock();
|
|
}
|
|
|
|
/// Open the log file for appending.
|
|
///
|
|
/// @return FILE* decided by log_path_init() or stderr in case of error
|
|
FILE *open_log_file(void)
|
|
{
|
|
static bool opening_log_file = false;
|
|
// check if it's a recursive call
|
|
if (opening_log_file) {
|
|
do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true,
|
|
"Cannot LOG() recursively.");
|
|
return stderr;
|
|
}
|
|
|
|
FILE *log_file = NULL;
|
|
opening_log_file = true;
|
|
if (log_path_init()) {
|
|
log_file = fopen(log_file_path, "a");
|
|
}
|
|
opening_log_file = false;
|
|
|
|
if (log_file != NULL) {
|
|
return log_file;
|
|
}
|
|
|
|
// May happen if:
|
|
// - LOG() is called before early_init()
|
|
// - Directory does not exist
|
|
// - File is not writable
|
|
do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true,
|
|
"Logging to stderr, failed to open $" LOG_FILE_ENV ": %s",
|
|
log_file_path);
|
|
return stderr;
|
|
}
|
|
|
|
#ifdef HAVE_EXECINFO_BACKTRACE
|
|
void log_callstack_to_file(FILE *log_file, const char *const func_name,
|
|
const int line_num)
|
|
{
|
|
void *trace[100];
|
|
int trace_size = backtrace(trace, ARRAY_SIZE(trace));
|
|
|
|
char exepath[MAXPATHL] = { 0 };
|
|
size_t exepathlen = MAXPATHL;
|
|
if (os_exepath(exepath, &exepathlen) != 0) {
|
|
abort();
|
|
}
|
|
assert(24 + exepathlen < IOSIZE); // Must fit in `cmdbuf` below.
|
|
|
|
char cmdbuf[IOSIZE + (20 * ARRAY_SIZE(trace))];
|
|
snprintf(cmdbuf, sizeof(cmdbuf), "addr2line -e %s -f -p", exepath);
|
|
for (int i = 1; i < trace_size; i++) {
|
|
char buf[20]; // 64-bit pointer 0xNNNNNNNNNNNNNNNN with leading space.
|
|
snprintf(buf, sizeof(buf), " %p", trace[i]);
|
|
xstrlcat(cmdbuf, buf, sizeof(cmdbuf));
|
|
}
|
|
// Now we have a command string like:
|
|
// addr2line -e /path/to/exe -f -p 0x123 0x456 ...
|
|
|
|
do_log_to_file(log_file, DEBUG_LOG_LEVEL, func_name, line_num, true,
|
|
"trace:");
|
|
FILE *fp = popen(cmdbuf, "r");
|
|
char linebuf[IOSIZE];
|
|
while (fgets(linebuf, sizeof(linebuf) - 1, fp) != NULL) {
|
|
fprintf(log_file, " %s", linebuf);
|
|
}
|
|
pclose(fp);
|
|
|
|
if (log_file != stderr && log_file != stdout) {
|
|
fclose(log_file);
|
|
}
|
|
}
|
|
|
|
void log_callstack(const char *const func_name, const int line_num)
|
|
{
|
|
log_lock();
|
|
FILE *log_file = open_log_file();
|
|
if (log_file == NULL) {
|
|
goto end;
|
|
}
|
|
|
|
log_callstack_to_file(log_file, func_name, line_num);
|
|
|
|
end:
|
|
log_unlock();
|
|
}
|
|
#endif
|
|
|
|
static bool do_log_to_file(FILE *log_file, int log_level,
|
|
const char *func_name, int line_num, bool eol,
|
|
const char* fmt, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
bool ret = v_do_log_to_file(log_file, log_level, func_name, line_num, eol,
|
|
fmt, args);
|
|
va_end(args);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static bool v_do_log_to_file(FILE *log_file, int log_level,
|
|
const char *func_name, int line_num, bool eol,
|
|
const char* fmt, va_list args)
|
|
{
|
|
static const char *log_levels[] = {
|
|
[DEBUG_LOG_LEVEL] = "DEBUG",
|
|
[INFO_LOG_LEVEL] = "INFO ",
|
|
[WARN_LOG_LEVEL] = "WARN ",
|
|
[ERROR_LOG_LEVEL] = "ERROR",
|
|
};
|
|
assert(log_level >= DEBUG_LOG_LEVEL && log_level <= ERROR_LOG_LEVEL);
|
|
|
|
// format current timestamp in local time
|
|
struct tm local_time;
|
|
if (os_get_localtime(&local_time) == NULL) {
|
|
return false;
|
|
}
|
|
char date_time[20];
|
|
if (strftime(date_time, sizeof(date_time), "%Y/%m/%d %H:%M:%S",
|
|
&local_time) == 0) {
|
|
return false;
|
|
}
|
|
|
|
// print the log message prefixed by the current timestamp and pid
|
|
int64_t pid = os_get_pid();
|
|
if (fprintf(log_file, "%s %s %" PRId64 "/%s:%d: ", date_time,
|
|
log_levels[log_level], pid, func_name, line_num) < 0) {
|
|
return false;
|
|
}
|
|
if (vfprintf(log_file, fmt, args) < 0) {
|
|
return false;
|
|
}
|
|
if (eol) {
|
|
fputc('\n', log_file);
|
|
}
|
|
if (fflush(log_file) == EOF) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|