Repository navigation
Conversation
|
Updated 1:02 AM PT - Jul 15th, 2026
❌ @robobun, your commit e2445ab has 2 failures in
🧪 To try this PR locally: bunx bun-pr 33290That installs a local version of the PR into your bun-33290 --bun |
|
Found 1 issue this PR may fix:
🤖 Generated with Claude Code |
|
This does not fix #33103, so I'm leaving the #33103 is about a successful subscribe followed by Checked against the issue's scenario (scripted RESP3 server, successful subscribe, then Same behaviour either way. #33104 is the one that fixes #33103, by teaching The two do overlap on one symptom: a rejected SUBSCRIBE today also pins the loop, and #33104 would mask that for the |
WalkthroughThis PR defers SUBSCRIBE listener registration until server confirmation. It adds pending-subscription state to command queuing and send paths, snapshots requested channels before sending, wires confirmed subscriptions in the Valkey runtime, and expands tests around rejection, reconnect, and channel immutability. ChangesDeferred subscription confirmation
Compact metadata
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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/runtime/valkey_jsc/js_valkey_functions.rs`:
- Around line 1658-1661: The explanatory comment in the subscription flow is too
long and exceeds the 3-line code comment limit. Trim the block near
SubscriptionCtx::register_subscription / the listener wiring so it stays within
3 lines, and remove the last sentence about snapshotting since that point is
already clear from the code.
In `@test/js/valkey/valkey-subscribe-listener.test.ts`:
- Around line 72-163: The three subscribe listener tests are independent and
each uses its own confirmingRedis server on an ephemeral port, so they should be
marked concurrent instead of running sequentially. Update the test declarations
in valkey-subscribe-listener.test.ts to use test.concurrent (or wrap them in
describe.concurrent) for the cases around RedisClient.subscribe, keeping the
existing assertions and cleanup intact so the tests can run safely in parallel.
- Around line 72-163: Add coverage for the pending-SUBSCRIBE
close/never-confirmed path in the subscribe listener tests. The current tests in
valkey-subscribe-listener.test.ts only cover rejection before send and
channel-array mutation, so add a scripted server scenario where Redis accepts
SUBSCRIBE but never sends the confirmation (or closes mid-flight) and verify the
listener is not registered. Use the existing RedisClient subscribe flow and
assert that shutdown()/fail() leave the handler map clean with no duplicate
delivery or lingering listener.
🪄 Autofix (Beta)
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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 7d0a74ff-6850-49b1-8e57-c60fc338f7a0
📒 Files selected for processing (6)
src/runtime/valkey_jsc/ValkeyCommand.rssrc/runtime/valkey_jsc/js_valkey.rssrc/runtime/valkey_jsc/js_valkey_functions.rssrc/runtime/valkey_jsc/mod.rssrc/runtime/valkey_jsc/valkey.rstest/js/valkey/valkey-subscribe-listener.test.ts
Status: diff is green, CI red on unrelated lanesRebased onto The three hard failures are repo-wide, none touch
The warning-annotation flakes ( Not pushing further retriggers. Ready for a maintainer to merge past the red lanes. |
subscribe() added the listener to the receive-handler map before sending the SUBSCRIBE, with no way to remove it if the command failed. The listener outlived the rejected promise: a retry registered it a second time and delivered every message twice, and the non-empty handler map kept the event loop pinned so the process never exited. Carry the channels and listener on the command's promise pair instead, and register them from the subscribe confirmation. A SUBSCRIBE that is rejected or never answered now leaves the map untouched.
Add a test for a SUBSCRIBE that is still in flight when the connection closes: it must not leave its listener registered either. Run the tests in this file concurrently, and keep the subscribe() comment within the 3-line limit.
5b2c6aa to
e2445ab
Compare
…ener (#39547) The problem subscribe() on a client that has already failed (idle timeout, protocol error, any terminal close) still adds the listener to the subscription map before it tries to send SUBSCRIBE. The send is rejected with "Connection has failed", but the listener stays. A client with a listener counts as active, so it refs the event loop and holds a strong reference to itself. The process cannot exit. unsubscribe() cannot remove the listener either, because the client never entered subscriber mode. If the user calls connect() and subscribes again, the same listener is registered twice and every message is delivered twice. This came out of a post-merge review of #39511, #39513 and #38281. Those changes route idle timeouts and protocol errors through the failed state, so every onclose'd client is now a failed client. What changed The state check that send() applies to a command (`send_rejection()`, now shared with send() itself) runs at the top of subscribe(), before any listener is stored. A failed client, or a disconnected client with the offline queue off, gets the rejected promise a normal command would get, and no listener is stored. The check comes after the first dial, as it does for any other command. The dial that send() made for a client that had never connected is now a helper both send() and subscribe() call before they look at the client's state, so subscribe() on a fresh client with the offline queue off is rejected the way get() is, with the connection under way. A first dial that fails inside the call (a TLS context that cannot be built) fails the client before the check runs, so that subscribe() is rejected too and stores nothing. The dial itself comes after the two argument type checks (listener, channel), as for any other command, so subscribe(123, fn) on a fresh client throws without dialing. What this closes, and what it does not A SUBSCRIBE can be rejected on three routes, and every one of them leaves the same orphaned listener. This PR closes only the synchronous one: the client is already failed, or disconnected with the offline queue off, when subscribe() is called. Two routes stay open after this PR: - subscribe() before connect() with the default offline queue. The listener is stored and the SUBSCRIBE is queued; when the dial then fails for good, the queued command is rejected and the listener stays. Same for a SUBSCRIBE in flight when the socket closes. - The server answering SUBSCRIBE with -ERR (an ACL NOPERM under Redis 7 channel permissions). Only the promise is rejected; the listener stays. The fix for the class is to register the listener only when the server's subscribe confirmation arrives, which is what #33290 does. It will be rebased onto this stack after #39542 and closes both routes above. This PR keeps its pre-check so a failed client rejects without any work, and adds nothing that #33290 would replace. Visible changes subscribe() on a failed client rejects with ERR_REDIS_CONNECTION_CLOSED "Connection has failed", the same as any command, and leaves no listener behind. The process exits when nothing else holds it. subscribe() on a client that has never connected, with enableOfflineQueue: false, still starts the connection and is rejected with "Connection is closed and offline queue is disabled", as before, but no listener is left behind and the client comes up connected on its own. Tests Three tests in test/js/valkey/reliability/connection-failures.test.ts run against a net stub that answers HELLO. Two let the client fail on its idle timeout: one checks that subscribe() rejects, that the client is not in subscriber mode, and that a subscribe() after connect() delivers a message once; one spawns a process that does the same and checks that it exits on its own. The third never calls connect() and has the offline queue off: subscribe() rejects with the offline-queue message, the stub sees one connection, the client becomes connected on its own, and a subscribe then delivers once. On main the message arrives twice in both in-process tests and the spawned process is still running after three seconds (a debug-build figure; the budget is 15 s under ASAN). A test.todo next to them spawns a process whose first call is subscribe() against a dead host with default options and expects it to exit. It fails today and documents the queued route above; it turns green with #33290. A fourth test calls subscribe(123, fn) on a fresh client, then connects a second client to the stub and checks that the stub has seen one connection. Before 4cd8bb1 it saw two. Not in this PR Clearing the subscription map when a client closes for good (#33103), so a subscriber that fails or is closed does not keep the loop alive by itself. The failure reason that connect() and onclose report is being changed in #39542. <!-- robobun:evidence:begin --> --- **no test proof** · iteration 1 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/js/valkey/reliability/connection-failures.test.ts <!-- robobun:evidence:end --> --------- Co-authored-by: robobun <117481402+robobun@users.noreply.github.com>
|
Closing as part of a cleanup of stale pull requests. This PR has had no new commits since 2026-07-15, it conflicts with main, and its last CI run failed. This is not a judgment on the fix itself. If the problem still reproduces on a current build, reopen this PR after a rebase or open a new one against main. |
Repro
subscribe()registers its listener before theSUBSCRIBEis sent. If the command thenfails, the listener stays registered. Retry the subscribe and every message is delivered
twice; the now-permanently-populated handler map also keeps the event loop pinned.
Cause
JSValkeyClient::subscribecalledupsert_receive_handlerfor each channel up front, asthe source itself noted:
Nothing does roll it back. The handler map is what
has_subscriptions()reads, soupdate_poll_refkeeps the event-loop ref (and the strongthis_value) forever, and alater
subscribe()for the same channel appends the same listener to the channel'scallback array a second time.
Rolling back at the failure site is not enough either: the map stores one array of
listeners per channel, so removing "the listener this command added" cannot be told apart
from an identical listener that an earlier, confirmed subscribe added.
Fix
Carry the channels and the listener on the command's promise pair (
PendingSubscription,two
Strongrefs, boxed so it costs one pointer on every other command) and register themfrom the
subscribeconfirmation push instead. A SUBSCRIBE that is rejected, dropped by aclosing connection, or never answered simply drops the pair and leaves the map untouched.
The channel list is snapshotted into a fresh array at call time, since the caller's array is
no longer read synchronously.
This also removes the
clear_all_receive_handlers()call on the synchronous send-failurepath, which used to wipe out every other channel's listeners too.
Verification
Without the
src/change, three of the four fail: two with the repro's duplicate delivery(
["hi", "hi"]for one published message, once for a subscribe rejected before it is sentand once for a subscribe still in flight when the connection closes), and one with a pinned
event loop. The fourth is a regression guard for the channel snapshot.
Unchanged behaviour against a real redis
Single- and multi-channel subscribe, message delivery, multiple listeners per channel,
unsubscribe(channel, listener),unsubscribe(), subscribing before the connection is up,and back-to-back un-awaited subscribes all behave identically to
main, with onecorrection: two un-awaited
subscribe()calls now resolve with1then2(the realchannel counts) rather than
2then2, because the count is no longer inflated bylisteners whose SUBSCRIBE has not been confirmed yet.
Notes
Found while investigating a report that
await redis.subscribe(ch, cb)hangs forever whenthe server answers the SUBSCRIBE with
-ERR LOADING. That hang is a separate bug and isalready fixed by #32858, which I verified by building its branch and running the repro
against it. This PR fixes what #32858 leaves behind, and is independent of it (they touch
adjacent lines in
handle_subscribe_response, so whichever lands second needs a trivialrebase).
[review] gate passed · iteration 1 · 6 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 1 passed · 0 rejected · iteration 1
evidence per changed file