Skip to content

xftp-server: close stalled connections, reduce memory - #2058

Open
sh-agents wants to merge 36 commits into
simplex-chat:masterfrom
sh-agents:sh/xftp-inactive-clients-default
Open

sh-agents wants to merge 36 commits into
simplex-chat:masterfrom
sh-agents:sh/xftp-inactive-clients-default

Conversation

@sh-agents

@sh-agents sh-agents commented Nov 13, 2026 •

Copy link
Copy Markdown

Stalled XFTP connections and connections of vanished peers are closed, and costs of each request and connection are reduced.

Connections

  • xftp-server init writes [INACTIVE_CLIENTS] disconnect = on, ttl = 21600 and check_interval = 3600, as smp-server init does. In existing ini files, disconnect = off is kept; the limits below are applied with expiry on or off, except where expiry is named.
  • XFTP server sockets have TCP keep-alive (probes after 60 s idle, every 10 s) and a TCP_USER_TIMEOUT of 3 minutes. A connection is closed when neither keep-alive probes nor sent data are acknowledged by its peer for 3 minutes.
  • Reading and queuing a file response is limited by fileTimeout, as receiving a chunk is; the connection is closed when the limit is reached. A connection with a queued response that is not read by the client is closed by inactive-client expiry. Reading a request body is limited by fileTimeout, also in a repeated upload of a committed chunk.
  • A connection without a request within 60 s is closed, with expiry on or off.
  • In inactive-client expiry, the idle time is measured from the end of the last request, and connections with a request in progress are skipped, so long downloads are completed.
  • Closing a TLS connection is limited to 1 s, for SMP, NTF and XFTP servers and their clients.
  • Keep-alive and the user timeout are new TransportServerConfig fields, off by default; they are set for XFTP server sockets only, without ini settings. firstRequestTimeout (60 s by default) is a new field too, applied in runHTTP2Server.

Requests and background work

  • A received chunk is hashed while it is written, instead of being read back: in the server and in client downloads (receiveEncFile).
  • In FNEW and FADD, all recipients are added in one transaction (addRecipients, in both stores).
  • In file expiry, 0.1 s is waited only after a file is deleted from disk, and the PostgreSQL batch has at most limit rows.
  • Responses without a file are sent with responseBuilder instead of a streaming worker.
  • The daily, weekly and monthly download sets are reset also when log_stats is off, so their Prometheus counts are correct.

Operations

  • With xftp-server start --maintenance (-m), database migrations are applied, and the server is not started.
  • Socket counts are exported to Prometheus as simplex_xftp_sockets_*{port}.
  • Errors of sending a file are logged instead of printed (sendEncFile: XFTP server downloads and simplex-chat remote-control transfers).
  • xftp-server is linked with the RTS defaults -N -F1.2 -A16m -I0.01 -Iw15 -T -ki2k --disable-delayed-os-memory-return. Only the matching defaults are overridden by flags given at run time, so -ki2k and -T are applied in existing systemd units without changes.

Measurements

Builds of 46253a4 and of master 880d089 were measured with GHC 9.6.3 and -O2. The server and the clients ran on t3.medium (2 vCPU) instances on AWS, and the servers were started with +RTS -N -T -RTS and [INACTIVE_CLIENTS] disconnect = off. "master + RTS defaults" is master started with the RTS defaults of this branch given at run time.

Measurement master master + RTS defaults this branch
Live heap per idle connection after the handshake (connections 301–600, 2 runs) 223–226 kB 100 kB 103 kB
RSS per idle connection (connections 301–600, 2 runs) 271–276 kB 149–153 kB 173–175 kB
CPU per GB uploaded and downloaded (40 clients, files of 1–10 MiB, 4.61 GB per run, 3 runs) 23.8–24.5 s 23.1–23.6 s 23.1–23.3 s
GC share of CPU, same load 6.4–6.6% 5.1% 4.9%
Peak RSS, same load 170–173 MiB 142–143 MiB 143–144 MiB

All 300 idle connections of a client host whose packets were dropped were closed 170–185 s after the drop.

Tests

  • XFTP server:
    • never-uploaded chunks are expired without delays;
    • downloads longer than the inactivity ttl are completed;
    • daily download stats are reset without the stats log;
    • socket counts are exported;
    • connections are closed when a download or a request body is stalled, when no request is received, and, with inactive-client expiry, when the end of a download is stalled;
    • TIMEOUT is returned when a repeated upload of a committed chunk is stalled;
    • digests of received chunks are verified without the file being read back.
  • Transport: a TLS connection is closed while its peer does not read and data is sent; keep-alive and the user timeout are set on accepted sockets only when configured.
  • Stores: all recipients or none are added, in both stores; the PostgreSQL batch of expired files has at most limit rows.
  • CLI: with --maintenance, the server is not started, and migrations are applied with PostgreSQL.
  • Untested:
    • the XFTP CLI setting of keep-alive and the user timeout;
    • the logging of send errors;
    • receiveSbFile;
    • the PostgreSQL db_store_log record of added recipients;
    • the start of the XFTP, SMP and NTF stats logs with the shared delayUntilDayTime, and its wait until the next day;
    • the retry of FNEW and FADD on duplicate ids;
    • the padding error response;
    • the wait after a file deletion;
    • the RTS defaults.

closeTLS is also changed in #2059, with the same implementation; the PR merged second is updated by merging master.

/

@sh-agents sh-agents changed the title xftp-server: close dead connections, reduce memory xftp-server: close stalled connections, reduce memory Nov 19, 2026
@sh-agents
sh-agents marked this pull request as ready for review November 19, 2026 15:38

This branch has not been deployed

No deployments
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.

1 participant