On 11/27/26 6:21 PM, ylavic.apache.org via commits wrote:
> Author: ylavic
> Date: Fri Nov 27 17:21:44 2026
> New Revision: 1940824
> 
> Log:
> apr_pollset: port: don't recycle event elements a concurrent poller holds.
> 
> impl_pollset_poll() moves the dead ring (elements removed while possibly
> returned by a port_getn()) to the free ring at the end of every poll. With
> several threads polling an APR_POLLSET_THREADSAFE pollset, an element
> returned to one poller's port_getn() and removed meanwhile can be recycled
> by another poller finishing first, and reused by an add for another
> descriptor before the first poller handles it (reporting the wrong
> descriptor).
> 
> Count the pollers from before their port_getn() until their events are
> processed (under the ring lock), and recycle the dead ring only when the
> last one is done.
> 
> * poll/unix/port.c:
>   (struct apr_pollset_private_t): Add polling, document waiting more
>   precisely.
>   (impl_pollset_create): Initialize polling.
>   (poll_begin): New, associate the add ring with the port and count the
>   poller.
>   (poll_fired): New, move a returned element to the add ring.
>   (poll_end): New, recycle the dead ring once no poller is left.
>   (impl_pollset_poll): Use them.
> 
> Modified:
>    apr/apr/trunk/poll/unix/port.c
> 
> Modified: apr/apr/trunk/poll/unix/port.c
> ==============================================================================
> --- apr/apr/trunk/poll/unix/port.c    Fri Nov 27 16:54:00 2026        
> (r1940823)
> +++ apr/apr/trunk/poll/unix/port.c    Fri Nov 27 17:21:44 2026        
> (r1940824)

>      while (!APR_RING_EMPTY(&(pollset->p->add_ring), pfd_elem_t, link)) {
> @@ -390,10 +388,62 @@ static apr_status_t impl_pollset_poll(ap
>          APR_RING_INSERT_TAIL(&(pollset->p->query_ring), ep, pfd_elem_t, 
> link);
>      }
>  
> -    pollset_unlock_rings();
> -
>      if (rv != APR_SUCCESS) {
>          apr_atomic_dec32(&pollset->p->waiting);
> +    }
> +    else {
> +        /* counted before port_getn(): what it returns must not be recycled
> +         * by another poller's poll_end() until this one's poll_end()
> +         */
> +        pollset->p->polling++;

Why doesn't this need to be done by apr_atomic_inc32?

> +    }
> +    return rv;
> +}
> +
> +/* After port_getn() returned it, with the ring lock held: if the element is
> + * still on the query ring, move it to the add ring for re-association with
> + * the event port later.  (It may have already been moved to the dead ring
> + * by a call to pollset_remove on another thread.)
> + */
> +static void poll_fired(apr_pollset_t *pollset, pfd_elem_t *ep)
> +{
> +    if (ep->on_query_ring) {
> +        APR_RING_REMOVE(ep, link);
> +        ep->on_query_ring = 0;
> +        APR_RING_INSERT_TAIL(&(pollset->p->add_ring), ep,
> +                             pfd_elem_t, link);
> +    }
> +}
> +
> +/* After processing the events, with the ring lock held */
> +static void poll_end(apr_pollset_t *pollset)
> +{
> +    /* Shift all PFDs in the Dead Ring to the Free Ring, unless another
> +     * poller may still hold some of them (returned by its port_getn()).
> +     */
> +    if (--pollset->p->polling == 0) {

Why doesn't this need to be done by apr_atomic_dec32?

> +        APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring),
> +                        pfd_elem_t, link);
> +    }
> +}
> +
> +static apr_status_t impl_pollset_poll(apr_pollset_t *pollset,
> +                                      apr_interval_time_t timeout,
> +                                      apr_int32_t *num,
> +                                      const apr_pollfd_t **descriptors)
> +{
> +    unsigned int nget, i;
> +    apr_int32_t j;
> +    pfd_elem_t *ep;
> +    apr_status_t rv;
> +
> +    *num = 0;
> +    nget = 1;
> +
> +    pollset_lock_rings();
> +    rv = poll_begin(pollset);
> +    pollset_unlock_rings();
> +    if (rv != APR_SUCCESS) {
>          return rv;
>      }
>  

Regards

RĂ¼diger

Reply via email to