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

Disable Excon sockets thread cache #346

Merged
merged 1 commit into from Oct 30, 2017
Merged

Conversation

alan
Copy link
Contributor

@alan alan commented Oct 26, 2017

This is the cause of the memory leak that we experienced recently.

Every Excon connection has a cache of sockets that ensures that a socket is only
used by a specific thread during its lifetime. This means that 1 Excon
connection might hold any number of open sockets to the same server.

In our case for example if Puma is running with 4 threads, and a connection pool
of 4 Excon connections to the PVB API it means that there will a maximum of 16
HTTP persistent connections.

The issue is that the implementation of the cache doesn't clear old connections
for threads that are no longer running (an issue when Puma is configured to
autoscale threads). We inadevertedly solved the memory leak by configuring Puma
to use a fixed number of threads.

The implementation of this cache was changed just before we started having
memory leaks to fix a use case of Excon that we don't use. The old
implementation didn't have a memory leak with our use case.

Although this makes the PrisonVisits::Client not thread safe it's fine in our
use case because:

  • we use a connection pool of connections and a connection is only used by 1
    thread at a time.
  • It means that we don't hold sockets open unnecessarily for each Excon
    connection.

I've tested the value of this setting locally by load testing the server with
concurrent requests:

  • with the cache enabled and Puma autoscaling threads it showed the memory
    leak when spacing the load testing by 1 minute between benchmarks
  • with the cache disabled and Puma autoscaling threads memory usage remained
    stable and there were no threading issues.

Excon discussion about the recent
change to the cache where it actually mentions that it has an memory leak issue.

@alan alan temporarily deployed to pvb-public-pr-346 October 26, 2017 15:57 Inactive
@alan
Copy link
Contributor Author

alan commented Oct 27, 2017

Opened an issue for Excon: excon/excon#651

Copy link
Contributor

@StupidCodeFactory StupidCodeFactory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as in the corresponding PR in pvb2

alan added a commit to ministryofjustice/prison-visits-2 that referenced this pull request Oct 30, 2017
Current version of Excon has a memory leak when threads using an Excon
connection get destroyed.

Applies same fix as
ministryofjustice/prison-visits-public#346

The memory leak in pvb2 is slower because Puma is configured to use less threads
so they probably get recycled less often.

Also sets a constant number of threads for Puma in production as we do in
pvb-public.
StupidCodeFactory pushed a commit to ministryofjustice/prison-visits-2 that referenced this pull request Oct 30, 2017
Current version of Excon has a memory leak when threads using an Excon
connection get destroyed.

Applies same fix as
ministryofjustice/prison-visits-public#346

The memory leak in pvb2 is slower because Puma is configured to use less threads
so they probably get recycled less often.

Also sets a constant number of threads for Puma in production as we do in
pvb-public.
This is the cause of the memory leak that we experienced recently.

Every Excon connection has a cache of sockets that ensures that a socket is only
used by a specific thread during its lifetime. This means that 1 Excon
connection might hold any number of open sockets to the same server.

In our case for example if Puma is running with 4 threads, and a connection pool
of 4 Excon connections to the PVB API it means that there will a maximum of 16
HTTP persistent connections.

The issue is that the implementation of the cache doesn't clear old connections
for threads that are no longer running (an issue when Puma is configured to
autoscale threads). We inadevertedly solved the memory leak by configuring Puma
to use a fixed number of threads.

The implementation of this cache was changed just before we started having
memory leaks to fix a use case of Excon that we don't use. The old
implementation didn't have a memory leak with our use case.

Although this makes the `PrisonVisits::Client` not thread safe it's fine in our
use case because:

 - we use a connection pool of connections and a connection is only used by 1
   thread at a time.
 - It means that we don't hold sockets open unnecessarily for each Excon
   connection.

I've tested the value of this setting locally by load testing the server with
concurrent requests:

  - with the cache enabled and Puma autoscaling threads it showed the memory
    leak when spacing the load testing by 1 minute between benchmarks
  - with the cache disabled and Puma autoscaling threads memory usage remained
    stable and there were no threading issues.

Excon [discussion](excon/excon#640) about the recent
change to the cache where it actually mentions that it has an memory leak issue.
@alan alan force-pushed the disable-connection-thread-cache branch from ed5ea72 to 194d030 Compare October 30, 2017 11:11
@alan alan merged commit 7bbe5b1 into master Oct 30, 2017
@alan alan deleted the disable-connection-thread-cache branch October 30, 2017 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants