question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document threading contract for Session class

See original GitHub issue

Right now, it’s quite difficult to figure out if the Session class is threadsafe or not. The docs don’t say, apart from a “thread-safe” bullet on the home page. Google led me to http://stackoverflow.com/questions/18188044/is-the-session-object-from-pythons-requests-library-thread-safe, whose first answer boils down to “be very careful”.

Inspired by that SO author, I’ve been auditing the code myself, and have come to the conclusion that Session is probably not threadsafe. (The use of self.redirect_cache set off red flags for me.) Reading through other requests bug reports, I see maintainers recommending one Session per thread, which implies that it’s not threadsafe.

The documentation should clarify this and recommend how to use Session in multithreaded programs. Possible text:

Session is not threadsafe. If you are using requests with an explicit Session
object in a multithreaded program, you should create one Session per thread.

If that’s accurate, let me know and I’ll submit a PR.

Also, I think the “thread-safe” bullet should be removed from the home page, or maybe replaced by “thread-safe in certain circumstances”.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:64
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
mmerickelcommented, May 17, 2017

What is the motivation for the “thread-safety” bullet point on the main page? I can’t find the word “thread” almost anywhere in the documentation and definitely nothing about thread safety. This issue alongside several others indicate that the session is not actually thread-safe. The bullet point really should be removed as it seems unfounded right now.

3reactions
Lukasacommented, Nov 5, 2015

I’d go further: I think the redirect cache is a bad idea. I still think that.

I think re-adding thread-safety would be a good idea. We can get away with it, I think, by locking around the CookieJar (which should be the only other thing that is a risk here). That’s a cheap-and-easy way to get something approximating thread safety, even if it doesn’t necessarily get great performance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multi-threading - SOCI (master)
The general rule for multithreading is that SOCI classes are not thread-safe, meaning that their instances should not be used concurrently by multiple...
Read more >
How to Programmatically Assemble Contracts in C# ... - Accusoft
First, use the Upload Document API to add the DOCX contract template to the Editor Server. PrizmDoc Editor will convert this file into...
Read more >
How to create a service with multiple threads which all use the ...
First, a Hibernate session is not thread-safe: ... You may also want to take a look at the Hibernate's batch processing doc page....
Read more >
ServiceBehaviorAttribute Class (System.ServiceModel)
Specifies the internal execution behavior of a service contract implementation. ... on one thread at a time * -- Creates one service object...
Read more >
Email-to-Case Threading - Salesforce Help
What's Messaging? Example: Apex Class for Time Selector Messaging Components · Create SMS Long Code Channels in Messaging · Start a Messaging Session...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found