Skip to content

server: don't let a stalled AGWPE client block the rest of Dire Wolf - #671

Merged
wb2osz merged 2 commits into
wb2osz:devfrom
elafargue:agwpe-nonblocking-send
Aug 13, 2026
Merged

wb2osz merged 2 commits into
wb2osz:devfrom
elafargue:agwpe-nonblocking-send

Conversation

@elafargue

Copy link
Copy Markdown

Context: when working on BBS2 development, I ran into a lot of situations where Direwolf would mysteriously stall and get into a bad state (see error messages below). After a lot of manual investigations, I realized that this was caused by the fact I often used the paracon terminal which connects over AGWPE, through a SSH session to my Raspberry Pi, and that I usually closed my laptop after working with it without always making sure I would terminate the session properly.

With that knowledge in hand, I enrolled the help of Claude.ai to get to the bottom of the issue and find a way to reliably reproduce the issue as well as fix it. Below is the result of the investigation and a proposed fix. This has been running on my own station for about 2 months without a single Direwolf internal error, despite the fact I still tend to be a bit too casual when terminating sessions ;)

I don't want to pollute the repository with very specific test harnesses, but I do have a set of scripts that validate the "before" and "after" this commit behaviors on Direwolf, available on request, you should probably try them before considering a merge into dev.


Root cause: every AGWPE client write went through a plain blocking send() on the caller's thread. If a client stopped reading, that send() blocked, and whichever thread was in it stopped doing its real job.

Two callers make this serious:

  • recv_process() is the single DLQ consumer thread. It does not only feed AGWPE clients -- the same loop drives dl_timer_expiry(), lm_data_indication(), dl_connect_request(), lm_channel_busy() and the rest of the connected mode state machine. Blocking there stops all AX.25 link layer timers, so established connections time out and drop, and the DLQ grows without bound. That produces the familiar cascade:

    Received frame queue is out of control. Length=...
    INTERNAL ERROR:  DLQ memory leak, new=..., delete=...
    Report to WB2OSZ - Memory leak for packet objects.
    
  • server_send_monitored() is also called from send_one_frame() on the xmit thread, between ptt_set(OCTYPE_PTT, chan, 1) and the matching ptt_set(..., 0). A stalled client with monitoring enabled could therefore block the transmit thread with PTT still asserted, holding the transmitter keyed.

The worst case is a client whose TCP connection is black holed rather than closed -- an AGWPE terminal over ssh when the link drops, a laptop that sleeps, a NAT entry that gets evicted. No FIN or RST ever arrives, keepalive is off by default, so send() waits on the retransmission timeout: about 15 minutes on Linux with the default tcp_retries2. A client that merely crashes sends a RST and fails fast, which is why this only ever showed up on flaky links.

Fix: decouple frame generation from socket I/O.

  • Per-client output queue drained by a dedicated client_send_thread(), so no caller ever blocks on a client socket. Bounded at 512 items (~150 KB); beyond that frames are dropped for that client only.
  • SO_SNDTIMEO of 10 seconds on each accepted socket, so a black holed connection is detected in 10 seconds instead of ~15 minutes.
  • Because SO_SNDTIMEO makes a short send() count possible, a partial write is now treated as an error. AGWPE is a length prefixed stream with no resync, so a truncated frame would desynchronize the client for the rest of the session; dropping the connection is the only safe response.
  • The "send queue full" warning uses hysteresis: it re-arms only once the queue has drained to half the limit. Resetting it on every successful enqueue would re-arm it each time a single frame drained, so a client parked at the limit would flood the console with the very warning meant to be occasional.
  • client_disconnect() replaces five scattered inline disconnect blocks. It swaps out client_sock under the lock, so it is idempotent and dlq_client_cleanup() runs exactly once no matter which thread notices the failure first. It also drains the queue, so a new client can never inherit frames left over from the previous occupant of the slot.

This also removes two WSACleanup() calls that were in the per-client send error paths. WSACleanup() deinitializes Winsock for the whole process, so on Windows a single failed write to one AGWPE client tore down every socket in Dire Wolf -- AGWPE, KISS over TCP and IGate alike. The four legitimate calls in connect_listen_thread are left alone.

Measured with a 9600 baud feed and a client that enables 'k' and 'm', reads for 3 seconds, then stops reading for 40:

before: decoding stops completely after ~15 s and has not resumed
7 s after the client finally closes the socket. 12598
packets decoded, and 188536 lines of the cascade above.
after: decode rate is flat across the entire stall. 58318 packets
decoded, 2 "send queue full" warnings, client disconnected.

Every AGWPE client write went through a plain blocking send() on the
caller's thread.  If a client stopped reading, that send() blocked, and
whichever thread was in it stopped doing its real job.

Two callers make this serious:

* recv_process() is the single DLQ consumer thread.  It does not only
  feed AGWPE clients -- the same loop drives dl_timer_expiry(),
  lm_data_indication(), dl_connect_request(), lm_channel_busy() and the
  rest of the connected mode state machine.  Blocking there stops all
  AX.25 link layer timers, so established connections time out and drop,
  and the DLQ grows without bound.  That produces the familiar cascade:

      Received frame queue is out of control. Length=...
      INTERNAL ERROR:  DLQ memory leak, new=..., delete=...
      Report to WB2OSZ - Memory leak for packet objects.

