Skip to content

FTP: bound the active-mode connect to 5 s - #1068

Merged
chrisgleissner merged 5 commits into
GideonZ:masterfrom
JC-000:fix/ftp-active-connect
Nov 11, 2026
Merged

chrisgleissner merged 5 commits into
GideonZ:masterfrom
JC-000:fix/ftp-active-connect

Conversation

@JC-000

@JC-000 JC-000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1057

Problem

In FTP active mode the server connects back to the client's PORT address. If that address does not answer, the connect blocks the FTP control task for the full TCP connect timeout (~18 s) before failing, even though the code intends a 5 s bound. During that time the control connection is unresponsive.

FTPDataConnection::connect_to (software/network/ftpd.cc) sets the socket's SO_RCVTIMEO/SO_SNDTIMEO to 5 s and then calls blocking connect(). lwIP's blocking connect() ignores those timeouts and waits for TCP to give up on its own, so the 5 s bound has no effect.

What changed and why

The blocking connect is replaced by a non-blocking connect plus a bounded select() (ftp_connect_bounded): put the socket in non-blocking mode, start the connect, wait at most 5 s for it to become writable, check SO_ERROR, then restore blocking mode. An unreachable data address now fails in 5 s.

Products. Runs on the Ultimate 64, Ultimate 64 Elite II, C64 Ultimate and the Ultimate-II cartridges (U2+, U2+L). Tested on the Ultimate 64 Elite and the C64 Ultimate.

How I tested it

There is no host test: a Linux host honours SO_SNDTIMEO on connect and lwIP does not, so a host run cannot show the defect. The fix rests on the hardware measurement below.

Hardware.

Device Firmware Ultimate FPGA C64 core
Ultimate 64 Elite ac0304316 126 1.50
C64 Ultimate b0f40a17b 126 1.50

Active connect to a dead address, then LIST:

U64 Elite C64 Ultimate
RED (master) 18.19 s 18.38 s
GREEN (fix) 5.00 s 5.01 s

Standard profile (./run-tests --profile standard), red = master, green = a build carrying this landing set of fixes (this change included), report from python3 tools/e2e_report.py:

  • U64 Elite: 71 of 72 suites pass on master and 71 of 72 with the fixes. The one failure is the same on both, iec-partition-config: it stops at a key-send step because the upstream test's REST backend has no F2 key alias (rest_backend.py:556), a test-side limitation, not an app difference.
  • C64 Ultimate: 71 of 72 on master and 71 of 72 with the fixes. The one failure is again iec-partition-config, here at a "menu screen unavailable" step that also fails on master, so it is not from these changes.

Soaks pass on both devices: mount-cache-leak, listener-soak, network-connection.

Untested

  • Ultimate-II cartridge (U2+ / U2+L): not tested. I do not have one. Not built either for the cartridges, because the build environment I use cannot build those targets; CI builds them.
  • No host test, for the reason above.

Written with Claude Opus and Claude Sonnet in Claude Code.

The active-mode data connection set a 5 s socket timeout before
connect() so that a dead PORT address would fail fast, but lwIP's
blocking connect ignores socket timeouts and waits for TCP to give up,
about 18 s with the session blocked. It now connects without blocking
and waits at most 5 s in select() for the outcome.

STOR wrote whatever each recv() returned, often a single TCP segment,
so the file system copied every write through its sector buffer. The
8 KB buffer is now filled before it is written, so the middle of each
write is whole sectors. A stalled or aborted upload still keeps every
byte that arrived, and an empty upload still creates no file.

The FTP protocol is unchanged.

The new host test target/pc/linux/ftpd runs ftpd.cc's data connection
against loopback sockets; it is part of make host_tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Nov 10, 2026
@chrisgleissner
chrisgleissner self-requested a review November 10, 2026 12:30

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JC-000 thanks for this. The bounded connect looks right to me: non-blocking connect, select on the write set, SO_ERROR for the result, and blocking mode restored on every path. Your U64E and C64U timings (about 18 s down to 5 s) are the evidence for it, since a Linux host honours SO_SNDTIMEO on connect and cannot show the defect.

Requesting one change: the whole-buffer STOR writes are a separate change from #1057. The description reports +13% on the U64E and 0% on the C64U and makes no throughput claim. Please move that part into its own PR, or drop it, so this one stays a focused fix for the connect timeout. The nine stub headers and the STOR tests would move with it.

