fix: drain signal eventfd after pop to prevent CPU spin; fix all warnings

- Drain signal queue eventfd (fds[0]) after each pop attempt.
  Without this, the eventfd counter stays >0 forever after the first
  signal, causing poll() to return immediately every iteration (100% CPU).
- Fix strncpy truncation warnings in fill_handler.c (use memcpy+nullterm)
- Fix write/read return value warnings in fill_handler.c, log.c
- Fix misleading indentation in http_client.c (add line breaks after if)
This commit is contained in:
nicolas 2026-05-28 15:43:43 -03:00
parent b8d6499c33
commit 2c3796005b
5 changed files with 39 additions and 12 deletions

7
AGENTS.md Normal file
View File

@ -0,0 +1,7 @@
Project Rules
Git Operations:
Do NOT run any git commands (commit, push, merge, rebase, checkout, etc.) without explicit approval. Always present the intended git operation and wait for confirmation before executing.
File Modifications:
Do NOT modify, create, or delete any files without explicit approval. When changes are needed, present the proposed changes and wait for confirmation before writing.

View File

@ -254,6 +254,14 @@ void *event_executor_thread(void *arg) {
if (read(fds[1].fd, &val, sizeof(val)) < 0) {} if (read(fds[1].fd, &val, sizeof(val)) < 0) {}
} }
/* Drain signal eventfd — reset counter so poll() can block.
Must happen AFTER the pop attempt so any signal arriving during
pop is not drained away (it will be caught next poll iteration). */
if (fds[0].revents & POLLIN) {
uint64_t val;
if (read(fds[0].fd, &val, sizeof(val)) < 0) {}
}
/* Keepalive: warm up REST connection every 30s */ /* Keepalive: warm up REST connection every 30s */
if (now_ka - last_keepalive_ms >= 30000 || last_keepalive_ms == 0) { if (now_ka - last_keepalive_ms >= 30000 || last_keepalive_ms == 0) {
executor_keepalive(exec); executor_keepalive(exec);

View File

@ -64,7 +64,7 @@ bool fill_channel_push(fill_channel_t *ch, const fill_event_t *ev) {
atomic_store_explicit(&ch->tail, next, memory_order_release); atomic_store_explicit(&ch->tail, next, memory_order_release);
uint64_t one = 1; uint64_t one = 1;
write(ch->wake_fd, &one, sizeof(one)); if (write(ch->wake_fd, &one, sizeof(one)) < 0) {}
return true; return true;
} }
@ -106,8 +106,12 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid,
total_funds += ev.match_price * ev.match_size; total_funds += ev.match_price * ev.match_size;
total_fee += ev.match_fee; total_fee += ev.match_fee;
match_count++; match_count++;
if (!order_id[0] && ev.order_id[0]) if (!order_id[0] && ev.order_id[0]) {
strncpy(order_id, ev.order_id, sizeof(order_id) - 1); size_t ol = strlen(ev.order_id);
if (ol >= sizeof(order_id)) ol = sizeof(order_id) - 1;
memcpy(order_id, ev.order_id, ol);
order_id[ol] = '\0';
}
if (ev.is_terminal && match_count > 0) { if (ev.is_terminal && match_count > 0) {
out->total_size = total_size; out->total_size = total_size;
@ -115,7 +119,10 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid,
out->total_fee = total_fee; out->total_fee = total_fee;
out->avg_price = total_size > 0 ? total_funds / total_size : 0; out->avg_price = total_size > 0 ? total_funds / total_size : 0;
out->match_count = match_count; out->match_count = match_count;
strncpy(out->order_id, order_id, sizeof(out->order_id) - 1); size_t ol = strlen(order_id);
if (ol >= sizeof(out->order_id)) ol = sizeof(out->order_id) - 1;
memcpy(out->order_id, order_id, ol);
out->order_id[ol] = '\0';
return true; return true;
} }
} }
@ -128,7 +135,7 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid,
int ret = poll(&pfd, 1, remaining); int ret = poll(&pfd, 1, remaining);
if (ret > 0) { if (ret > 0) {
uint64_t val; uint64_t val;
read(ch->wake_fd, &val, sizeof(val)); if (read(ch->wake_fd, &val, sizeof(val)) < 0) {}
} }
} }
@ -139,7 +146,10 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid,
out->total_fee = total_fee; out->total_fee = total_fee;
out->avg_price = total_size > 0 ? total_funds / total_size : 0; out->avg_price = total_size > 0 ? total_funds / total_size : 0;
out->match_count = match_count; out->match_count = match_count;
strncpy(out->order_id, order_id, sizeof(out->order_id) - 1); size_t ol = strlen(order_id);
if (ol >= sizeof(out->order_id)) ol = sizeof(out->order_id) - 1;
memcpy(out->order_id, order_id, ol);
out->order_id[ol] = '\0';
} }
return false; return false;
} }

