bridgestan: Improved error handling in the C API

There was some discussion about error handling in #88, but I thought a separate issue would be a better place to discuss this.

Currently, the error messages of the C++ exception are printed to stderr, which works fine, but has the disadvantage that many users might not know to look there, and depending on the setup it might not even be easy to find it (for example in a jupyter notebook that is started from jupyterhub or from a system service). Also, if several models are failing at the same time, a user would have to figure out which error belongs to which model manually.

The two (I think) most important kinds of failures would be

  1. Errors during model creation (ie bs_construct). Users will I think provide invalid data fairly often, and the error message should contain information about what part of the data was invalid.
  2. Errors during bs_log_density_gradient (or for different sampling / VI algorithms the other log_density functions): Error messages often contain information about why there was a divergence during sampling, and sampling algorithms might want to collect this information to help model debugging.

I can see a couple of different options of how this could be achieved, my favorites would probably be those:

  1. Store the error information in the bs_model_rng struct as an extra field, and provide an accessor function for it. This unfortunately makes the bs_log_density_gradient function family no longer thread safe. We can work around that issue however by creating new bs_model_rng structs for each thread. With the current API this would require loading the data several times however. This again can be avoided by splitting bs_model_rng into two separate types: One for the dataset and the potential error message from model creating, and a separate context for rng and error messages during sampling. This would then look like this:
bs_model *model = bs_model_construct(data)
if (model == 0) {
    // A failure here indicates that we couldn't even allocate the error msg,
    // which would usually not happen...
    return -1;
}

// Callers have to check that there wasn't an error explicitly again!
const char *msg = bs_model_error_msg(model);
if (msg != 0) {
    // Data was invalid...
    // Do something with message
    bs_model_free(model);
}

// In each thread do the following:
{
    bs_ctx *ctx = bs_ctx_construct(model, seed, seed_idx);
    if (ctx == 0)
        stop();
    }

    // Call log_density and other functions in the sampler or...
    for (int i = 0; i < 10; i++) {
        int rc = bs_log_density(ctx, ...);
        if (rc != 0) {
            char *msg = bs_ctx_error_msg(ctx);
            // do something with message
        }
    }

    bs_ctx_free(ctx);  // Frees the msg field if necessary
}

// And finally a single call to free the memory in the
// original thread
bs_model_free(model);  # Frees the msg field if necessary

The additional function declarations:

// Create a new model. A non-null return value still has to
// be checked for errors with `bs_model_error_msg`.
bs_model *bs_model_construct(const char*);

// Check for an error in model creation.
const char *bs_model_error_msg(const bs_model*);

// Create a new ctx object with rng and space for an error message.
// the model has to outlive the ctx.
bs_ctx *ctx bs_ctx_construct(const bs_model*, int, int);

// Most other functions now take a bs_ctx argument.
// The ctx arguments are not const, because we might change the error.
int bs_log_density_gradient(bs_ctx*, ...);

const char *bs_ctx_error_msg(const bs_ctx*);

In this scenario bs_model would be Sync (functions that take a const pointer to bs_model can be called concurrently from different threads), but not Send (ownership of bs_model can not be transferred to a different thread, ie it has to be deallocated from the same thread where it was created. Is that actually a requirement?). bs_ctx doesn’t need to be Sync or Send, as every thread can just create a separate context (which should be super cheap).

  1. Add char **error_msg arguments to the fallible functions, and add a bs_free_error_msg(char *) function. This would then look something like this:
char *msg;
bs_model_rng *ctx = bs_construct(data, *msg);
if (ctx == 0) {
    do_something_with_msg(msg);
    bs_free_error_msg(msg);
}

int rc = bs_log_density(ctx, *msg, ...);
if (rc != 0) {
    do_something_with_msg(msg);
    bs_free_error_msg(msg);
}

I hope this is at least somewhat useful to you, happy to discuss more if there is interest in a change like this.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

I haven’t done anything, seeing as I have no clue who all of you are.

On Friday, March 24, 2023, Brian Ward @.***> wrote:

Why were you suggesting char** arguments rather than streams? Is it hard to take in a C stream and use it in C++? Streams just make the code a lot simpler if you are going to write to a file or handle like stderr or stdout and they don’t need to be freed like character pointers. From a Python interface, I would strongly prefer to have the exceptions percolate through to Python exceptions and then let the clients handle them. Presumably that would also make sense in Julia and R. For interfaces like C where that’s not possible, I think we want to take in an output stream.

C-style streams (I assume you mean FILE* from stdio.h) seem like an equally ugly API boundary to have in something as a char**. The only resources I can find about how to use them from Python all involve creating temporary files or platform-specific named pipes, which is much worse than just creating a char**

— Reply to this email directly, view it on GitHub https://github.com/roualdes/bridgestan/issues/90#issuecomment-1483426731, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATCK5W3MVE22BR2UERKKTYTW5YHCFANCNFSM6AAAAAAWG7FDAQ . You are receiving this because you were mentioned.Message ID: @.***>