Happy to merge the connect fix as soon as that is split.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000 JC-000 changed the title # FTP: bound the active-mode connect to 5 s and write STOR data in whole buffers FTP: bound the active-mode connect to 5 s and write STOR data in whole buffers Nov 10, 2026
JC-000 and others added 2 commits November 10, 2026 09:57
The whole-buffer STOR writes are a separate change from the active-mode
connect bound (GideonZ#1057), so they move to their own change. This removes the
receivefile() change and the four STOR cases from the host test. The two
connect cases stay, and so does the host test harness, which they need to
build ftpd.cc.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The connect fix is shown by the hardware measurement, since a Linux host
honours the socket timeout that lwIP ignores, so the host test could not
show the defect. The harness goes with the STOR change that it was
written for.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I split it as you asked: a new commit takes the whole-buffer STOR change out, along with its tests and the host test harness and stub headers, so this PR is now the connect fix alone (only software/network/ftpd.cc). The STOR change is parked, not dropped. I will only bring it back with hardware evidence for the throughput and with its interaction with #803 worked out.

The connect fix has no host test now. A Linux host honours the socket timeout on connect and lwIP does not, so only the hardware timings show the defect (about 18 s down to 5 s on both devices, in the description).

Written with Claude Opus and Claude Sonnet in Claude Code.

@JC-000 JC-000 changed the title FTP: bound the active-mode connect to 5 s and write STOR data in whole buffers FTP: bound the active-mode connect to 5 s Nov 10, 2026
Makefile host_tests: drop the stale ftpd line (this PR removed the ftpd host
test) and keep master's new socketdma line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The STOR change is out, so this is the connect fix alone (only ftpd.cc). The non-blocking connect, select on the write set, SO_ERROR check and restored blocking mode on every path look right. Approving.

@chrisgleissner chrisgleissner self-assigned this Nov 11, 2026
@chrisgleissner

Copy link
Copy Markdown
Collaborator

I'm assigning this to myself. I'm investigating whether it is merge ready by running the standard E2E profile locally on an Ultimate 64 Elite and an Ultimate II+L, with this PR combined with the other approved bug fixes, and comparing against master. I'll report the result here.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Test result for the batch this PR is part of (#1043, #1064, #1065, #1066, #1068, #1070).

Firmware: upstream master b57d894a0 plus these six PRs (build 5c1492908), compared with plain master.
Devices: Ultimate 64 Elite (JTAG, from RAM) and Ultimate II+L in a C64 Ultimate (flashed). Standard E2E profile, overlay mode.

Run Ultimate 64 Elite Ultimate II+L in C64 Ultimate
Batch build, full standard profile 67 of 72 suites passed 39 of 48 suites passed
Failed suites rerun after installing the missing host Python modules (tests/requirements.txt) all 5 pass 6 of 8 fail
Master, same suites all 5 pass the same 6 of 8 fail

@chrisgleissner
chrisgleissner merged commit 7f1b62f into GideonZ:master Nov 11, 2026
1 check passed
JC-000 added a commit to JC-000/1541ultimate that referenced this pull request Nov 12, 2026
Brings in GideonZ#1043 (send the whole HTTP request, give up on a stalled
server), GideonZ#1064, GideonZ#1065 and GideonZ#1068, which this change is stacked on.

Conflicts, all from this branch carrying the pre-merge form of GideonZ#1043:
- build.yml, test_http_target.cc, http_request.cc, send_all.h,
  send_all_test.cpp, test_http_target/Makefile: kept this branch's side,
  which is upstream's plus this PR's own change.
- io/network/tests/Makefile: took upstream's (only target order differed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JC-000 added a commit to JC-000/1541ultimate that referenced this pull request Nov 12, 2026
Merges the updated fix/uci-abort-reset-deferral (890ef6d), which carries
upstream master with GideonZ#1043, GideonZ#1064, GideonZ#1065 and GideonZ#1068 merged in.

Conflict: software/io/network/tests/Makefile, only in the target order
(this branch had send-all after sys-arch-red, upstream before it). Kept
upstream's order and added the network-target target and variables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FTP active-mode connect blocks the control task ~18 s on an unreachable PORT address

2 participants