* server_send_monitored() is also called from send_one_frame() on the
  xmit thread, between ptt_set(OCTYPE_PTT, chan, 1) and the matching
  ptt_set(..., 0).  A stalled client with monitoring enabled could
  therefore block the transmit thread with PTT still asserted, holding
  the transmitter keyed.

The worst case is a client whose TCP connection is black holed rather
than closed -- an AGWPE terminal over ssh when the link drops, a laptop
that sleeps, a NAT entry that gets evicted.  No FIN or RST ever arrives,
keepalive is off by default, so send() waits on the retransmission
timeout: about 15 minutes on Linux with the default tcp_retries2.  A
client that merely crashes sends a RST and fails fast, which is why this
only ever showed up on flaky links.

Fix: decouple frame generation from socket I/O.

* Per-client output queue drained by a dedicated client_send_thread(),
  so no caller ever blocks on a client socket.  Bounded at 512 items
  (~150 KB); beyond that frames are dropped for that client only.
* SO_SNDTIMEO of 10 seconds on each accepted socket, so a black holed
  connection is detected in 10 seconds instead of ~15 minutes.
* Because SO_SNDTIMEO makes a short send() count possible, a partial
  write is now treated as an error.  AGWPE is a length prefixed stream
  with no resync, so a truncated frame would desynchronize the client
  for the rest of the session; dropping the connection is the only safe
  response.
* The "send queue full" warning uses hysteresis: it re-arms only once
  the queue has drained to half the limit.  Resetting it on every
  successful enqueue would re-arm it each time a single frame drained,
  so a client parked at the limit would flood the console with the very
  warning meant to be occasional.
* client_disconnect() replaces five scattered inline disconnect blocks.
  It swaps out client_sock under the lock, so it is idempotent and
  dlq_client_cleanup() runs exactly once no matter which thread notices
  the failure first.  It also drains the queue, so a new client can
  never inherit frames left over from the previous occupant of the slot.

This also removes two WSACleanup() calls that were in the per-client
send error paths.  WSACleanup() deinitializes Winsock for the whole
process, so on Windows a single failed write to one AGWPE client tore
down every socket in Dire Wolf -- AGWPE, KISS over TCP and IGate alike.
The four legitimate calls in connect_listen_thread are left alone.

Measured with a 9600 baud feed and a client that enables 'k' and 'm',
reads for 3 seconds, then stops reading for 40:

  before:  decoding stops completely after ~15 s and has not resumed
           7 s after the client finally closes the socket.  12598
           packets decoded, and 188536 lines of the cascade above.
  after:   decode rate is flat across the entire stall.  58318 packets
           decoded, 2 "send queue full" warnings, client disconnected.
@wb2osz

wb2osz commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Thank you for all your effort researching this issue and finding a solution.
It will take me a little while to study this and get comfortable with it.
We had a similar issue with the KISS pseudo terminal. If there was not a client app reading from it, the write would block eventually. The solution was to make the device non-blocking so the write would get an error if the outgoing buffers were full. However, there is a single attribute for non-blocking in both directions. The read then became non-blocking. As I recall, a select was put in front of it to wait until incoming data was available.

@elafargue

Copy link
Copy Markdown
Author

Yes, that' s a bit of an edge case but with bad consequence when it happens. If that's OK I'll reach out directly with the test package so that you can verify independently?

@wb2osz

wb2osz commented Aug 13, 2026

Copy link
Copy Markdown
Owner

We have a similar situation in #620 where a TCP KISS client app was not reading the data quickly enough. The write to the client app socket eventually blocked. The DLQ queue for received frames (and other events) started growing out of control because it was not being drained.
As an experiment, I added a config file option to set the TCP send buffer size (tcp_wmem) for TCP KISS only. Increasing the buffer size improved the situation but did not solve the root problem that the socket write could block.
To be continued...

@elafargue

Copy link
Copy Markdown
Author

That does look like a similar issue indeed. This patch helps by making sure “bad” clients block nothing, and stalled clients are gracefully closed eventually since, well, they are stalled. I have had this running for a couple of months without errors on my own station. Did you get my test scripts? I can also add then on a separate commit if you want.

@elafargue

elafargue commented Aug 13, 2026 •

Copy link
Copy Markdown
Author

I added the test Harness to the PR as a separate commit, let me know if you manage to reproduce on your end! We can keep that one for future regression tests, not sure about the best location for it in the repo...

The tests cover:

  • What happens if an AGWPE client "stalls" and stops reading from its tcp socket
  • What happens if two clients are connected and one stalls (spoiler: both clients end up stalling)

@elafargue
elafargue force-pushed the agwpe-nonblocking-send branch from 59308ba to ba77034 Compare August 13, 2026 21:02
and test that the commit solves it.

We can move this to a better location before merge if needed.
@elafargue
elafargue force-pushed the agwpe-nonblocking-send branch from ba77034 to 2b5b503 Compare August 13, 2026 22:10
@wb2osz
wb2osz merged commit 9acba4c into wb2osz:dev Aug 13, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants