Skip to content

bug: throwing from streambuf::underflow() causes very strange behavior #542

Closed

Description

@crusader-mike

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?

I've attached logs, they are kind of curious...

win_log.zip
linux_log.zip

P.S. I use v1.0.59. Please, let me know if this issue is already fixed in newer version.

Activity

  1. JonathanHenson commented on Jun 13, 2017

    @JonathanHenson
    Contributor

    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.

  2. crusader-mike commented on Jun 13, 2017

    @crusader-mike
    Author

    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. :-\

  3. crusader-mike commented on Jun 14, 2017

    @crusader-mike
    Author

    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.

    1. 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.

    1. Is there a cmake flag on Linux that allows me to remove -fno-exceptions from compiler cmd line?

    2. 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.

  4. JonathanHenson commented on Jun 14, 2017

    @JonathanHenson
    Contributor

    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.

  5. crusader-mike commented on Jun 15, 2017

    @crusader-mike
    Author

    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):

    1. a CMake option is needed to control -fno-exceptions flag
    2. fix problem with libcurl so that it fails operation immediately if read() fails (exception or failbit) instead of waiting until timeout expires
    3. check why log contains garbled data (on Windows) if stream read() fails

    I can file these as separate issues, if you prefer. Should I?

  6. JonathanHenson commented on Jun 28, 2017

    @JonathanHenson
    Contributor

    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.

  7. crusader-mike commented on Jun 29, 2017

    @crusader-mike
    Author

    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.

  8. crusader-mike commented on Sep 22, 2017

    @crusader-mike
    Author

    Hi, has anything changed wrt issues raised in this thread? Namely:

    1. Need ALLOW_EXCEPTIONS CMake option (to remove -fno-exceptions flag on Linux)

    2. 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?

    3. if related iostream::read() call fails (during upload) on Linux we end up sleeping for entire request timeout (which can be very long)

    4. 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

  9. added
    guidance
    help wanted
    and removed on Apr 19, 2019
  10. zdv1509 commented on Oct 7, 2019

    @zdv1509

    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.)?

  11. crusader-mike commented on Oct 7, 2019

    @crusader-mike
    Author

    @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)

  12. zdv1509 commented on Oct 7, 2019

    @zdv1509

    @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?

  13. crusader-mike commented on Oct 7, 2019

    @crusader-mike
    Author

    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...

  14. crusader-mike commented on Oct 7, 2019

    @crusader-mike
    Author

    @zdv1509 FYI - nothing happened wrt preventing C++ exception from unwinding curl stack. See this.

  15. github-actions commented on Oct 7, 2020

    @github-actions

    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.

  16. added
    closing-soon
    and removed
    closing-soon
    on Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-for-stalenessguidancehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions