Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor error handling code to eliminate internal ID calls #4453

Merged
merged 10 commits into from
May 9, 2024

Conversation

qkoziol
Copy link
Contributor

@qkoziol qkoziol commented May 1, 2024

All calls to the H5I routines are now made in API routines (sometimes in FUNC_ENTER/LEAVE_* macros), except for some calls to H5E_clear_stack() within the library, but I'm planning to remove those over time.

Also, made all the library internal error messages into static const variables, instead of malloc'ing them, which means that they can just be referenced and not copied.

Several new and updated auto-generated header files were necessary to enable this.

qkoziol and others added 2 commits May 1, 2024 16:52
All calls to the H5I routines are now made in API routines (sometimes in
FUNC_ENTER/LEAVE_* macros), except for some calls to H5E_clear_stack() within
the library, but I'm planning to remove those over time.

Also, made all the library internal error messages into static const variables,
instead of malloc'ing them, which means that they can just be referenced
and not copied.

Several new and updated auto-generated header files were necessary to enable
this.

Signed-off-by: Quincey Koziol <quincey@koziol.cc>
@derobins
Copy link
Member

derobins commented May 2, 2024

If we're going to check in the generated header files, we should do that for the other files as well (in a separate PR) and create a GitHub action to complain if they need to be regenerated (in another PR).

@derobins derobins added Merge - To 1.14 This needs to be merged to HDF5 1.14 Priority - 1. High 🔼 These are important issues that should be resolved in the next release Component - C Library Core C library issues (usually in the src directory) Component - Build CMake, Autotools Type - Improvement Improvements that don't add a new feature or functionality labels May 2, 2024
@qkoziol
Copy link
Contributor Author

qkoziol commented May 2, 2024

Ah, I thought the generated H5E headers were included in the repo. I'll take them out.

Signed-off-by: Quincey Koziol <quincey@koziol.cc>
@qkoziol
Copy link
Contributor Author

qkoziol commented May 2, 2024

Ah, I thought the generated H5E headers were included in the repo. I'll take them out.

Removed

printf HEADER "H5_DLLVAR hid_t %-20s /* %s */\n","${name}_g;",$major{$name};
}

