Open HertzDevil opened 2 years ago
Regardles of HTTPS, it is not safe to do this with an instance of HTTP::Client
and ensure correctness.
HTTP::Client
is not "fiber-safe" as-designed, as you have found out - but besides TLS negotiation problems, one fiber may receive the response of a totally different request started by another, or other permutations. So this is probably never a good idea.
Anyways - there are probably two ways to fix the segfault off the top of my head:
HTTP::Client.new
will lazy-connect. Meaning HTTP::Client.new
doesn't do anything until you call .get
or some other action. It could be changed so that it connects right away, ensuring only one fiber does the setup.Personally I would like to see (1). I think this is fine. I patch HTTP::Client
to do this anyways because I set up connection pools.
I think we should eventually change HTTP::Client
so that it's fiber-safe.
In the meantime, it would be great if the docs were be updated to state that HTTP::Client
is not fiber-safe. I made the same assumption (with the aim of reusing a TCP connection), and was similarly bitten.
Meta issue on refactoring HTTP::Client
: https://github.com/crystal-lang/crystal/issues/6011
The following code seems to always crash:
The same works for non-HTTPS connections and also if two different
HTTP::Client
s sharing the same TLS context are used: