Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent connections from entering Reactor after shutdown begins #2377

Merged
merged 1 commit into from Sep 23, 2020

Commits on Sep 22, 2020

  1. Prevent connections from entering Reactor after shutdown begins

    When a `Puma::Server` instance starts to shutdown, it sets the instance
    variable `@queue_requests` to `false`, then starts to shut down the
    Reactor. The intent here is that after the Reactor shuts down, threads
    won't have the option of sending their connections to the Reactor (the
    connection must either be closed, assuming we've written a response to
    the socket, or the thread must wait until the client has finished
    writing its request, then write a response, then finally close the
    connection). This works most of the time just fine.
    
    The problem is that there are races in the implementation of the
    `ThreadPool` where it's possible for a thread to see that
    `@queue_requests` is `true` before the `Server` shutdown has started,
    then later try to add the request to the Reactor, not knowing that it's
    already shutting down.
    
    Depending on the precise order of operations, one possible outcome is
    that the `ThreadPool` executes `@reactor.add` after the `@reactor` has
    closed its `@trigger` pipe, resulting in the error `Error reached top of
    thread-pool: closed stream (IOError)`. Clients experience connection
    reset errors as a result.
    
    The fix introduced in this commit is to add a `Mutex` that makes it
    impossible for a thread in the `ThreadPool` to add a client connection
    to the `@reactor` after `@queue_requests` has been set to `false`.
    
    Co-Authored-By: Leo Belyi <leonid.belyi@mycase.com>
    cjlarose and LeoTheMighty committed Sep 22, 2020
    Copy the full SHA
    373bb27 View commit details
    Browse the repository at this point in the history