curl-rust icon indicating copy to clipboard operation
curl-rust copied to clipboard

curl handle stashed to TLS hangs shutdown on various windows versions

Open mitsuhiko opened this issue 7 years ago • 3 comments

We have not fully identified what the cause it but it would appear that if a curl easy handle is put to TLS on windows, it can cause hangs on shutdown on certain versions. I'm entirely unsure what causes this and the exact conditions of the hang are very mysterious.

What we know so far is that if the handle is made and put to TLS everything is fine. If the handle however did an HTTP request first, is kept in TLS the program will hang on shutdown the first time after the HTTP request is made but if the program is run a second time it succeeds and only will start blocking again after some time has passed (6 hours?). Even a computer restart does not bring the program back to hanging. We saw this with Windows 7, Windows Server 2012.

This sounds very much like something in curl itself but it's mysterious nontheless. We currently work around by resetting the handles before shutdown on all threads that have one.

mitsuhiko avatar Aug 13 '18 07:08 mitsuhiko

Oh dear, this definitely sounds bad! I agree that this is probably a curl issue, but I can at least chime in and say that I've definitely seen my fair share of weirdness of TLS destructors and windows. At least in the standard library we have to update tests to avoid that in the past because it causes various hangs and crashes.

I've also never managed to bottom out the issues here, I've sort of just prayed all along that it's not a bug in libstd (or in this case libcurl) and it's instead just a weird Windows issue.

In any case I definitely agree that manually resetting TLS vars is the best way to go here, that'll make sure that hopefully no code executes on a TLS destructor

alexcrichton avatar Aug 14 '18 00:08 alexcrichton

@alexcrichton any pointers to those hangs? I have not had much success in finding similar cases and the fact that tls on windows can cause hangs is concerning.

mitsuhiko avatar Aug 14 '18 06:08 mitsuhiko

I think what I was remembering were cases like https://github.com/rust-lang/rust/pull/20704 where if a thread is panicking while the main thread exits on windows "weird stuff happens" including either segfaults or deadlock. These were all really long ago though and we may have just fixed bugs in libstd, I certainly haven't seen anything recently

alexcrichton avatar Aug 14 '18 17:08 alexcrichton