From ca424832a7ce10a4a1e72c70ba235ac78cf980d4 Mon Sep 17 00:00:00 2001 From: Simon Rose Date: Wed, 11 Mar 2026 12:37:06 +0100 Subject: [PATCH] Block resizes from listener callbacks --- modules/libcom/src/error/errlog.c | 14 ++++++++++---- modules/libcom/src/error/errlog.h | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/libcom/src/error/errlog.c b/modules/libcom/src/error/errlog.c index 3ed887d40..657f8a609 100644 --- a/modules/libcom/src/error/errlog.c +++ b/modules/libcom/src/error/errlog.c @@ -77,6 +77,7 @@ typedef struct { } buffer_t; static struct { + epicsThreadId tid; epicsMutexId bufSizeLock; /* guarded by bufSizeLock, allowing them to be resized after initialisation */ size_t maxMsgSize; @@ -585,7 +586,6 @@ static void errlogInitPvt(void *arg) { struct initArgs *pconfig = (struct initArgs *) arg; - epicsThreadId tid = NULL; epicsThreadOpts topts = EPICS_THREAD_OPTS_INIT; topts.priority = epicsThreadPriorityLow; @@ -596,6 +596,7 @@ static void errlogInitPvt(void *arg) * cantProceed() calls us. */ + pvt.tid = NULL; pvt.errlogInitFailed = TRUE; pvt.bufSizeLock = epicsMutexCreate(); pvt.bufSize = pconfig->bufsize; @@ -629,11 +630,11 @@ static void errlogInitPvt(void *arg) && pvt.log->base && pvt.print->base ) { - tid = epicsThreadCreateOpt("errlog", (EPICSTHREADFUNC)errlogThread, 0, &topts); + pvt.tid = epicsThreadCreateOpt("errlog", (EPICSTHREADFUNC)errlogThread, 0, &topts); } - if (tid) { + if (pvt.tid) { pvt.errlogInitFailed = FALSE; - epicsAtExit(errlogExitHandler, tid); + epicsAtExit(errlogExitHandler, pvt.tid); } } @@ -699,6 +700,11 @@ int errlogInit2(int bufsize, int maxMsgSize) // Don't resize the buffer for any of the calls to errlogInit(0) if (bufsize == 0) return 0; + if (epicsThreadGetIdSelf() == pvt.tid) { + fprintf(stderr, "Cannot resize buffer from callback function.\n"); + return -1; + } + epicsMutexMustLock(pvt.bufSizeLock); if (pvt.bufSize != config.bufsize || pvt.maxMsgSize != config.maxMsgSize) { // We are resizing the buffer after initialisation diff --git a/modules/libcom/src/error/errlog.h b/modules/libcom/src/error/errlog.h index a204bec87..a608f0395 100644 --- a/modules/libcom/src/error/errlog.h +++ b/modules/libcom/src/error/errlog.h @@ -208,6 +208,7 @@ LIBCOM_API int errlogSetConsole(FILE *stream); * Can be used to initialize the error logging system with a larger buffer. The default buffer size is 2560 bytes. * * \param bufsize The desired buffer size + * \return 0 if successful, -1 if called from callback function * * \since UNRELEASED the default buffer size is 2560. */ @@ -219,6 +220,7 @@ LIBCOM_API int errlogInit(int bufsize); * * \param bufsize The desired buffer size * \param maxMsgSize The desired max message size + * \return 0 if successful, -1 if called from callback function * * \since UNRELEASED The default buffer size is 2560 and the default maximum message size is 512. */