Skip to content

fix(server): the client-gone check sees descriptors past 1023 - #218

Closed
jayleaton wants to merge 1 commit into
ashhart:mainfrom
jayleaton:pr0-cancel-fd
Closed

jayleaton wants to merge 1 commit into
ashhart:mainfrom
jayleaton:pr0-cancel-fd

Conversation

@jayleaton

Copy link
Copy Markdown
Contributor

What

socket_cancellation (shared by tensorfold.server.http and tensorfold.cuda.server) asks select() whether a
request's socket is readable. select() raises ValueError for a descriptor past FD_SETSIZE (1023), and the check
catches that as "still connected". So once a server holds more than about 1,000 descriptors (keep-alive clients,
checkpoint shards, NCCL sockets), a request whose client has left is never stopped: the engine decodes the whole
reply for nobody.

The check now asks poll() where the platform has it (Linux, macOS). Elsewhere it falls back to select():
Windows has no poll(), and its select() has no 1023 limit. For every descriptor select() could read, the answer
is the same. 0.6.1's portable MSG_DONTWAIT change on the next line is kept as it is.

Same bits

No reply changes. Only the disconnect check changes, and below descriptor 1024 it returns what it returned before.

Tests

  • tests/test_cuda_server_disconnect.py::test_the_socket_check_reads_descriptors_past_1023: a socket moved to
    descriptor 1050 reads as open, then as gone once its peer closes. On main it never reads as gone (checked). It
    is skipped without the resource module (Windows) or where the hard descriptor limit is below 1100.
  • Upstream CPU suite on this box (Python 3.14.7, torch 2.14.0+cpu, no GPU or MLX, pytest -n 4 tests): 3,128 passed /
    149 skipped / 134 failed / 13 errors, against 3,127 / 149 / 134 / 13 on clean main (v0.6.1). The failing ids are
    the same 147 on both, all from this environment (no MLX, no GPU).

Where it came from

We found it while moving our two-DGX-Spark GLM-5.3-Flash server
(jayleaton/glm53-tensorfold-spark) onto upstream's
server/cancellation.py. Our own copy already used poll(). We have not seen a server reach 1,024 descriptors in
production. This is a latent bug fixed with a test, not a reported outage.

🤖 Generated with Claude Code