View File

@ -420,9 +420,11 @@ char *https_post_auth(const char *host, int port, const char *path,
while (*p && !(*p == '0' && (p[1] == '\r' || p[1] == '\n'))) { while (*p && !(*p == '0' && (p[1] == '\r' || p[1] == '\n'))) {
int cl = 0; int cl = 0;
while (*p && *p != '\r' && *p != '\n') { char h = *p; cl <<= 4; cl += (h >= '0' && h <= '9') ? (h - '0') : ((h & 0x1f) + 9); p++; } while (*p && *p != '\r' && *p != '\n') { char h = *p; cl <<= 4; cl += (h >= '0' && h <= '9') ? (h - '0') : ((h & 0x1f) + 9); p++; }
if (*p == '\r') p++; if (*p == '\n') p++; if (*p == '\r') p++;
if (*p == '\n') p++;
if (cl > 0 && op + cl < HTTP_BUFFER_SIZE - 1) { memcpy(out + op, p, cl); op += cl; p += cl; } if (cl > 0 && op + cl < HTTP_BUFFER_SIZE - 1) { memcpy(out + op, p, cl); op += cl; p += cl; }
if (*p == '\r') p++; if (*p == '\n') p++; if (*p == '\r') p++;
if (*p == '\n') p++;
} }
out[op] = '\0'; out[op] = '\0';
free(resp); free(resp);

View File

@ -37,9 +37,9 @@ static void *log_worker(void *arg) {
while (atomic_load(&log_running)) { while (atomic_load(&log_running)) {
ssize_t n = read(log_pipe[0], buf, sizeof(buf)); ssize_t n = read(log_pipe[0], buf, sizeof(buf));
if (n > 0) { if (n > 0) {
write(STDERR_FILENO, buf, (size_t)n); if (write(STDERR_FILENO, buf, (size_t)n) < 0) {}
if (log_file_fd >= 0) if (log_file_fd >= 0)
write(log_file_fd, buf, (size_t)n); if (write(log_file_fd, buf, (size_t)n) < 0) {}
} else if (n < 0) { } else if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
usleep(100); usleep(100);
@ -111,7 +111,7 @@ void log_write(const char *fmt, ...) {
int total = ts_len + (msg_len > 0 ? msg_len : 0); int total = ts_len + (msg_len > 0 ? msg_len : 0);
if (total > 0) { if (total > 0) {
write(log_pipe[1], buf, (size_t)total); if (write(log_pipe[1], buf, (size_t)total) < 0) {}
} }
} }
@ -140,6 +140,6 @@ void log_write_screen(const char *fmt, ...) {
int total = ts_len + (msg_len > 0 ? msg_len : 0); int total = ts_len + (msg_len > 0 ? msg_len : 0);
if (total > 0) { if (total > 0) {
write(STDERR_FILENO, buf, (size_t)total); if (write(STDERR_FILENO, buf, (size_t)total) < 0) {}
} }
} }