Skip to content

Commit

Permalink
Block SIGPIPE on background thread. Fixes #168
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Aug 14, 2018
1 parent f93d152 commit f554469
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/httpuv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ void stop_io_loop(uv_async_t *handle) {
uv_stop(io_loop.get());
}

// Blocks SIGPIPE on the current thread.
void block_sigpipe() {
sigset_t set;
int result;
sigemptyset(&set);
sigaddset(&set, SIGPIPE);
result = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (result) {
err_printf("Error blocking SIGPIPE on httpuv background thread.\n");
}
}

void io_thread(void* data) {
register_background_thread();
Barrier* blocker = reinterpret_cast<Barrier*>(data);
Expand All @@ -110,6 +122,11 @@ void io_thread(void* data) {
// Tell other thread that it can continue.
blocker->wait();

// Must ignore SIGPIPE for libuv code; otherwise unexpectedly closed
// connections kill us. https://github.com/trestletech/plumber/issues/289
#ifndef _WIN32
block_sigpipe();
#endif
// Run io_loop. When it stops, this fuction continues and the thread exits.
uv_run(io_loop.get(), UV_RUN_DEFAULT);

Expand Down

0 comments on commit f554469

Please sign in to comment.