SchOS/kernel/net
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Nexoniarz 6db9cc1230 net: closing a listening socket panicked the kernel
Ctrl-C on httpd took the whole machine down:

    lwip assert: invalid socket state for recv callback at tcp.c:2024
      #1 tcp_recv  #2 inet_close  #3 sockclose  #4 fileclose  #5 exit

tcp_listen() does not put a pcb into a listening state -- it frees the
connection pcb and returns a *different, smaller* structure, a
tcp_pcb_listen, which has no recv, sent, err or poll field at all. The two
are different types behind one `struct tcp_pcb *`, and the only callback a
listening one has is accept.

inet_close() cleared the callbacks identically for both, so on a listening
socket tcp_recv() wrote past the end of the allocation. lwIP asserts rather
than allowing that, and an assert here is a panic -- which means any program
that listened and then exited killed the kernel, because the close path runs
from exit(). A user process, ending normally, halting the system.

Nothing had ever done it: until cmd/httpd nothing in the tree listened on TCP
and exited. schdbgd would have found it a few days later.

inet_close() now branches on is->listening -- which was already there, one
field away, and the close path simply never asked. The listening case clears
accept and calls tcp_close (always ERR_OK for a LISTEN pcb, and tcp_abort
would be wrong on one: it reaches for connection state that does not exist).
The connected case also clears `sent`, which it had been leaving set.

Verified: Ctrl-C the listener, no panic, and a second httpd binds port 80
again -- which is the part that proves the pcb was actually freed. Six
requests after the change, all 200.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
2026-09-14 19:53:53 +02:00
..
lwip ldls: a second driver, and everything it found 2026-09-14 15:20:07 +02:00
inet.c net: closing a listening socket panicked the kernel 2026-09-14 19:53:53 +02:00
net.c userland: 268 applets, an editor, names that resolve, and a web server 2026-09-14 19:24:55 +02:00
netlink.c net: AF_NETLINK, so getifaddrs() works for everything and not just one program 2026-09-12 19:23:34 +02:00
unix.c debugger: the kernel can be asked a question 2026-09-14 00:58:45 +02:00