Sending a notification to the main nginx thread

Maxim Dounin mdounin at
Thu Oct 7 13:07:12 UTC 2021


On Wed, Oct 06, 2021 at 07:53:00PM +0000, Eran Kornblau wrote:

> > 
> > -----Original Message-----
> > From: nginx-devel <nginx-devel-bounces at> On Behalf Of Maxim Dounin
> > Sent: Wednesday, 6 October 2021 16:12
> > To: nginx-devel at
> > Subject: Re: Sending a notification to the main nginx thread
> > 
> > Hello!
> > 
> > First of all, you may want to take a look at this warning in the development guide:
> > 
> >
> > 
> > Quoting it here:
> > 
> > It is recommended to avoid using threads in nginx because it will definitely break things: most nginx functions are not thread-safe. It is expected that a thread will be executing only system calls and thread-safe library functions.
> > If you need to run some code that is not related to client request processing, the proper way is to schedule a timer in the init_process module handler and perform required actions in timer handler. Internally nginx makes use of threads to boost IO-related operations, but this is a special case with a lot of limitations.
> > 
> Thanks Maxim, I completely get that, that is the reason I was looking for way to send a notification to the main thread, and didn't just try to call nginx functions from some other thread.
> In my case, I need to integrate with some 3rd party library that has its own event loop, it would require significant changes to the library to make it run inside nginx's event loop...
> So, my plan is to run it on a side thread, and send notifications between the threads, which would trigger some handler on the main thread whenever new data arrives.
> I can use ngx_notify for this, but if someone will use the module and also nginx's thread pool or some 3rd party module that uses ngx_notify, it will break.
> I think I can live with that, but would be nice to have a solution that is complete.

To re-iterate: inside a "side thread" you are not allowed to call 
any non-thread-safe libc functions.  It is reasonable to assume 
this excludes all 3rd party libraries with their own even loops.

Maxim Dounin

More information about the nginx-devel mailing list