Nginx thread pool is not working in 1.14 in custom module

Maxim Dounin mdounin at mdounin.ru
Wed May 23 14:28:14 UTC 2018


Hello!

On Wed, May 23, 2018 at 09:08:08AM -0400, isolomka wrote:

> Thanks for the response.
> The main issue is that now request is closed before actual task is done in
> thread pool.
> How can I avoid that?
> It worked fine before the upgrade. 
> What is correct thread pool usage in custom module in 1.14?
> 
> Here is my request handler for reference:
> static ngx_int_t ngx_http_thread_handler(ngx_http_request_t* r)
> {
>   //...
>   
>   // Add handler (blocking handler)
>   task->handler = ngx_http_cgpi_task_handler;
>   // Init event
>   task->event.data = taskCtx;
>   task->event.handler = ngx_http_cgpi_task_done_cb;
> 
>   // Try to get the pool to put task
>   ngx_thread_pool_t* tp = clcf->thread_pool;
> 
>   if (tp == NULL)
>   {
>     // Create pool if not exists
>     if (ngx_http_complex_value(r, clcf->thread_pool_value, &name) !=
> NGX_OK)
>     {
>       ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
> "ngx_http_complex_value \"%V\" failed", &name);
>       return NGX_ERROR;
>     }
>     tp = ngx_thread_pool_get((ngx_cycle_t* ) ngx_cycle, &name);
>     if (tp == NULL)
>     {
>       ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "thread pool \"%V\"
> not found", &name);
>       return NGX_ERROR;
>     }
>   }
> 
>   // Put the task into thread pool
>   if (ngx_thread_task_post(tp, task) != NGX_OK)
>   {
>     return NGX_ERROR;
>   }
>   // Make the request blocked
>   r->main->blocked++;
>   r->aio = 1;
> 
>   return NGX_AGAIN;
> }

The code returns control to the caller without incrementing 
r->main->count.  As such, the request is expected to be complete and 
will be closed.  This is incorrect, and will cause various 
problems including in previous versions - e.g., expect a similar 
segmentation fault if a write event happens on the connection.

To fix things you should increment r->main->count and return 
NGX_DONE, and then call ngx_http_finalize_request() when your 
external processing is complete, much like with normal 
non-threaded external processing.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list