From 81f7161f2f56bf649d13926800bf7fb2d8d8e363 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 25 Nov 2023 21:11:10 +0100 Subject: [PATCH 1/4] Mark when entering atexit Python atexit handler --- bindings/python/dlite-misc-python.i | 5 +++++ bindings/python/dlite-misc.i | 7 +++++++ src/dlite-misc.c | 13 +++++++++++++ src/dlite-misc.h | 10 +++++----- src/dlite-storage.c | 2 +- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/bindings/python/dlite-misc-python.i b/bindings/python/dlite-misc-python.i index f3b595ed7..e25439b85 100644 --- a/bindings/python/dlite-misc-python.i +++ b/bindings/python/dlite-misc-python.i @@ -4,6 +4,11 @@ %pythoncode %{ +import atexit + +atexit.register(_mark_python_atexit) + + class errctl(): """Context manager for temporary disabling specific DLite error messages or redirecting them. diff --git a/bindings/python/dlite-misc.i b/bindings/python/dlite-misc.i index 666ae1853..9501b5605 100644 --- a/bindings/python/dlite-misc.i +++ b/bindings/python/dlite-misc.i @@ -129,6 +129,13 @@ Understands the following patterns: int globmatch(const char *pattern, const char *s); +%feature("docstring", "\ +Tell DLite that we are in a Python atexit handler. +") _mark_python_atexit; +%rename(_mark_python_atexit) dlite_globals_set_atexit; +void dlite_globals_set_atexit(void); + + %feature("docstring", "\ Clear the last error (setting its error code to zero). ") dlite_errclr; diff --git a/src/dlite-misc.c b/src/dlite-misc.c index 5b791733a..385d7a625 100644 --- a/src/dlite-misc.c +++ b/src/dlite-misc.c @@ -457,6 +457,7 @@ int dlite_add_dll_path(void) * Managing global state ********************************************************************/ +#define ATEXIT_MARKER_ID "atexit-marker-id" #define ERR_STATE_ID "err-globals-id" #define ERR_MASK_ID "err-ignored-id" @@ -588,6 +589,18 @@ int dlite_globals_in_atexit(void) return (dlite_globals_get_state(ATEXIT_MARKER_ID)) ? 0 : 1; } +/* + Mark that we are in an atexit handler. + */ +void dlite_globals_set_atexit(void) +{ + static void **dummy_ptr=NULL; + + /* Add an atexit marker used by dlite_blobals_in_atexit(). + The value of the state is not used. */ + session_add_state((Session *)_globals_handler, ATEXIT_MARKER_ID, + &dummy_ptr, NULL); +} /******************************************************************** diff --git a/src/dlite-misc.h b/src/dlite-misc.h index b1f086705..d52859dfa 100644 --- a/src/dlite-misc.h +++ b/src/dlite-misc.h @@ -14,11 +14,6 @@ #define DLITE_UUID_LENGTH 36 /*!< length of an uuid (excl. NUL-termination) */ -/** Special state id only used to indicate whether we are in an atexit - handler or not */ -#define ATEXIT_MARKER_ID "dlite-atexit-marker-id" - - /** @name General dlite utility functions @{ @@ -289,6 +284,11 @@ void *dlite_globals_get_state(const char *name); */ int dlite_globals_in_atexit(void); +/** + Mark that we are in an atexit handler. + */ +void dlite_globals_set_atexit(void); + /** @} */ diff --git a/src/dlite-storage.c b/src/dlite-storage.c index cf389b204..b5688315d 100644 --- a/src/dlite-storage.c +++ b/src/dlite-storage.c @@ -216,7 +216,7 @@ int dlite_storage_iter_next(DLiteStorage *s, void *iter, char *buf) void dlite_storage_iter_free(DLiteStorage *s, void *iter) { // Do not call iterFree() during atexit(), since it may lead to segfault - if (dlite_globals_get_state(ATEXIT_MARKER_ID)) return; + if (dlite_globals_in_atexit()) return; if (!s->api->iterFree) errx(1, "driver '%s' does not support iterFree()", s->api->name); From 737672703b898610464f7229dc5591e1e8d075d5 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sun, 26 Nov 2023 23:48:24 +0100 Subject: [PATCH 2/4] Improved error codes in storages --- src/dlite-storage.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/dlite-storage.c b/src/dlite-storage.c index b5688315d..6408d9b25 100644 --- a/src/dlite-storage.c +++ b/src/dlite-storage.c @@ -190,7 +190,8 @@ void dlite_storage_set_idflag(DLiteStorage *s, DLiteIDFlag idflag) void *dlite_storage_iter_create(DLiteStorage *s, const char *pattern) { if (!s->api->iterCreate) - return errx(1, "driver '%s' does not support iterCreate()", + return errx(dliteUnsupportedError, + "driver '%s' does not support iterCreate()", s->api->name), NULL; return s->api->iterCreate(s, pattern); } @@ -206,7 +207,8 @@ void *dlite_storage_iter_create(DLiteStorage *s, const char *pattern) int dlite_storage_iter_next(DLiteStorage *s, void *iter, char *buf) { if (!s->api->iterNext) - return errx(-1, "driver '%s' does not support iterNext()", s->api->name); + return errx(dliteUnsupportedError, + "driver '%s' does not support iterNext()", s->api->name); return s->api->iterNext(iter, buf); } @@ -219,7 +221,8 @@ void dlite_storage_iter_free(DLiteStorage *s, void *iter) if (dlite_globals_in_atexit()) return; if (!s->api->iterFree) - errx(1, "driver '%s' does not support iterFree()", s->api->name); + errx(dliteUnsupportedError, + "driver '%s' does not support iterFree()", s->api->name); else s->api->iterFree(iter); } From dece31374e4d7cd99f838de6d415fcb5de375d72 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sun, 26 Nov 2023 23:57:44 +0100 Subject: [PATCH 3/4] Revert all changes but improved error codes to origin/master --- bindings/python/dlite-misc-python.i | 5 ----- bindings/python/dlite-misc.i | 7 ------- src/dlite-misc.c | 13 ------------- src/dlite-misc.h | 10 +++++----- 4 files changed, 5 insertions(+), 30 deletions(-) diff --git a/bindings/python/dlite-misc-python.i b/bindings/python/dlite-misc-python.i index e25439b85..f3b595ed7 100644 --- a/bindings/python/dlite-misc-python.i +++ b/bindings/python/dlite-misc-python.i @@ -4,11 +4,6 @@ %pythoncode %{ -import atexit - -atexit.register(_mark_python_atexit) - - class errctl(): """Context manager for temporary disabling specific DLite error messages or redirecting them. diff --git a/bindings/python/dlite-misc.i b/bindings/python/dlite-misc.i index 9501b5605..666ae1853 100644 --- a/bindings/python/dlite-misc.i +++ b/bindings/python/dlite-misc.i @@ -129,13 +129,6 @@ Understands the following patterns: int globmatch(const char *pattern, const char *s); -%feature("docstring", "\ -Tell DLite that we are in a Python atexit handler. -") _mark_python_atexit; -%rename(_mark_python_atexit) dlite_globals_set_atexit; -void dlite_globals_set_atexit(void); - - %feature("docstring", "\ Clear the last error (setting its error code to zero). ") dlite_errclr; diff --git a/src/dlite-misc.c b/src/dlite-misc.c index 385d7a625..5b791733a 100644 --- a/src/dlite-misc.c +++ b/src/dlite-misc.c @@ -457,7 +457,6 @@ int dlite_add_dll_path(void) * Managing global state ********************************************************************/ -#define ATEXIT_MARKER_ID "atexit-marker-id" #define ERR_STATE_ID "err-globals-id" #define ERR_MASK_ID "err-ignored-id" @@ -589,18 +588,6 @@ int dlite_globals_in_atexit(void) return (dlite_globals_get_state(ATEXIT_MARKER_ID)) ? 0 : 1; } -/* - Mark that we are in an atexit handler. - */ -void dlite_globals_set_atexit(void) -{ - static void **dummy_ptr=NULL; - - /* Add an atexit marker used by dlite_blobals_in_atexit(). - The value of the state is not used. */ - session_add_state((Session *)_globals_handler, ATEXIT_MARKER_ID, - &dummy_ptr, NULL); -} /******************************************************************** diff --git a/src/dlite-misc.h b/src/dlite-misc.h index d52859dfa..b1f086705 100644 --- a/src/dlite-misc.h +++ b/src/dlite-misc.h @@ -14,6 +14,11 @@ #define DLITE_UUID_LENGTH 36 /*!< length of an uuid (excl. NUL-termination) */ +/** Special state id only used to indicate whether we are in an atexit + handler or not */ +#define ATEXIT_MARKER_ID "dlite-atexit-marker-id" + + /** @name General dlite utility functions @{ @@ -284,11 +289,6 @@ void *dlite_globals_get_state(const char *name); */ int dlite_globals_in_atexit(void); -/** - Mark that we are in an atexit handler. - */ -void dlite_globals_set_atexit(void); - /** @} */ From 3cbc5096f34e2de9e021c873652911d2ea3d5a8f Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Mon, 27 Nov 2023 00:27:00 +0100 Subject: [PATCH 4/4] Reverted call to dlite_globals_in_atexit() --- src/dlite-storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dlite-storage.c b/src/dlite-storage.c index 6408d9b25..a3bdbad53 100644 --- a/src/dlite-storage.c +++ b/src/dlite-storage.c @@ -218,7 +218,7 @@ int dlite_storage_iter_next(DLiteStorage *s, void *iter, char *buf) void dlite_storage_iter_free(DLiteStorage *s, void *iter) { // Do not call iterFree() during atexit(), since it may lead to segfault - if (dlite_globals_in_atexit()) return; + if (dlite_globals_get_state(ATEXIT_MARKER_ID)) return; if (!s->api->iterFree) errx(dliteUnsupportedError,