cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: CURLOPT_FRESH_CONNECT and multithreading

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Tue, 24 Sep 2013 23:03:36 +0200 (CEST)

On Tue, 24 Sep 2013, Dima Tisnek wrote:

> Hey all, I'm working on some legacy code that uses FRESH_CONNECT as a hack
> to ensure that sockets left in inconsistent state are closed and refreshed.
>
> Possible causes of inconsistent state are http-level timeout/confusion,
> server bugs, read/write function bugs, incomplete chunked transfer read-out
> via error return from readdata function, incorrect content-length, etc.
>
> First off, was the original developer's logic sounds in single-threaded
> case?

The original developer's (mine) logic was to make sure connections can be
re-used as much as possible as long as the handle is re-used. Handles in
libcurl were never made to be used by more than one thread at a time but
things were designed from the beginning to allow applications to use separate
handles in different threads without problems.

> I'm trying to make this code logically thread-safe. New libcurl handle is
> allocated for each transfer, but the underlying socket is up to libcurl to
> reuse or recreate.

libcurl keeps perfect track of its own sockets. Just don't share the handle
between threads and you'll be fine!

> This begs the question, is libcurl's socket cache per-thread or global?

It is per handle. If you use the easy interface the cache is on the easy
handle, if you use the multi interface the cache is in the multi handle!

> If so, I suppose FRESH_CONNECT only hides the problem. in this case, what's
> the right way to explicitly close the underlying socket?

Since you didn't really describe your problem we can't tell what it is... But
why would you need or want to "explicitly close the underlying socket" ?
libcurl doesn't offer a socket API, it offers a transfer focused API so that
it can handle the sockets and connections on its own terms and there should be
very rare that an application actually needs to have a connection forcibly
torn down and not allow libcurl to re-use it (if possible).

-- 
  / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2013-09-24