# Iterate over all the minor error sections
print HEADER "\n/*********************/\n";
print HEADER "/* Minor error codes */\n";
print HEADER "/*********************/\n";
while ( ($sect_name, $sect_desc) = each (%section)) {
print HEADER "\n/* $sect_desc */\n";
foreach $sect_name (sort keys %section) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort the errors alphabetically by name, so they have stable lists in the generated headers.

##############################################################################
# Create the generated portion of the H5E major message definition code
#
sub create_majdef ($) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a static const message for each library-internal major error message, so they aren't malloc'ed/free'd at init/term.

##############################################################################
# Create the generated portion of the H5E minor message definition code
#
sub create_mindef ($) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a static const message for each library-internal minor error message, so they aren't malloc'ed/free'd at init/term.

foreach $name (sort keys %major) {
print HEADER "/* $name */\n";
print HEADER " "x(0*$indent),"assert(H5I_INVALID_HID == ${name}_g);\n";
print HEADER " "x(0*$indent),"if((${name}_g = H5I_register(H5I_ERROR_MSG, &${name}_msg_s, false)) < 0)\n";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set up the major error IDs, to use the static variables.

for $name (sort @{$section_list{$sect_name}}) {
print HEADER "/* $name */\n";
print HEADER " "x(0*$indent),"assert(H5I_INVALID_HID == ${name}_g);\n";
print HEADER " "x(0*$indent),"if((${name}_g = H5I_register(H5I_ERROR_MSG, &${name}_msg_s, false)) < 0)\n";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set up the minor error IDs, to use the static variables.

@@ -2137,7 +2137,7 @@ H5Dformat_convert(hid_t dset_id)

/* Convert the dataset */
if (H5VL_dataset_optional(vol_obj, &vol_cb_args, H5P_DATASET_XFER_DEFAULT, H5_REQUEST_NULL) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_INTERNAL, FAIL, "can't convert dataset format");
HGOTO_ERROR(H5E_DATASET, H5E_CANTUPDATE, FAIL, "can't convert dataset format");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use minor error code instead of major.

@@ -2442,7 +2442,7 @@ H5Dchunk_iter(hid_t dset_id, hid_t dxpl_id, H5D_chunk_iter_op_t op, void *op_dat

/* Iterate over the chunks */
if ((ret_value = H5VL_dataset_optional(vol_obj, &vol_cb_args, dxpl_id, H5_REQUEST_NULL)) < 0)
HERROR(H5E_BADITER, H5E_BADITER, "error iterating over dataset chunks");
HERROR(H5E_DATASET, H5E_BADITER, "error iterating over dataset chunks");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use major error code instead of minor.

@@ -1495,7 +1495,7 @@ H5D_open(const H5G_loc_t *loc, hid_t dapl_id)
/* Check if dataset was already open */
if (NULL == (shared_fo = (H5D_shared_t *)H5FO_opened(dataset->oloc.file, dataset->oloc.addr))) {
/* Clear any errors from H5FO_opened() */
H5E_clear_stack(NULL);
H5E_clear_stack();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internal to the library, we always clear the default error stack, so update H5E_clear_stack() to do that.

/*
* HERROR macro, used to facilitate error reporting between a FUNC_ENTER()
* and a FUNC_LEAVE() within a function body. The arguments are the major
* error number, the minor error number, and a description of the error.
*/
#define HERROR(maj_id, min_id, ...) \
do { \
H5E_printf_stack(NULL, __FILE__, __func__, __LINE__, H5E_ERR_CLS_g, maj_id, min_id, __VA_ARGS__); \
H5E_printf_stack(__FILE__, __func__, __LINE__, maj_id, min_id, __VA_ARGS__); \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internal errors always use the default error stack and the library's error class.

@@ -88,6 +88,10 @@
* For pre-releases like \c snap0. Empty string for official releases.
*/
#define H5_VERS_SUBRELEASE ""
/**
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concise version string, for library's error class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved all the internal routines to the H5Eint.c file, to reflect current library coding style.

if (HDvasprintf(&tmp, fmt, ap) < 0)
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
/* Duplicate string information */
if (NULL == (tmp_file = strdup(file)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate strings and increment IDs for application error push operations. Do this here, in the API routine, instead of within the library.


/* Don't clear the error stack! :-) */
FUNC_ENTER_API_NOCLEAR(FAIL)

/* Duplicate string information */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as H5Epush2(): Duplicate strings and increment IDs for application error push operations. Do this here, in the API routine, instead of within the library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly all of the routines moved here are unmodified. I've added comments to the moved ones that changed.


/* Library's error class */
static const H5E_cls_t H5E_err_cls_s = {false, H5E_CLS_NAME, H5E_CLS_LIB_NAME, H5_VERS_STR};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autogenerated headers that define the major and minor error messages for the library. Used to register the library's error code IDs.

assert(err);

/* Free resources, if application registered this message */
if (err->app_msg)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only application error messages need to be freed, the library error messages are statically declared variables.

/* Copy new stack to current error stack */
current_stack->nused = estack->nused;
for (u = 0; u < current_stack->nused; u++)
if (H5E__copy_stack_entry(&current_stack->entries[u], &estack->entries[u]) < 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored all the routines that manipulate stack entries to use a new, common stack entry copy routine.

@@ -658,22 +1410,13 @@ H5E_printf_stack(H5E_t *estack, const char *file, const char *func, unsigned lin
va_start(ap, fmt);
va_started = true;

/* Use the vasprintf() routine, since it does what we're trying to do below */
if (HDvasprintf(&tmp, fmt, ap) < 0)
Copy link
Contributor Author

@qkoziol qkoziol May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than call vasprintf() here, then have H5E__push_stack() duplicate the string, just have H5E__push_stack() (H5E__set_stack_entry(), actually) call vasprintf().

FUNC_LEAVE_NOAPI(ret_value)
} /* end H5E_clear_stack() */

/*-------------------------------------------------------------------------
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is analogous to H5E_clear_stack(), but is internal to the H5E package, to allow clearing any error stack.

@qkoziol
Copy link
Contributor Author

qkoziol commented May 6, 2024

Can I get some reviews on this PR? I'd like to get this change to the H5E package in, so I can work on refactoring the H5E_clear_stack() calls out of the internal library code with this refactor underneath those updates.

src/H5Eint.c Outdated Show resolved Hide resolved
bin/make_err Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
Copy link
Collaborator

@jhendersonHDF jhendersonHDF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My perl isn't exactly strong, but everything looks reasonable to me

Signed-off-by: Quincey Koziol <quincey@koziol.cc>
Signed-off-by: Quincey Koziol <quincey@koziol.cc>
@qkoziol
Copy link
Contributor Author

qkoziol commented May 8, 2024

My perl isn't exactly strong, but everything looks reasonable to me

Super, thanks!

@derobins derobins merged commit 2b5769e into HDFGroup:develop May 9, 2024
58 checks passed
@qkoziol qkoziol deleted the h5e_refactor branch May 9, 2024 14:50
byrnHDF pushed a commit to byrnHDF/hdf5 that referenced this pull request May 14, 2024
…#4453)

All calls to the H5I routines are now made in API routines (sometimes in
FUNC_ENTER/LEAVE_* macros), except for some calls to H5E_clear_stack() within
the library, but I'm planning to remove those over time.

Also, made all the library internal error messages into static const variables,
instead of malloc'ing them, which means that they can just be referenced
and not copied.

Several new and updated auto-generated header files were necessary to enable
this.
lrknox pushed a commit to lrknox/hdf5 that referenced this pull request May 20, 2024
…#4453)

All calls to the H5I routines are now made in API routines (sometimes in
FUNC_ENTER/LEAVE_* macros), except for some calls to H5E_clear_stack() within
the library, but I'm planning to remove those over time.

Also, made all the library internal error messages into static const variables,
instead of malloc'ing them, which means that they can just be referenced
and not copied.

Several new and updated auto-generated header files were necessary to enable
this.
lrknox added a commit that referenced this pull request May 23, 2024
* win32defs: Fix Wundef warning (#4467)

* Refactor error handling code to eliminate internal ID calls (#4453)

All calls to the H5I routines are now made in API routines (sometimes in
FUNC_ENTER/LEAVE_* macros), except for some calls to H5E_clear_stack() within
the library, but I'm planning to remove those over time.

Also, made all the library internal error messages into static const variables,
instead of malloc'ing them, which means that they can just be referenced
and not copied.

Several new and updated auto-generated header files were necessary to enable
this.

* CMake: Fix mingw/fortran build (#4466)

* Update for blosc2 in plugins and prefix hdf5 cmake varnames (#4468)

* Fix an issue where compound datatype member IDs can be leaked during conversion (#4459)

Also fixes issues with handling of partially initialized datatypes during library shutdown

* H5Group: Fix operator= (#4473)

Closes #4472

* Fix github issue #2523: doxygen -- fix grammatically incorrect sentence alias (#4474)

* Remove env step not used by CI in testing (#4476)

* Add H5fortkit dependecy for H5Rff.F90 (#4482)

* Properly clean up cache when failing to load an object header (#4477)

* Properly clean up cache when failing to load an object header

* Don't check message type a second time in H5G__open_oid if the first attempt returns error

* Add more asserts to H5O__assert() to avoid segfaults

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>

* Add a missing image from the original document (#4490)

* Disable EOF checks for SWMR readers in more cases. (#4489)

Fixes a race condition where the reader opens the file and sets its EOF from the
file's size (from the stat() call in the driver open callback).  Then, before
the reader can read the file's superblock, a SWMR writer races in, extends the
file, and closes the file, writing an updated superblock with the 'writer' and
'SWMR writer' flags in the superblock off (appropriately).  Then the reader
proceeds to read the superblock, and flags the EOF as wrong.  Taking out the
check for the 'writer' and 'SWMR writer' flags will cause SWMR readers to avoid
flagging the file as incorrect.

* Remove unnecessary fortran install (#4498)

* Only one version of binaries is produced for platforms (#4496)

* Fix for github issue #2220. (#4497)

Document the limitation in the Passthrough Conncector section of the VOL Connector Author Guide.
The limitation is posted by Neil in the github issue on Dec 22, 2022.

* Release asset tarballs with no version filenames (#4494)

* Improve spec. reading superblock into cache (a little) by using v2 size (#4491)

* Improve spec. reading superblock into cache (a little) by using v2 size

Instead of reading the absolute minimal possible, use the likely value of
a v2+ superblock w/8-byte addresses & lengths.

* Fix for github Issue #1388 can't delete renamed dense attribute with corder tracking enabled (#4462)

* Fix for github issue #1388: can't delete renamed dense attribute with corder tracking enabled

The problem occurs in step 3(b) below which will delete the attribute with corder x
from the creation order index v2 B-tree.

The rename sequence in H5A__dense_rename() occurs in the following order:
1) The old attribute with corder x was removed from the creation order index v2 B-tree
2) The new renamed attribute was inserted via H5A__dense_insert():
(a) insert the attribute with new name j into the name index v2 B-tree
(b) insert the attribute with corder x into the creation order index v2 B-tree
3) The old attribute was removed via H5A__dense_remove():
(a) remove the attribute with old name k from the name index v2 B-tree
(b) remove the attribute with coorder x from the creation order index v2 B-tree

Fix: deactivate the "corder_bt2_addr" field so that H5A__dense_remove()
won't delete the attribute with corder x from the creation order index v2 B-tree.

* Fix/revert a libtool sed hack (#4501)

* Revert "Remove Autotools sed hack (#3848)"

This reverts commit 8b3ffde.

* Fix libtool sed cleanup on MacOS

Convert sed -i line to sed > libtool.bak && mv libtool.bak libtool
to avoid non-portable -i option.

* Update src/H5public.h

* Set H5 specific vars immediately if legacy find (#4512)

* Correct find process vars (vs in-line build)

* Correct SZIP find

* Everything is libaec 1.0.6 or newer

* Correct option help text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component - Build CMake, Autotools Component - C Library Core C library issues (usually in the src directory) Merge - To 1.14 This needs to be merged to HDF5 1.14 Priority - 1. High 🔼 These are important issues that should be resolved in the next release Type - Improvement Improvements that don't add a new feature or functionality
Projects
Status: Needs Merged
Development

Successfully merging this pull request may close these issues.

3 participants