Skip to content

Commit

Permalink
Truncation detection in sendfile() on Linux.
Browse files Browse the repository at this point in the history
This addresses connection hangs as observed in ticket #504, and
CPU hogs with "aio threads; sendfile on" as reported in the mailing list,
see http://mailman.nginx.org/pipermail/nginx-ru/2016-March/057638.html.

The alert is identical to one used on FreeBSD.
  • Loading branch information
mdounin committed Mar 15, 2016
1 parent 2ce791f commit 681e735
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/os/unix/ngx_linux_sendfile_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ ngx_linux_sendfile(ngx_connection_t *c, ngx_buf_t *file, size_t size)
}
}

if (n == 0) {
/*
* if sendfile returns zero, then someone has truncated the file,
* so the offset became beyond the end of the file
*/

ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"sendfile() reported that \"%s\" was truncated at %O",
file->file->name.data, file->file_pos);

return NGX_ERROR;
}

ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, "sendfile: %z of %uz @%O",
n, size, file->file_pos);

Expand Down Expand Up @@ -354,6 +367,19 @@ ngx_linux_sendfile_thread(ngx_connection_t *c, ngx_buf_t *file, size_t size,
return NGX_ERROR;
}

if (ctx->sent == 0) {
/*
* if sendfile returns zero, then someone has truncated the file,
* so the offset became beyond the end of the file
*/

ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"sendfile() reported that \"%s\" was truncated at %O",
file->file->name.data, file->file_pos);

return NGX_ERROR;
}

*sent = ctx->sent;

return (ctx->sent == ctx->size) ? NGX_DONE : NGX_AGAIN;
Expand Down

0 comments on commit 681e735

Please sign in to comment.