Azure / Azure/azure-sdk-for-cpp

Close() throws exception from the destructor of ProducerClient causing the users to terminate, without an opportunity to catch the exception

Open
#6,958 2 comments 0 reactions 0 assignees View on GitHub
customer-reported Event Hubs needs-triage question
Dominant language
C++
Stars
205
Forks
172
Avg merge
1d 15m
Merged PRs (30d)
33

Description

**Describe the bug**
In our project, we sometimes recreate the ProducerClient to come out of idle channels scenario, we call Reset() on the producer client. Sometimes, we get terminated with the error: "Could not close message sender". Needless to ask, we have many try and catches in place to handle exceptions, but this exception was not handled.

Upon looking closer into the implementation, we got to the conclusion that Close() throws from the destructor, which is marked as noexcept, hence we terminate no matter what.

Close may throw that is fine but calling Close() from the destructor and then letting the exception propagate from destructor as per C++ standards will cause std::terminate to be called.

Close() is being called from here:

https://github.com/Azure/azure-sdk-for-cpp/blob/04f2ce086af80d41892bfffd645c891b36c98961/sdk/eventhubs/azure-messaging-eventhubs/inc/azure/messaging/eventhubs/producer_client.hpp#L101

***Exception or Stack Trace***
"Could not close message sender", it comes from deep inside amqp implementation here: https://github.com/Azure/azure-sdk-for-cpp/blob/04f2ce086af80d41892bfffd645c891b36c98961/sdk/core/azure-core-amqp/src/impl/uamqp/amqp/message_sender.cpp#L401

**To Reproduce**
We can create a minimal example where a destructor throws in C++17. Real scenarion is difficult to reproduce as the underlying channel needs to throw while closing, which we cannot mimic.

It only happens when close() throws, similar code can be checked to understand the issue where the destructor throws.

**Expected behavior**
The ~ProducerClient() should not throw.

**Setup (please complete the following information):**
We're using vcpkg installed version with C++17.

**Information Checklist**
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [x] Bug Description Added
- [x] Repro Steps Added
- [x] Setup information Added

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.