common: add (and use) status_unusual_once and status_broken_once helpers.
What changed, and why it matters
This commit is a simple code cleanup: it introduces two new helper macros that log a warning or error message only once, and replaces three existing hand-written 'log once' patterns with those helpers. There is no change to program logic, no security fix, and no behavior change beyond making the code easier to read and maintain.
No security action needed. This is a routine refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds status_unusual_once() and status_broken_once() macros in common/status.h. These wrap a static bool flag check around status_fmt(LOG_UNUSUAL/LOG_BROKEN, …). It then refactors three call sites in connectd/connectd.c, connectd/multiplex.c, and gossipd/gossmap_manage.c to use the new helpers. The generated code is functionally identical to the previous inline implementations.
Changed components
common/status.hconnectd/connectd.cconnectd/multiplex.cgossipd/gossmap_manage.cInspect captured patch +30 / −19
diff --git a/common/status.h b/common/status.h
index 95d51593..fb2cad77 100644
--- a/common/status.h
+++ b/common/status.h
@@ -50,6 +50,23 @@ void status_io(enum log_level iodir,
#define status_broken( ...) \
status_fmt(LOG_BROKEN, NULL, __VA_ARGS__)
+/* Common case of logging once, based on bool flag */
+#define status_unusual_once(flag, ...) \
+ do { \
+ if (*(flag) != true) { \
+ status_fmt(LOG_UNUSUAL, NULL, __VA_ARGS__); \
+ (*flag) = true; \
+ } \
+ } while(0)
+
+#define status_broken_once(flag, ...) \
+ do { \
+ if (*(flag) != true) { \
+ status_fmt(LOG_BROKEN, NULL, __VA_ARGS__); \
+ (*flag) = true; \
+ } \
+ } while(0)
+
/* For daemons which handle multiple peers */
#define status_peer_trace(peer, ...) \
status_fmt(LOG_TRACE, (peer), __VA_ARGS__)
diff --git a/connectd/connectd.c b/connectd/connectd.c
index 14dbb36b..4689ff74 100644
--- a/connectd/connectd.c
+++ b/connectd/connectd.c
@@ -646,11 +646,9 @@ static struct io_plan *connection_in(struct io_conn *conn,
/* Did we fail to accept? */
if (!conn) {
static bool accept_logged = false;
- if (!accept_logged) {
- status_broken("accepting incoming fd failed: %s",
- strerror(errno));
- accept_logged = true;
- }
+ status_broken_once(&accept_logged,
+ "accepting incoming fd failed: %s",
+ strerror(errno));
/* Maybe free up some fds by closing something. */
close_random_connection(daemon);
return NULL;
diff --git a/connectd/multiplex.c b/connectd/multiplex.c
index b81c3678..39404251 100644
--- a/connectd/multiplex.c
+++ b/connectd/multiplex.c
@@ -1626,12 +1626,10 @@ void peer_connect_subd(struct daemon *daemon, const u8 *msg, int fd)
* (subd will see immediate hangup). */
if (fd == -1) {
static bool recvfd_logged = false;
- if (!recvfd_logged) {
- status_broken("receiving lightningd fd failed for %s: %s",
- fmt_node_id(tmpctx, &id),
- strerror(errno));
- recvfd_logged = true;
- }
+ status_broken_once(&recvfd_logged,
+ "receiving lightningd fd failed for %s: %s",
+ fmt_node_id(tmpctx, &id),
+ strerror(errno));
/* Maybe free up some fds by closing something. */
close_random_connection(daemon);
return;
diff --git a/gossipd/gossmap_manage.c b/gossipd/gossmap_manage.c
index 3210f31f..67089e42 100644
--- a/gossipd/gossmap_manage.c
+++ b/gossipd/gossmap_manage.c
@@ -184,14 +184,12 @@ static bool map_add(struct cannounce_map *map,
{
/* More than 10000 pending things? Stop! */
if (map->count > 10000) {
- if (!map->flood_reported) {
- status_unusual("%s being flooded by %s: dropping some",
- map->name,
- pca->source_peer
- ? fmt_node_id(tmpctx, pca->source_peer)
- : "unknown");
- map->flood_reported = true;
- }
+ status_unusual_once(&map->flood_reported,
+ "%s being flooded by %s: dropping some",
+ map->name,
+ pca->source_peer
+ ? fmt_node_id(tmpctx, pca->source_peer)
+ : "unknown");
return false;
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.