Skip to content

sp_net: add sp_net_recv_some_timeout, a read with a deadline - #1

Closed
baron-hines wants to merge 2 commits into
masterfrom
sp-net-recv-timeout
Closed

baron-hines wants to merge 2 commits into
masterfrom
sp-net-recv-timeout

Conversation

@baron-hines

@baron-hines baron-hines commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

What this changes

Before: sp_net_recv_some waits 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 passes timeout_s to sp_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, like sp_net_take_resolution_error.

How:

  • A negative timeout_s waits as recv_some does; 0 is a peek.
  • The recv uses MSG_DONTWAIT, so the deadline also holds on a blocking fd. The deadline is on CLOCK_MONOTONIC and is rechecked after every wake.
  • Under SP_THREADS the thread parks with the remaining time. The unthreaded build polls in 1 s slices, as sp_net_wait_io does, so the term flag still ends the wait.
  • The timeout flag is per worker, like the receive buffer it describes.
  • recv_some, recv_all and the other existing calls are unchanged.

Tests (both # spinel: not-cruby, since they use ffi_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_later jobs to Redis in Sidekiq's format, through spinel-redis on sp_net. Next, the same binary will boot as a Sidekiq worker that takes jobs with BRPOP.

Both paths read from Redis with sp_net_recv_some, which has no deadline:

  • Pushes run inside web requests. If Redis stops answering mid-reply (a failover, or a network path that drops packets without a reset), the request's green thread parks forever. A real Sidekiq client gives up after redis-rb's read timeout and raises.
  • Worker threads block in BRPOP by 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_net already does. rubys pointed out on #1047 that the scheduler already has sp_sched_wait_io_timeout, so the deadline belongs here: one call, used by spinel-redis for read_timeout: and by the worker's BRPOP. Any other sp_net client that talks to a server it doesn't control (HTTP clients, other databases) gets the same thing.

🤖 Generated with Claude Code

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 baron-hines self-assigned this Oct 19, 2026
@github-actions github-actions Bot added the gate: no trailer The head commit carries no Gate trailer label Oct 19, 2026
@baron-hines
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gate: no trailer The head commit carries no Gate trailer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants