Repository navigation
Conversation
When all time events have been lazy-deleted (id == AE_DELETED_EVENT_ID), the scan loop in usUntilEarliestTimer() leaves 'earliest' NULL. Subsequent dereference of earliest->when causes a crash. Add a NULL guard that returns -1 when no valid time events exist, consistent with the existing empty-list guard on line 307. The caller in aeProcessEvents() already handles -1 correctly. Fixes: valkey-io#4349 Signed-off-by: Yudistira Putra <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe event loop returns ChangesTimer deletion safety
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/unit/test_ae.cpp`:
- Line 31: Update the registered-time-event tests in src/unit/test_ae.cpp at
lines 31, 59, and 81: use aeProcessEvents with AE_TIME_EVENTS | AE_DONT_WAIT and
an inert file descriptor for the initial passive pass, then assert called1 and
called2 are both 1. Re-register the timers before the active pass and assert
each second callback executes, preventing aeApiPoll from blocking when no
descriptors or valid timers remain.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: bb50021f-a134-4f71-9af1-fa1ae4048b18
📒 Files selected for processing (2)
src/ae.csrc/unit/test_ae.cpp
There was a problem hiding this comment.
The src/ae.c guard is correct. Compiling ae.c with and without that one line and driving a loop whose only two time events were just lazy-deleted: the unpatched build segfaults on earliest->when in the next aeProcessEvents, the patched build completes. No concerns there.
The problem is the new unit test — as written, both cases block inside aeApiPoll, so make test-unit would hang instead of pass. Details inline.
| * Before the fix, this would dereference a NULL pointer and crash. | ||
| * After the fix, it should return -1 and the caller should handle it. | ||
| */ | ||
| aeProcessEvents(el, AE_TIME_EVENTS); |
There was a problem hiding this comment.
This call never returns. No file event is registered, so maxfd == -1 and, with AE_DONT_WAIT unset, aeProcessEvents takes the timer path at src/ae.c:441: usUntilEarliestTimer now returns -1 once every time event is deleted, tvp stays NULL, and aeApiPoll calls epoll_wait with a -1 timeout on an epoll set with no fds. I compiled both tests against the real ae.c and ran them — SurvivesWhenAllTimeEventsDeleted hangs indefinitely here, and HandlesMixedDeletedAndActiveTimers blocks ~1000 s at line 81 because noopProc reschedules 1000000 ms out. make test-unit never finishes either way.
Adding AE_DONT_WAIT is not the fix: src/ae.c:438 short-circuits before usUntilEarliestTimer is ever called, so the test then passes on unpatched ae.c too (verified) and covers nothing. What is needed is a poll that can return while still going through the timer path — e.g. a pipe left readable:
static void readableNoop(aeEventLoop *el, int fd, void *privdata, int mask) { ... }
int fds[2];
ASSERT_EQ(pipe(fds), 0);
ASSERT_EQ(write(fds[1], "x", 1), 1); /* keep fds[0] permanently readable */
ASSERT_NE(aeCreateFileEvent(el, fds[0], AE_READABLE, readableNoop, NULL), AE_ERR);
...
aeProcessEvents(el, AE_FILE_EVENTS | AE_TIME_EVENTS);With that shape both passes return immediately, usUntilEarliestTimer still runs, and the test segfaults on unpatched ae.c and passes with the fix — which is what you want from the regression test. Same change is needed at line 81.
| /* | ||
| * Test: ae event loop survives when all time events are deleted. | ||
| * | ||
| * This test verifies that aeProcessEvents() does not crash when | ||
| * all time events have been lazy-deleted (id == AE_DELETED_EVENT_ID). | ||
| * The bug was a NULL dereference in usUntilEarliestTimer() when the | ||
| * scan loop found no valid time events. | ||
| */ |
There was a problem hiding this comment.
Missing the standard file header — 29 of the 31 .cpp files in src/unit/ start with it.
| /* | |
| * Test: ae event loop survives when all time events are deleted. | |
| * | |
| * This test verifies that aeProcessEvents() does not crash when | |
| * all time events have been lazy-deleted (id == AE_DELETED_EVENT_ID). | |
| * The bug was a NULL dereference in usUntilEarliestTimer() when the | |
| * scan loop found no valid time events. | |
| */ | |
| /* | |
| * Copyright (c) Valkey Contributors | |
| * All rights reserved. | |
| * SPDX-License-Identifier: BSD-3-Clause | |
| */ | |
| /* | |
| * Test: ae event loop survives when all time events are deleted. | |
| * | |
| * This test verifies that aeProcessEvents() does not crash when | |
| * all time events have been lazy-deleted (id == AE_DELETED_EVENT_ID). | |
| * The bug was a NULL dereference in usUntilEarliestTimer() when the | |
| * scan loop found no valid time events. | |
| */ |
|
Thanks for the detailed review — the feedback was correct. The original test called I updated the test to register an always-readable pipe as a file event so each Verified locally:
|
The original test called aeProcessEvents with only AE_TIME_EVENTS, which blocks forever inside aeApiPoll when there are no file events and no valid timers (the poll has no timeout). Register an always-readable pipe as a file event so each aeProcessEvents call returns promptly. Verified: - Without the ae.c NULL guard, SurvivesWhenAllTimeEventsDeleted segfaults (exit 139). - With the guard, both tests pass (exit 0). Signed-off-by: Yudistira Putra <[email protected]>
65d96bb to
279c822
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
| @@ -0,0 +1,118 @@ | |||
| /* | |||
There was a problem hiding this comment.
I appreciate adding the unit test, but given this is untriggered code. I don't think we need this, I would just drop this file.
|
Thanks for pushing this forward, @Yudis-bit — and for linking #4349. For context: we reported this bug in #4349 with a full reproducer (PoC + gdb backtrace) and a regression test. Since the report, analysis and reproducer came from our investigation, we'd appreciate it if you could add us as co-signers to the commit: Signed-off-by: Haoze Xie [email protected] One thing worth the maintainers' attention: #4350 appeared 11 minutes after #4349 was filed, with a description closely mirroring the issue text, and its author has shown no engagement since — it also remains open despite the duplicate PRs, it also made us confused when it just opened 48 seconds earlier before our own pr #4351 (we opened it manually), thus making us closing our PR to avoid duplication. Whatever the explanation (the pattern is consistent with automated issue-watching), it may be worth consolidating on this PR and closing that one. Happy to help with additional review or testing if needed. |
|
Replaced by #4351. @Yudis-bit Thanks for your effort! There are multiple duplicate PRs fixing the same issue. |
Problem
When all time events in the event loop are lazy-deleted (id == AE_DELETED_EVENT_ID),
usUntilEarliestTimer()insrc/ae.cdereferences a NULLearliestpointer.Issue: #4349
Root Cause
The scan loop on lines 309-313 can leave
earliestNULL when every remaining time event hasid == AE_DELETED_EVENT_ID. The existing guard on line 307 handles the truly-empty list case (te == NULL), but the list can be non-empty yet contain only zombie events.Fix
Add a single-line NULL guard after the scan loop:
This is consistent with the existing empty-list guard on line 307 which also returns -1. The caller in
aeProcessEvents()already handles -1 correctly (line 441:if (usUntilTimer >= 0)).Regression Test
Added
src/unit/test_ae.cppwith two tests:SurvivesWhenAllTimeEventsDeleted— creates time events, deletes them all, verifies no crashHandlesMixedDeletedAndActiveTimers— mixed state of deleted and active timersVerification
make -j4succeedsLimitations
test_ae.cpp) is included but will be validated by CI