socket_cancellation asked select() whether a request's socket was readable. select() raises ValueError for a
descriptor past FD_SETSIZE (1023), and the check caught it as "still connected", so a server holding more than
~1,000 descriptors (many keep-alive clients, open checkpoint shards, NCCL sockets) never stopped a request whose
client had left: it decoded the whole reply for nobody. The check now asks poll() where the platform has one (Linux,
macOS) and select() elsewhere (Windows' select() has no such limit). For every descriptor select() could read the
answer is the same, so both servers (tensorfold.server.http and tensorfold.cuda.server, which share the check)
behave as before below 1024.

No reply changes.

Tests: tests/test_cuda_server_disconnect.py: a socket moved to descriptor 1050 reads as open, then as gone once its
peer closes (before this change it never reads as gone; skipped without the resource module or where the
descriptor limit is below 1100).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@plotarmordev

Copy link
Copy Markdown
Contributor

Thanks @jayleaton. I ran this end to end on one DGX Spark (GB10), since the PR's test is the socket check alone: v0.6.1 with and without this commit (ccee0d5, on 17c73e1), a real Flash Next server (--parallel 1 --context 16384, Vontra MLX 4-bit), container started with --ulimit nofile=65536:65536. To push the request's socket past 1023 I opened 1,100 idle keep-alive connections first (each holds one server descriptor), then sent one request with ignore_eos and max_tokens 4000 and closed the client. /health then shows when requests_running drops to 0 and how many completion tokens were counted.

request idle connections server's top fd stopped after the client left tokens decoded
v0.6.1 non-streaming 0 427 0.25 s, 0.25 s 150, 177
v0.6.1 non-streaming 1,100 1,527 71.0 s, 64.2 s 4,000, 4,000
v0.6.1 streaming 1,100 1,527 0.25 s 64
+ #218 non-streaming 0 427 0.25 s, 0.26 s 152, 177
+ #218 non-streaming 1,100 1,527 0.25 s, 0.25 s 177, 177
+ #218 streaming 1,100 1,527 0.25 s 64

So on v0.6.1 the bug is real on the CUDA server: a non-streaming request whose socket is past 1023 decodes its whole reply after the client has gone, and with this change it stops at the next round, as below 1024. Streaming replies stop either way, since the next write to the closed socket fails, so in my opinion the exposure is non-streaming requests (and a stream's quiet stretches).

Two things that may help size it: an idle Flash Next server on this box already holds descriptors up to 427 (shards, CUDA, threads), and the container's default ulimit -n here is 1024. With that default the server can't reach 1024 at all (it would fail to accept first); with a raised limit, it takes about 600 open connections.

@plotarmordev

Copy link
Copy Markdown
Contributor

Follow-up to my run above, to cover what it left out: --parallel 2, two ranks, and the default descriptor limit. Same method: v0.6.1 with and without ccee0d5, idle keep-alive connections to push the request's socket past 1023, then a non-streaming ignore_eos request of 4,000 tokens whose client closes after 3 s.

One Spark, Flash Next, --parallel 2 (--ulimit nofile=65536:65536):

request idle connections server's top fd stopped after the client left tokens decoded
v0.6.1 non-streaming 0 427 0.26 s 160
v0.6.1 non-streaming 1,100 1,527 68.0 s, 64.6 s 4,000, 4,000
v0.6.1 streaming 1,100 1,527 0.25 s 64
+ #218 non-streaming 0 427 0.25 s 146
+ #218 non-streaming 1,100 1,527 0.26 s, 0.26 s 168, 174
+ #218 streaming 1,100 1,527 0.25 s 62

A greedy follow-up request after each one returned the same reply every time.

Two DGX Sparks, GLM-5.3-Flash EXL3 4 bpw (--tp 2 --context 16384, nofile raised on both ranks):

idle connections rank 0's top fd tokens counted for the departed client pair busy after the client left
v0.6.1 0 202 76 156.6 s
v0.6.1 1,100 1,299 4,000, 4,000 156.5 s, 156.7 s
+ #218 0 208 73 156.8 s
+ #218 1,100 1,299 77, 77 156.7 s, 156.9 s

On two ranks this PR does its part: rank 0 now sees the client leave past 1023 and ends the reply where it left (77 tokens, as below 1024). But the pair stays busy to max_tokens either way. That's the separate problem @pulseandthread describes and fixes in #194 (rank 1 can't see the client; the stop vote ends both ranks after the same round). In my opinion the time saving on two ranks needs both. Rank 1 logged no errors, and a follow-up request after each one returned the same reply in about 2 s.

The default limit, measured. Above I wrote that it takes about 600 open connections; that was arithmetic, so here it is measured. In a container with Docker's default ulimit -n 1024, an idle Flash Next server holds 427 descriptors. At about 600 idle connections it reaches 1,023, and from there it silently stops accepting: further connects queue and then time out, a request waits 30 s with no reply, and the server logs nothing. Once the idle connections close it answers again at once. So with the default limit this bug can't happen (the server stops accepting first); it needs a raised limit, as this PR's test assumes.

The PR's own test. tests/test_cuda_server_disconnect.py passes 18/18 with this change; with v0.6.1's cancellation.py the new test_the_socket_check_reads_descriptors_past_1023 fails on assert gone.cancelled (Linux container, nofile raised).

In my opinion #218 is merge ready. On GB10 it fixes what it says, at --parallel 1 and 2 and on two ranks, without changing replies or follow-up requests, and its test fails before and passes after. Not covered by me: the Mac server (server/http.py uses the same check; macOS has poll() too, but I didn't run it) and the Windows fallback (unchanged select()). On two ranks the time saving also needs #194.

@pulseandthread

Copy link
Copy Markdown

Thanks for running this on two Sparks, that 157 s pair-busy with #218 alone is the same signature I measured before the stop vote (343 s for an 8,192-token reply after the client left, free within 1 s with it). The two fixes don't overlap: #218 makes rank 0 notice the departed client past fd 1023, #194 makes rank 1 stop in the same round once rank 0 does. Both are needed on a pair.

#194 rebases onto 0.6.1 with two small conflicts (the shared tower helper and the two-rank chat header), resolved locally. I can push the rebased branch if that helps review.

@jayleaton

Copy link
Copy Markdown
Contributor Author

Cheers! Nice to see some more results.

Agreed on the summary: #218 makes rank 0 see the client leave past fd 1023, and on a pair the time saving also needs #194's stop vote so rank 1 ends in the same round. They don't overlap, and I'd be glad to see both land.

Your default-limit result is worth its own issue: at ~600 idle connections, the server stops accepting without logging anything. I'm happy to open one, or leave it with you since you measured it.

On Mac: uses the same check and macOS has poll(), but I haven't run it there either. If anyone has a Mac box handy, the PR's test should run as-is with a raised ulimit -n.

@pulseandthread: #219 (the communicator interface) touches the same GLM engine files as #194. It only changes how the two-rank communicator is opened (open_comm instead of NCCL(...)), and your stop vote goes through comm.all_gather, which #219 keeps. So the merge is mechanical whichever lands first, and I'm happy to rebase #219 onto #194.

@pulseandthread

Copy link
Copy Markdown

Sounds good, thanks. The stop vote only needs all_gather, which #219 keeps, so either order works.

@ashhart

ashhart commented Oct 2, 2026

Copy link
Copy Markdown
Owner

This is in TensorFold 0.6.2, with your commits under your name in the release history: https://github.com/ashhart/TensorFold/releases/tag/v0.6.2

Thank you, @jayleaton!

@plotarmordev

Copy link
Copy Markdown
Contributor

Opened #293 for the default-limit finding, thanks @jayleaton.

@jayleaton
jayleaton deleted the pr0-cancel-fd branch October 3, 2026 06:00
@ashhart ashhart added the landed Landed on main by the maintainers; the commits keep the contributor's authorship label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

landed Landed on main by the maintainers; the commits keep the contributor's authorship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants