Threading: do not memoize/reuse Patron session #796
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This reverts changes from b0b1e38 and the Session object is going to be configured anew for each request.
The rationale/reason is that Patron session objects are not thread-safe by default, but are rather meant to be used thread-locally or pooled. Contrary to what one would believe, retaining a Patron session will not lead to the libCURL handle being reused across requests to achieve keepalive. Patron is architected in such a way that all resources associated to a specific request get initialized and allocated right when the request begins, and get freed/deallocated once the response has been read out or an abort has been raised. Therefore the only memory saving achieved from preserving a Patron session is the Ruby object for the Session itself.
This retaining however will lead to unpleasant situations with threads, since the same session might end up being reused by different threads. For example, when configuring Faraday like so:
there will be effectively one Session object reused across threads. Since Patron unlocks the GIL during request/response, this will lead to threads receiving response bodies for other threads and other unpleasant occurrences.
Also passes the block in create_connection in integration tests as the adapter configuration block.
Todos