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

SocketException: Socket closed when :as != :string #3

Open
S11001001 opened this issue Apr 1, 2010 · 1 comment
Open

SocketException: Socket closed when :as != :string #3

S11001001 opened this issue Apr 1, 2010 · 1 comment

Comments

@S11001001
Copy link

On 85ce13f, unreliably (about 25% of the time), when using :as :identity, I get a prematurely closed socket underlying the .getContent return value.

To reproduce: GET some content with :as :identity, then (-> response :entity .getContent duck/slurp*) a few times.

I worked around it by making "getConnectionManager shutdown" happen only when as is :string.

@rnewman
Copy link
Owner

rnewman commented Apr 2, 2010

Yes, this is a thorny issue arising because HttpClient doesn't know when we're done with a resource. The only reasonable approach was to eagerly shut things down, which is slightly less efficient and — in this case, apparently — unsafe.

There is probably a race between your code using the Entity and the connection manager closing the stream. I can't reproduce it, but that doesn't mean it doesn't exist.

However, if we don't shut down the connection manager, and don't empty the stream, then the resources are never freed.

Your workaround is unfortunately not quite the correct fix, which would be to use some kind of resourcing system to keep track of the scope of the entity; when it leaves scope it would be freed.

I will consider whether there is a good solution to this problem.

In the meantime, I recommend defining your own entity-as method:

(defmethod http/entity-as :my-slurp [entity as status]
  (-> entity .getContent duck/slurp*))

then accessing the slurped result as (:entity response). This will ensure that your code runs while the socket is still open. :identity is really not ideal when compared to an entity-as method.

senior referenced this issue in revelytix/clj-apache-http Mar 6, 2012
Remove dependency on clojure-contrib to make this friendlier for Clojure 1.3 use.
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

No branches or pull requests

2 participants