Repository navigation
sp_net: add sp_net_recv_some_timeout, a read with a deadline - #1
Closed
baron-hines wants to merge 2 commits into
Closed
baron-hines wants to merge 2 commits into
baron-hines wants to merge 2 commits into
Conversation
sp_net_recv_some waits for the first byte with no limit, so a client whose server stops answering parks its green thread forever. The new call takes timeout_s and passes it to sp_sched_wait_io_timeout, the timed wait the scheduler already has, so the read and the deadline are one wait. A negative timeout waits as recv_some does. The recv uses MSG_DONTWAIT, so the deadline also holds on a blocking fd, and the unthreaded build polls in 1s slices as sp_net_wait_io does, still honoring the term flag. A timeout and EOF both return an empty string, so sp_net_take_recv_timeout answers whether the calling worker's last timed read gave up on its deadline. Reading it clears it, like sp_net_take_resolution_error. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
baron-hines
marked this pull request as ready for review
October 19, 2026 00:32
A recv that keeps returning EINTR retried with no clock check, so a signal storm could hold sp_net_recv_some_timeout past its deadline. Check the deadline before retrying, and report a timeout once it has passed. A wait that reported the fd ready still retries the recv: the bytes arrived in time, so they are returned. Also give the two new public functions a one-line comment at their definitions. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Before:
sp_net_recv_somewaits for the first byte with no limit. A client whose server stops answering (a Redis behind a dead network path, say) parks its green thread forever, and the only way to bound it is a separate readiness wait before every read.After:
sp_net_recv_some_timeout(fd, maxlen, timeout_s)reads with a deadline. It passestimeout_stosp_sched_wait_io_timeout, the timed wait the scheduler already has, so the read and its deadline are one wait.sp_net_take_recv_timeout()tells a timeout apart from EOF, since both return an empty string; reading it clears it, likesp_net_take_resolution_error.How:
timeout_swaits asrecv_somedoes;0is a peek.recvusesMSG_DONTWAIT, so the deadline also holds on a blocking fd. The deadline is onCLOCK_MONOTONICand is rechecked after every wake.SP_THREADSthe thread parks with the remaining time. The unthreaded build polls in 1 s slices, assp_net_wait_iodoes, so the term flag still ends the wait.recv_some,recv_alland the other existing calls are unchanged.Tests (both
# spinel: not-cruby, since they useffi_func):test/sp_net_recv_timeout.rb: on blocking UNIX-socket fds, silence times out after the deadline, a zero timeout peeks, waiting bytes come back and are not a timeout, a negative timeout waits, and a closed peer is EOF, not a timeout.test/sp_net_recv_timeout_threads.rb: two green threads park in timed reads; the main thread keeps running and answers one, which wakes with the data, while the other ends at its deadline.Why
This is for Roundhouse's Sidekiq support (rubys/roundhouse#1047). A Rails app compiled by Roundhouse can already push
perform_laterjobs to Redis in Sidekiq's format, through spinel-redis onsp_net. Next, the same binary will boot as a Sidekiq worker that takes jobs withBRPOP.Both paths read from Redis with
sp_net_recv_some, which has no deadline:BRPOPby design. Without a deadline of the server timeout plus a margin, a dead connection looks exactly like an empty queue, so the thread never reconnects and the worker silently stops taking jobs.spinel-redis could wait for readiness itself before every read, but that is a second wait in front of the one
sp_netalready does. rubys pointed out on #1047 that the scheduler already hassp_sched_wait_io_timeout, so the deadline belongs here: one call, used by spinel-redis forread_timeout:and by the worker'sBRPOP. Any othersp_netclient that talks to a server it doesn't control (HTTP clients, other databases) gets the same thing.🤖 Generated with Claude Code