Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3873,7 +3873,11 @@ This directive controls whether to check for premature client connection abortio

When this directive is on, the ngx_lua module will monitor the premature connection close event on the downstream connections and when there is such an event, it will call the user Lua function callback (registered by [ngx.on_abort](#ngxon_abort)) or just stop and clean up all the Lua "light threads" running in the current request's request handler when there is no user callback function registered.

According to the current implementation, however, if the client closes the connection before the Lua code finishes reading the request body data via [ngx.req.socket](#ngxreqsocket), then ngx_lua will neither stop all the running "light threads" nor call the user callback (if [ngx.on_abort](#ngxon_abort) has been called). Instead, the reading operation on [ngx.req.socket](#ngxreqsocket) will just return the error message "client aborted" as the second return value (the first return value is surely `nil`).
If the client disconnects during a pending [ngx.req.socket](#ngxreqsocket) body read, the read returns `nil` with the error `"client aborted"`. The other light threads continue running.

If Lua cancels the pending body read first, client-abort monitoring resumes. A later disconnect invokes the registered callback or, if none was registered, stops the request's light threads. This also applies when additional body bytes arrive between cancellation and disconnect.

For pipelined HTTPS requests, the monitor can detect a TLS `close_notify` even when the next request exactly fills the header buffer. If more application data follows, it stays available for the next request. A TLS close notification behind those unread bytes becomes visible when nginx reads them.

When TCP keepalive is disabled, it is relying on the client side to close the socket gracefully (by sending a `FIN` packet or something like that). For (soft) real-time web applications, it is highly recommended to configure the [TCP keepalive](http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html) support in your system's TCP stack implementation in order to detect "half-open" TCP connections in time.

Expand Down
14 changes: 14 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,20 @@ ngx_feature_test='setsockopt(1, SOL_SOCKET, SO_PASSCRED, NULL, 0);'

# ----------------------------------------

if [ "$NGX_PLATFORM" != win32 ]; then
ngx_feature="poll() for downstream socket hangups"
ngx_feature_name=NGX_HTTP_LUA_HAVE_POLL
ngx_feature_run=no
ngx_feature_incs="#include <poll.h>"
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test='struct pollfd pfd = { -1, 0, 0 };
(void) poll(&pfd, 1, 0);'
. auto/feature
fi

# ----------------------------------------

ngx_feature="SA_RESTART"
ngx_feature_libs=
ngx_feature_name="NGX_HTTP_LUA_HAVE_SA_RESTART"
Expand Down
28 changes: 2 additions & 26 deletions src/ngx_http_lua_accessby.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,10 @@ ngx_http_lua_access_by_chunk(lua_State *L, ngx_http_request_t *r)
ngx_int_t rc;
ngx_uint_t nreqs;
lua_State *co;
ngx_event_t *rev;
ngx_connection_t *c;
ngx_http_lua_ctx_t *ctx;
ngx_pool_cleanup_t *cln;

ngx_http_lua_loc_conf_t *llcf;

/* {{{ new coroutine to handle request */
co = ngx_http_lua_new_thread(r, L, &co_ref);

Expand Down Expand Up @@ -317,29 +314,8 @@ ngx_http_lua_access_by_chunk(lua_State *L, ngx_http_request_t *r)

ctx->context = NGX_HTTP_LUA_CONTEXT_ACCESS;

llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

if (llcf->check_client_abort) {
r->read_event_handler = ngx_http_lua_rd_check_broken_connection;

#if (NGX_HTTP_V2)
if (!r->stream) {
#endif

rev = r->connection->read;

if (!rev->active) {
if (ngx_add_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
return NGX_ERROR;
}
}

#if (NGX_HTTP_V2)
}
#endif

} else {
r->read_event_handler = ngx_http_block_reading;
if (ngx_http_lua_set_read_handler(r) != NGX_OK) {
return NGX_ERROR;
}

c = r->connection;
Expand Down
28 changes: 2 additions & 26 deletions src/ngx_http_lua_contentby.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ ngx_http_lua_content_by_chunk(lua_State *L, ngx_http_request_t *r)
int co_ref;
ngx_int_t rc;
lua_State *co;
ngx_event_t *rev;
ngx_http_lua_ctx_t *ctx;
ngx_pool_cleanup_t *cln;

ngx_http_lua_loc_conf_t *llcf;

dd("content by chunk");

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
Expand Down Expand Up @@ -96,29 +93,8 @@ ngx_http_lua_content_by_chunk(lua_State *L, ngx_http_request_t *r)

ctx->context = NGX_HTTP_LUA_CONTEXT_CONTENT;

llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

if (llcf->check_client_abort) {
r->read_event_handler = ngx_http_lua_rd_check_broken_connection;

#if (NGX_HTTP_V2)
if (!r->stream) {
#endif

rev = r->connection->read;

if (!rev->active) {
if (ngx_add_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
return NGX_ERROR;
}
}

#if (NGX_HTTP_V2)
}
#endif

} else {
r->read_event_handler = ngx_http_block_reading;
if (ngx_http_lua_set_read_handler(r) != NGX_OK) {
return NGX_ERROR;
}

rc = ngx_http_lua_run_thread(L, r, ctx, 0);
Expand Down
28 changes: 2 additions & 26 deletions src/ngx_http_lua_precontentby.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,10 @@ ngx_http_lua_precontent_by_chunk(lua_State *L, ngx_http_request_t *r)
ngx_int_t rc;
ngx_uint_t nreqs;
lua_State *co;
ngx_event_t *rev;
ngx_connection_t *c;
ngx_http_lua_ctx_t *ctx;
ngx_pool_cleanup_t *cln;

ngx_http_lua_loc_conf_t *llcf;

/* {{{ new coroutine to handle request */
co = ngx_http_lua_new_thread(r, L, &co_ref);

Expand Down Expand Up @@ -309,29 +306,8 @@ ngx_http_lua_precontent_by_chunk(lua_State *L, ngx_http_request_t *r)

ctx->context = NGX_HTTP_LUA_CONTEXT_PRECONTENT;

llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

if (llcf->check_client_abort) {
r->read_event_handler = ngx_http_lua_rd_check_broken_connection;

#if (NGX_HTTP_V2)
if (!r->stream) {
#endif

rev = r->connection->read;

if (!rev->active) {
if (ngx_add_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
return NGX_ERROR;
}
}

#if (NGX_HTTP_V2)
}
#endif

} else {
r->read_event_handler = ngx_http_block_reading;
if (ngx_http_lua_set_read_handler(r) != NGX_OK) {
return NGX_ERROR;
}

c = r->connection;
Expand Down
28 changes: 2 additions & 26 deletions src/ngx_http_lua_rewriteby.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,10 @@ ngx_http_lua_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
lua_State *co;
ngx_int_t rc;
ngx_uint_t nreqs;
ngx_event_t *rev;
ngx_connection_t *c;
ngx_http_lua_ctx_t *ctx;
ngx_pool_cleanup_t *cln;

ngx_http_lua_loc_conf_t *llcf;

/* {{{ new coroutine to handle request */
co = ngx_http_lua_new_thread(r, L, &co_ref);

Expand Down Expand Up @@ -316,29 +313,8 @@ ngx_http_lua_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)

ctx->context = NGX_HTTP_LUA_CONTEXT_REWRITE;

llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

if (llcf->check_client_abort) {
r->read_event_handler = ngx_http_lua_rd_check_broken_connection;

#if (NGX_HTTP_V2)
if (!r->stream) {
#endif

rev = r->connection->read;

if (!rev->active) {
if (ngx_add_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
return NGX_ERROR;
}
}

#if (NGX_HTTP_V2)
}
#endif

} else {
r->read_event_handler = ngx_http_block_reading;
if (ngx_http_lua_set_read_handler(r) != NGX_OK) {
return NGX_ERROR;
}

c = r->connection;
Expand Down
28 changes: 2 additions & 26 deletions src/ngx_http_lua_server_rewriteby.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,10 @@ ngx_http_lua_server_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
lua_State *co;
ngx_int_t rc;
ngx_uint_t nreqs;
ngx_event_t *rev;
ngx_connection_t *c;
ngx_http_lua_ctx_t *ctx;
ngx_pool_cleanup_t *cln;

ngx_http_lua_loc_conf_t *llcf;

/* {{{ new coroutine to handle request */
co = ngx_http_lua_new_thread(r, L, &co_ref);

Expand Down Expand Up @@ -282,29 +279,8 @@ ngx_http_lua_server_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)

ctx->context = NGX_HTTP_LUA_CONTEXT_SERVER_REWRITE;

llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

if (llcf->check_client_abort) {
r->read_event_handler = ngx_http_lua_rd_check_broken_connection;

#if (NGX_HTTP_V2)
if (!r->stream) {
#endif

rev = r->connection->read;

if (!rev->active) {
if (ngx_add_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
return NGX_ERROR;
}
}

#if (NGX_HTTP_V2)
}
#endif

} else {
r->read_event_handler = ngx_http_block_reading;
if (ngx_http_lua_set_read_handler(r) != NGX_OK) {
return NGX_ERROR;
}

c = r->connection;
Expand Down
Loading
Loading