RestClient thread safety

5,981 views
Skip to first unread message

Clayton sayer

unread,
Jan 27, 2012, 12:57:39 PM1/27/12
to RestSharp
In our product we have a REST client base class which ends up doing
all the REST calls to the backend. In it, we currently create a new
RestClient for each REST call, although we do reuse cookies and such,
since they're part of the RestRequest itself. I would prefer to change
it so we have 1 RestClient that is reused across calls, but I'm unable
to find any details on the thread safety of RestClient. The only thing
that I have found that supports that is
http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/,
which says "The Client handles all the sending of Requests and can be
shared across the whole application if you want (Don’t take my word
for it, but it looks like it is Thread-safe)." Not very encouraging,
to say the least.

Can anyone confirm or deny that RestClient is thread safe?

Thanks,
Clay

Clayton sayer

unread,
Jan 27, 2012, 1:04:01 PM1/27/12
to RestSharp
Sorry, I forgot to mention that we're only using Execute<T> in
RestClient.

On Jan 27, 9:57 am, Clayton sayer <cbig...@gmail.com> wrote:
> In our product we have a REST client base class which ends up doing
> all the REST calls to the backend. In it, we currently create a new
> RestClient for each REST call, although we do reuse cookies and such,
> since they're part of the RestRequest itself. I would prefer to change
> it so we have 1 RestClient that is reused across calls, but I'm unable
> to find any details on the thread safety of RestClient. The only thing
> that I have found that supports that ishttp://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-...,

Andrew Young

unread,
Jan 31, 2012, 1:28:29 AM1/31/12
to rest...@googlegroups.com
From my experience RestClient was designed to be threadsafe. Requests are completely atomic.

Maybe John can put the final word on this?

John Sheehan

unread,
Feb 2, 2012, 3:17:23 PM2/2/12
to rest...@googlegroups.com
I honestly have no idea. Never once made any thread safe considerations.

Adam Ralph

unread,
Mar 1, 2012, 5:17:11 AM3/1/12
to RestSharp
It seems like it isn't thread safe. When I execute a parallel query
using .AsParallel(), with an invocation of RestClient.Execute(request)
for each item, I get random InvalidOperationExceptions being thrown.
Each thread creates it's own independent request object. If I change
to creating a new RestClient in each thread, everything works
perfectly.

ayoung

unread,
Apr 3, 2012, 1:59:32 PM4/3/12
to RestSharp
Ralph,

Just curious: what is the stack trace like when the exceptions are
thrown?

huntert

unread,
Oct 24, 2012, 12:03:43 AM10/24/12
to rest...@googlegroups.com
Something like this:

   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at RestSharp.RestClient.ConfigureHttp(IRestRequest request, IHttp http)
   at RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action`2 callback, String httpMethod, Func`4 getWebRequest)
   at RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action`2 callback)
   at RestSharp.RestClientExtensions.ExecuteAsync(IRestClient client, IRestRequest request, Action`1 callback)

the exception throws at "foreach(var p in DefaultParameters)" in "private void ConfigureHttp(IRestRequest request, IHttp http)"

ayoung於 2012年4月4日星期三UTC+8上午1時59分32秒寫道:

Keivan Beigi

unread,
Oct 25, 2012, 2:48:22 AM10/25/12
to rest...@googlegroups.com
I have the exact same issue using 104.1.0.0.

Henk Punt

unread,
Jul 3, 2013, 6:36:00 AM7/3/13
to rest...@googlegroups.com
It is not thread-safe,

for instance we found a subtle threading bug when we were trying to use the same single RestClient across multiple threads
AND using the RootElement property of Request object.
Internally the deserializers are cached on the RestClient instance.Then the 'RootElement' property of a Request is
copied to the deserializer RootElement property at the start of a request. This means that if you have 2 simultaneous request
the will potentially overwrite each others root element if they use the same deserializer (for instance the JsonDeserializer).

Regards

Henk Punt

So like John says on 2/2/12, it does not seem to be written with Thread safety in mind.

Matt Evans

unread,
Dec 7, 2018, 7:03:08 AM12/7/18
to RestSharp
Anyone know if this is still the case (2018...)

I'd like to implement something like this:

Alexey Zimarev

unread,
Jan 16, 2019, 4:32:18 PM1/16/19
to RestSharp
The issue with manipulated RootElement and other serialization attributes is fixed in the code, the package will be released with the new minor version.
Reply all
Reply to author
Forward
0 new messages