How to handle abort() invoked in mongodb c driver when developing applications on mongodb c++ driver

Hi folks:

I am developing an application basing on mongodb c++ driver.
When I call insert_many(), if the documents are too large, the underlayer of the c driver invokes c standard api ‘abort()’ and so my application exits sliently.
I saw c++ could throw c++ exception when calling insert_many() , but obvisouly it doesn’t cover this case.
But I do need a chance to run my application code when abort() is invoked.

So my question is:
#1 does mongodb c++ driver provides a way to handle this gracefully? If so, how?
#2 is there a recommended way to handle this case?
#3 Is it necessary to handle this in my application layer? for example, use SIGABRT mechanism. I don’t like this way if #1 or #2 could work.

Thanks very much.

When I call insert_many(), if the documents are too large, the underlayer of the c driver invokes c standard api ‘abort()’ and so my application exits sliently.

I was unable to reproduce with this example. It calls insert_many() with a large document and results in an exception with the message object to insert too large. size in bytes: 16777258, max size: 16777216.

Some additional information may help to reproduce:

  • How is insert_many() called? Can you provide a code snippet?
  • What server version was used? The error I got comes from the server. I tested with MongoDB server 7.0.0.
  • What version of the C and C++ drivers was used? I tested with C driver 1.26.2 and C++ driver 3.9.0.

Thanks very much for your reply.

I pasted the crash dump file debugging screenshot for your reference.
I highlighted some information:

  1. the crash happens on line of BSON_ASSERT ((buffer->datalen + data_size) < INT_MAX);
  2. the (buffer->datalen + data_size) is bigger than INT_MAX
  3. The concret values of buffer->datalen and data_size in debugger’s Watch window.

Could you please try to create a huge document?

  • How is insert_many() called? Can you provide a code snippet?
    My application reads documents from a queue and writes the documents together by calling insert_many. No special configurations.
  • What server version was used? The error I got comes from the server. I tested with MongoDB server 7.0.0.
    7.0.
  • What version of the C and C++ drivers was used? I tested with C driver 1.26.2 and C++ driver 3.9.0.
    mongo-c-driver_x64-windows: 1.24.3
    mongo-cxx-driver_x64-windows: 3.8.0

Thank you for the additional information. I can reproduce the error with C driver 1.23.2 (example). However, this did not reproduce when testing with C driver 1.25.1. I expect this is fixed by CDRIVER-4772. Try upgrading to C driver 1.25.1 or later.

Thanks very much, very helpful.

One general question, currently I am using try catch to handle MongoDB driver exceptions. Should I handle the abort() in my application? Because there are might be somewhere else I could trigger ‘abort()’.

Should I handle the abort() in my application? Because there are might be somewhere else I could trigger ‘abort()’.

Probably “no”. I expect an abort during normal operations is likely a driver bug. A notable exception: libbson aborts on failure to allocate memory.

Great, thanks very much.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.