You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: throwing from streambuf::underflow() causes very strange behavior #542
Here a claim was made -- "we are not using exceptions, but you are free to use them in your code".
Well, for starters since AWS SDK uses libstd -- it certainly throws std::bad_alloc (e.g. when making a copy of a std::string). I am not sure what is the effect of "-fno-exceptions" here -- abort or lack of proper stack unwinding, but in any case it will be a surprise.
Nevertheless I decided to check second portion of this claim -- data I push to S3 is sourced elsewhere. And that elsewhere can fail (e.g. network went down). Which means my custom std::streambuf::underflow() override throws exception. And when this happens I observe this:
on Windows -- operation immediately fails with "can't connect to endpoint"
on Linux -- operation hangs until s3requestTimeoutMs (1 minute in my case) expires and then fails with "can't connect to endpoint"
in both cases my exception doesn't "bubble up" into my code, something catches it.
I am not sure if SDK state is good at this point -- did it properly unwind all related stacks?
what I am supposed to do in underflow() in case of an error?
If you are using exceptions, you need to not compile with -fno-exceptions.
I don't. AWS SDK cmake script uses it by default (on Linux) and I don't know how to avoid it without modifying aforementioned script.
I don't think anything threw an exception in your case, the iostream::read call failed and we were able to fail the overall operation.
I definitely threw an exception (just added throw std::runtime_error("AAA") into my_streambuf::underflow(). Something (not me) was catching that exception. I'll check it again tomorrow.
Also, you may want to look at windows log in original post -- looks like there is access to uninitialized memory (see send error).
In general -- are you saying that on Linux there is no way to handle this without timing out? i.e. if smth goes wrong it'll take entire timeout for me to get back from related function call? So, on one hand I need large timeout when uploading large data (operation fails if it takes too long, even if it makes progress) and on the other hand I need smaller timeout to be able to discover problems early. :-\
Spent some time in debugger (on Windows) today. Exception is caught in iostream library, in read() method there is a piece that uses this macro:
#define _CATCH_IO_END _CATCH_ALL /* catch block for _Myios */ \
_Myios::setstate(ios_base::badbit, true); /* set badbit and rethrow */ \
_CATCH_END
The thing is it rethrows only if any bit of exceptions mask is set. My copy of C++11 draft standard has this in 27.7.2.1.p4:
If one of these called functions throws an exception, then unless explicitly noted otherwise, the input function sets badbit in error state. If badbit is on in exceptions(), the input function rethrows the exception without completing its actions, otherwise it does not throw anything and proceeds as if the called function had returned a failure indication.
Sigh... I hate iostreams even more now.
The fact that AWS SDK is compiled with -fno-exceptions (on Linux) doesn't matter because libstdc++ isn't linked statically.
Guess I'll set exceptions mask and see how it goes... There are outstanding questions still:
You can't safely turn them [exceptions] off in windows.
May I assume that AWS SDK is compiled with exceptions support on windows? (I am referring to nuget packages published on nuget.org)
If you are using exceptions, you need to not compile with -fno-exceptions.
Is there a cmake flag on Linux that allows me to remove -fno-exceptions from compiler cmd line?
If my AWS SDK library compiled with -fno-exceptions -- is it safe for exception to be propagated from basic_streambuf::underflow() to catch clause in my code? i.e. will it unwind portion of stack belonging to AWS SDK library?
P.S. I think you need to check why log contains garbage (only on Windows) when underflow() throws.
May I assume that AWS SDK is compiled with exceptions support on windows?
Yes. Turning it off, does not prevent windows from throwing, it only turns off stack unwinding... which would be bad.
Is there a cmake flag on Linux that allows me to remove -fno-exceptions from compiler cmd line?
No, but we can add one.
If my AWS SDK library compiled with -fno-exceptions -- is it safe for exception to be propagated from basic_streambuf::underflow() to catch clause in my code? i.e. will it unwind portion of stack belonging to AWS SDK library?
No, but the library itself is exception safe, so when we allow you to turn exceptions back on, then it will be safe to throw.
library itself is exception safe, so when we allow you to turn exceptions back on, then it will be safe to throw.
How do you provide data to libcurl? If it is done through a call back of some sort -- it may not be safe to throw from underflow() -- exception will have to propagate through portion of stack owned by libcurl (which is written in C?) and it may not like it.
In any case -- here is what outstanding (as I see it):
a CMake option is needed to control -fno-exceptions flag
fix problem with libcurl so that it fails operation immediately if read() fails (exception or failbit) instead of waiting until timeout expires
check why log contains garbled data (on Windows) if stream read() fails
I can file these as separate issues, if you prefer. Should I?
we're adding a ALLOW_EXCEPTIONS cmake option and then enclosing all c interop that touches C++ to #ifdef try catch around it. We'll let you know when it's done.
What you are going to do with caught exception (that are thrown from places like streambuf::underflow())?
I checked and indeed -- propagation of C++ exception through C stack is bad. I'll have to come up with some other way to deal with errors in my streambuf overrides. I wish you guys used some custom mechanism for feeding data -- you'd be dictating it's behavior (and it would be much simpler than one iostreams have). Every time I have to deal with them it is PITA. Way too over-engineered.
Can you check why (when stream get bad()) on Windows build random data gets logged?
Similarly for Linux build -- bad stream causes long delay and eventual timeout. Basically, if something went wrong on data retrieval side -- I can't quickly fail upload.
Hi, has anything changed wrt issues raised in this thread? Namely:
Need ALLOW_EXCEPTIONS CMake option (to remove -fno-exceptions flag on Linux)
handling exceptions escaped from iostream object (especially those thrown while inside of libcurl callback) -- you will be forced to catch these (to prevent propagation through C stack), how they will be ultimately reported?
if related iostream::read() call fails (during upload) on Linux we end up sleeping for entire request timeout (which can be very long)
on Windows when there is a problem with HTTPS connection (e.g. SSL error) log often generates "SendRequest: <long string full of garbage>". It is a problem when you intercept SDK logging and write it elsewhere. I've recently upgraded to v1.1.50 -- problem is still here
I have a related question on exceptions. I am working on Linux and I'm willing to enable exceptions. Say I want to use a custom memory allocator that might occasionally run out of memory. Is it safe to throw std::bad_alloc assuming I catch it in the application calling code? Will it cause any resource leaks (sockets, file descriptors, memory, etc.)?
@zvd1509 Yes, on assumption that SDK doesn't have related bugs and your exception doesn't unwind stack owned by C code (e.g. throwing from curl callback)
@crusader-mike regarding SDK I guess my question is whether it is designed to support exception throwing (a separate question is whether it was properly tested in this scenario)? Regarding curl callbacks, are they expected to allocate memory using a custom allocator?
SDK is compiled with exceptions on Windows, so likely it is fine with exceptions.
About curl callbacks -- SDK can call your code from curl callbacks (e.g. your own streambuf implementation). You potentially could do anything in your code and if related iostream (also in your total control) is configured to allow exceptions to escape -- you'll end up with C++ exception unwinding C stack. There were talks about preventing such things in SDK code, but I don't know if it was ever done.
Plus, curl may not be the only way to create this situation... I didn't follow SDK progress for a long time...
Greetings! Sorry to say but this is a very old issue that is probably not getting as much attention as it deservers. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to open a new one.
Activity
JonathanHenson commentedon Jun 13, 2017
If you are using exceptions, you need to not compile with -fno-exceptions. You can't safely turn them off in windows.
If you want to throw an exception from your code and handle it in your code, that should work provided you have a try{} catch{} in your calling code.
Given the default implementations, On Linux, libcurl will wait until we send it data and if we take too long will timeout.
On windows, we are able to immediately fail the operation. None of this has anything to do with exceptions though.
I don't think anything threw an exception in your case, the iostream::read call failed and we were able to fail the overall operation.
crusader-mike commentedon Jun 13, 2017
I don't. AWS SDK cmake script uses it by default (on Linux) and I don't know how to avoid it without modifying aforementioned script.
I definitely threw an exception (just added
throw std::runtime_error("AAA")intomy_streambuf::underflow(). Something (not me) was catching that exception. I'll check it again tomorrow.Also, you may want to look at windows log in original post -- looks like there is access to uninitialized memory (see send error).
In general -- are you saying that on Linux there is no way to handle this without timing out? i.e. if smth goes wrong it'll take entire timeout for me to get back from related function call? So, on one hand I need large timeout when uploading large data (operation fails if it takes too long, even if it makes progress) and on the other hand I need smaller timeout to be able to discover problems early. :-\
crusader-mike commentedon Jun 14, 2017
Spent some time in debugger (on Windows) today. Exception is caught in iostream library, in read() method there is a piece that uses this macro:
The thing is it rethrows only if any bit of exceptions mask is set. My copy of C++11 draft standard has this in 27.7.2.1.p4:
Sigh... I hate iostreams even more now.
The fact that AWS SDK is compiled with -fno-exceptions (on Linux) doesn't matter because libstdc++ isn't linked statically.
Guess I'll set exceptions mask and see how it goes... There are outstanding questions still:
Is there a cmake flag on Linux that allows me to remove -fno-exceptions from compiler cmd line?
If my AWS SDK library compiled with -fno-exceptions -- is it safe for exception to be propagated from basic_streambuf::underflow() to catch clause in my code? i.e. will it unwind portion of stack belonging to AWS SDK library?
P.S. I think you need to check why log contains garbage (only on Windows) when underflow() throws.
JonathanHenson commentedon Jun 14, 2017
May I assume that AWS SDK is compiled with exceptions support on windows?
Yes. Turning it off, does not prevent windows from throwing, it only turns off stack unwinding... which would be bad.
Is there a cmake flag on Linux that allows me to remove -fno-exceptions from compiler cmd line?
No, but we can add one.
If my AWS SDK library compiled with -fno-exceptions -- is it safe for exception to be propagated from basic_streambuf::underflow() to catch clause in my code? i.e. will it unwind portion of stack belonging to AWS SDK library?
No, but the library itself is exception safe, so when we allow you to turn exceptions back on, then it will be safe to throw.
crusader-mike commentedon Jun 15, 2017
How do you provide data to libcurl? If it is done through a call back of some sort -- it may not be safe to throw from
underflow()-- exception will have to propagate through portion of stack owned by libcurl (which is written in C?) and it may not like it.In any case -- here is what outstanding (as I see it):
I can file these as separate issues, if you prefer. Should I?
JonathanHenson commentedon Jun 28, 2017
we're adding a ALLOW_EXCEPTIONS cmake option and then enclosing all c interop that touches C++ to #ifdef try catch around it. We'll let you know when it's done.
crusader-mike commentedon Jun 29, 2017
What you are going to do with caught exception (that are thrown from places like streambuf::underflow())?
I checked and indeed -- propagation of C++ exception through C stack is bad. I'll have to come up with some other way to deal with errors in my streambuf overrides. I wish you guys used some custom mechanism for feeding data -- you'd be dictating it's behavior (and it would be much simpler than one iostreams have). Every time I have to deal with them it is PITA. Way too over-engineered.
Can you check why (when stream get bad()) on Windows build random data gets logged?
Similarly for Linux build -- bad stream causes long delay and eventual timeout. Basically, if something went wrong on data retrieval side -- I can't quickly fail upload.
Thank you.
crusader-mike commentedon Sep 22, 2017
Hi, has anything changed wrt issues raised in this thread? Namely:
Need ALLOW_EXCEPTIONS CMake option (to remove -fno-exceptions flag on Linux)
handling exceptions escaped from iostream object (especially those thrown while inside of libcurl callback) -- you will be forced to catch these (to prevent propagation through C stack), how they will be ultimately reported?
if related iostream::read() call fails (during upload) on Linux we end up sleeping for entire request timeout (which can be very long)
on Windows when there is a problem with HTTPS connection (e.g. SSL error) log often generates "SendRequest: <long string full of garbage>". It is a problem when you intercept SDK logging and write it elsewhere. I've recently upgraded to v1.1.50 -- problem is still here
Namespace-qualify HttpMethod (#542)
zdv1509 commentedon Oct 7, 2019
I have a related question on exceptions. I am working on Linux and I'm willing to enable exceptions. Say I want to use a custom memory allocator that might occasionally run out of memory. Is it safe to throw std::bad_alloc assuming I catch it in the application calling code? Will it cause any resource leaks (sockets, file descriptors, memory, etc.)?
crusader-mike commentedon Oct 7, 2019
@zvd1509 Yes, on assumption that SDK doesn't have related bugs and your exception doesn't unwind stack owned by C code (e.g. throwing from curl callback)
zdv1509 commentedon Oct 7, 2019
@crusader-mike regarding SDK I guess my question is whether it is designed to support exception throwing (a separate question is whether it was properly tested in this scenario)? Regarding curl callbacks, are they expected to allocate memory using a custom allocator?
crusader-mike commentedon Oct 7, 2019
SDK is compiled with exceptions on Windows, so likely it is fine with exceptions.
About curl callbacks -- SDK can call your code from curl callbacks (e.g. your own streambuf implementation). You potentially could do anything in your code and if related iostream (also in your total control) is configured to allow exceptions to escape -- you'll end up with C++ exception unwinding C stack. There were talks about preventing such things in SDK code, but I don't know if it was ever done.
Plus, curl may not be the only way to create this situation... I didn't follow SDK progress for a long time...
crusader-mike commentedon Oct 7, 2019
@zdv1509 FYI - nothing happened wrt preventing C++ exception from unwinding curl stack. See this.
github-actions commentedon Oct 7, 2020
Greetings! Sorry to say but this is a very old issue that is probably not getting as much attention as it deservers. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to open a new one.