981: Fix some bugs related to asynchronous IO r=Learath2 a=heinrich5991

Since `aio_thread` was crashing with #964 on an address that we didn't hand to `io_write`, I hope that this was just a bug with concurrently writing to stdout on Windows, and not a bug in the `aio` stuff.

Fixes #914. Fixes #964.
This commit is contained in:
bors[bot] 2018-01-14 02:20:29 +00:00
commit 861849c924

View file

@ -170,6 +170,15 @@ static void logger_file(const char *line, void *user)
aio_unlock(logfile); aio_unlock(logfile);
} }
#if defined(CONF_FAMILY_WINDOWS)
static void logger_stdout_sync(const char *line, void *user)
{
(void)user;
puts(line);
fflush(stdout);
}
#endif
static void logger_stdout_finish(void *user) static void logger_stdout_finish(void *user)
{ {
ASYNCIO *logfile = (ASYNCIO *)user; ASYNCIO *logfile = (ASYNCIO *)user;
@ -212,7 +221,11 @@ void dbg_logger(DBG_LOGGER logger, DBG_LOGGER_FINISH finish, void *user)
void dbg_logger_stdout() void dbg_logger_stdout()
{ {
#if defined(CONF_FAMILY_WINDOWS)
dbg_logger(logger_stdout_sync, 0, 0);
#else
dbg_logger(logger_file, logger_stdout_finish, aio_new(io_stdout())); dbg_logger(logger_file, logger_stdout_finish, aio_new(io_stdout()));
#endif
} }
void dbg_logger_debugger() void dbg_logger_debugger()
@ -448,6 +461,7 @@ int io_flush(IOHANDLE io)
#define ASYNC_BUFSIZE 8 * 1024 #define ASYNC_BUFSIZE 8 * 1024
#define ASYNC_LOCAL_BUFSIZE 64 * 1024
struct ASYNCIO struct ASYNCIO
{ {
@ -456,8 +470,6 @@ struct ASYNCIO
SEMAPHORE sphore; SEMAPHORE sphore;
void *thread; void *thread;
unsigned char *old_buffer;
unsigned char *buffer; unsigned char *buffer;
unsigned int buffer_size; unsigned int buffer_size;
unsigned int read_pos; unsigned int read_pos;
@ -525,6 +537,8 @@ static void aio_thread(void *user)
{ {
struct BUFFERS buffers; struct BUFFERS buffers;
int result_io_error; int result_io_error;
unsigned char local_buffer[ASYNC_LOCAL_BUFSIZE];
unsigned int local_buffer_len = 0;
if(aio->read_pos == aio->write_pos) if(aio->read_pos == aio->write_pos)
{ {
@ -544,24 +558,33 @@ static void aio_thread(void *user)
} }
buffer_ptrs(aio, &buffers); buffer_ptrs(aio, &buffers);
if(buffers.buf1)
{
if(buffers.len1 > sizeof(local_buffer) - local_buffer_len)
{
buffers.len1 = sizeof(local_buffer) - local_buffer_len;
}
mem_copy(local_buffer + local_buffer_len, buffers.buf1, buffers.len1);
local_buffer_len += buffers.len1;
if(buffers.buf2)
{
if(buffers.len2 > sizeof(local_buffer) - local_buffer_len)
{
buffers.len2 = sizeof(local_buffer) - local_buffer_len;
}
mem_copy(local_buffer + local_buffer_len, buffers.buf2, buffers.len2);
local_buffer_len += buffers.len2;
}
}
aio->read_pos = (aio->read_pos + buffers.len1 + buffers.len2) % aio->buffer_size;
lock_unlock(aio->lock); lock_unlock(aio->lock);
io_write(aio->io, buffers.buf1, buffers.len1); io_write(aio->io, local_buffer, local_buffer_len);
if(buffers.buf2)
{
io_write(aio->io, buffers.buf2, buffers.len2);
}
io_flush(aio->io); io_flush(aio->io);
result_io_error = io_error(aio->io); result_io_error = io_error(aio->io);
lock_wait(aio->lock); lock_wait(aio->lock);
aio->error = result_io_error; aio->error = result_io_error;
aio->read_pos = (aio->read_pos + buffers.len1 + buffers.len2) % aio->buffer_size;
if(aio->old_buffer)
{
mem_free(aio->old_buffer);
aio->old_buffer = 0;
}
} }
} }
@ -577,7 +600,6 @@ ASYNCIO *aio_new(IOHANDLE io)
sphore_init(&aio->sphore); sphore_init(&aio->sphore);
aio->thread = 0; aio->thread = 0;
aio->old_buffer = 0;
aio->buffer = mem_alloc(ASYNC_BUFSIZE, 1); aio->buffer = mem_alloc(ASYNC_BUFSIZE, 1);
if(!aio->buffer) if(!aio->buffer)
{ {
@ -679,14 +701,7 @@ void aio_write_unlocked(ASYNCIO *aio, const void *buffer, unsigned size)
mem_copy(next_buffer + next_len, buffer, size); mem_copy(next_buffer + next_len, buffer, size);
next_len += size; next_len += size;
if(!aio->old_buffer) mem_free(aio->buffer);
{
aio->old_buffer = aio->buffer;
}
else
{
mem_free(aio->buffer);
}
aio->buffer = next_buffer; aio->buffer = next_buffer;
aio->buffer_size = next_size; aio->buffer_size = next_size;
aio->read_pos = 0; aio->read_pos = 0;