Repository navigation
Description
Every send of the host (Sources/AgentTermHost/HostServer.swift:1908-1925) hands its bytes to NWConnection with completion: .idempotent: nothing counts what is still waiting to go out.
Scenario. A peer that is alive but does not read: an iPhone whose app is suspended while iOS keeps its connection open (the comment on Client.inBackground says iOS may), or any client that stops reading. Everything meant for it is queued inside NWConnection: the output of the sessions it is attached to, systemStats every 3 seconds, the lists of sessions, the frames of a page of the browser. A peer that is gone is failed by TCP after a while; one whose window is zero is not. The host's memory grows with what the sessions print for as long as the phone stays that way.
Found by reading the code in the review of 2026-11-22, at commit 968516d; not reproduced yet. Not known, and to be tried before anything is designed (principle 5): what iOS really does with the socket of a suspended app, at the version in use, and how much memory the host takes in an hour of a session that prints.
A way to do it, not decided: count the bytes not yet sent, in the send's completion; past a limit, detach the client from its sessions (it attaches again with have and gets what it missed) or close it. What happens to a client that falls behind is a change of behaviour and of design: the owner's choice.
- Found out first: a client that signs in, attaches to a session that prints, and stops reading; the host's memory measured over time, the numbers written here
- The limit and what is done past it decided by the owner
- Made, its test passing, the layers of rule 2 run, committed, CI green
Decided by the owner on 2026-11-23: past about 8 MB waiting, the client is detached from its sessions' output and caught up from where it had all once it reads again; nothing is said to the user.
Definition of done
- The growth is measured with a real host (a state folder and a port of its own) and a client that does not read, and written into this issue with
docs/PERFORMANCE.md's way of measuring; if it does not grow, the issue is closed saying so. - The owner decides the limit and what is done past it (detach, close), and whether the user is told.
- A test in
AgentTermHostTestsshows it: a client that does not read never makes the host keep more than the limit for it, and one that comes back gets, withhave, all it missed, nothing twice. - A client that reads slowly but reads is never cut off; the other clients of the same session are not slowed.
- If the client is told why it was detached, the words are in
AgentTermProtocol(principle 9) and the protocol's number is raised for a changed message (principle 6). docs/DESIGN.md(Protocol; Attaching to a running session),docs/PERFORMANCE.mdanddocs/TESTING.mdsay it.swift buildandswift testpass;scripts/test-mac.shandscripts/test-ios.sh "iPhone Air", since both apps attach through it.make formatandmake lint, committed on the milestone's branch and pushed, CI on the branch green.
Tiếng Việt
Đây là bản dịch của phần tiếng Anh ở trên; các ô đánh dấu chỉ có ở phần tiếng Anh.
Mọi lần gửi của host (Sources/AgentTermHost/HostServer.swift:1908-1925) đều giao byte cho NWConnection với completion: .idempotent: không có gì đếm những gì còn đang chờ đi ra.
Kịch bản. Một peer còn sống nhưng không đọc: iPhone có app bị suspend trong khi iOS vẫn giữ kết nối (comment ở Client.inBackground nói iOS có thể làm vậy), hay bất kỳ client nào ngừng đọc. Mọi thứ dành cho nó được xếp hàng bên trong NWConnection: output của các session nó đang attach, systemStats mỗi 3 giây, danh sách session, các frame của một trang trong browser. Peer đã mất thì TCP sẽ báo lỗi sau một lúc; peer có window bằng 0 thì không. Bộ nhớ của host tăng theo những gì các session in ra, chừng nào điện thoại còn ở trạng thái đó.
Tìm thấy khi đọc code trong đợt rà soát ngày 2026-11-22, tại commit 968516d; chưa tái hiện. Chưa biết, và cần thử trước khi thiết kế bất cứ gì (nguyên tắc 5): iOS thật sự làm gì với socket của app bị suspend, ở phiên bản đang dùng, và host tốn bao nhiêu bộ nhớ sau một giờ của một session có in ra.
Một cách làm, chưa quyết: đếm số byte chưa gửi, trong completion của lần gửi; vượt một giới hạn thì detach client khỏi các session của nó (nó attach lại với have và nhận phần đã lỡ) hoặc đóng nó. Chuyện gì xảy ra với client bị tụt lại là thay đổi hành vi và thiết kế: owner quyết.
- Tìm hiểu trước: một client đăng nhập, attach vào một session đang in, rồi ngừng đọc; đo bộ nhớ của host theo thời gian, ghi số vào đây
- Owner quyết giới hạn và việc làm khi vượt giới hạn
- Làm xong, test pass, chạy các tầng của rule 2, commit, CI xanh
Đã quyết bởi owner ngày 2026-11-23: khi phần chờ gửi vượt khoảng 8 MB, host ngừng gửi output của các session cho client đó, và gửi bù từ chỗ client đã có đủ khi nó đọc lại; không báo gì cho người dùng.
Định nghĩa hoàn thành
- Mức tăng được đo với host thật (state folder và port riêng) và một client không đọc, rồi ghi vào issue này theo cách đo của
docs/PERFORMANCE.md; nếu không tăng, issue được đóng và ghi rõ như vậy. - Owner quyết giới hạn và việc làm khi vượt (detach, đóng), và có báo cho user không.
- Một test trong
AgentTermHostTestschỉ ra: client không đọc không bao giờ khiến host giữ cho nó nhiều hơn giới hạn, và client quay lại thì nhận, vớihave, mọi thứ đã lỡ, không gì bị lặp. - Client đọc chậm nhưng có đọc thì không bao giờ bị cắt; các client khác của cùng session không bị chậm đi.
- Nếu client được báo vì sao bị detach, câu chữ nằm trong
AgentTermProtocol(nguyên tắc 9) và số của protocol được tăng khi message thay đổi (nguyên tắc 6). docs/DESIGN.md(Protocol; Attaching to a running session),docs/PERFORMANCE.mdvàdocs/TESTING.mdghi lại.swift buildvàswift testpass;scripts/test-mac.shvàscripts/test-ios.sh "iPhone Air", vì cả hai app attach qua đường này.make formatvàmake lint, commit trên nhánh của milestone và push, CI trên nhánh xanh.
Activity
manhpham90vn commentedon Nov 23, 2026
Reproduced on 2026-11-23, on
milestone-0.15.1:Reproduced on 2026-11-23, on
milestone-0.15.1at e5c1bd8, byReviewCoreBackpressureTests.testWhatAClientDoesNotReadIsNotKeptWithoutLimitinTests/AgentTermHostTests/ReviewCoreBackpressureTests.swift(new, not committed).A host of the test's own; a client on a plain socket (its WebSocket frames written by hand, so that what it does not read stays in the system's buffers and not in the process) signs in and starts a session that prints 200 MB; the session's creator is attached to it. The memory of the process the host runs in (
phys_footprint) is read before and after, once with the client reading all it is sent and once with it reading nothing.swift test --filter ReviewCoreBackpressureTests:What it proves: nothing bounds what is queued for a client that does not read; it grows with the session's output (here more than twice the bytes printed, since each piece is wrapped in a frame and kept).
Not shown, not known: whether iOS really keeps a suspended app's connection open without reading (it needs a real iPhone); why the run with a client that reads also grew by 134 MB (the host is in the test's process, and that memory was not looked into); the 48 MB limit of the test is the test's own guess at "bounded", not a decided number.
The failing test, kept out of the branch while the issue is not done (it would make CI red), to be put back with the fix (
Tests/AgentTermHostTests/ReviewCoreSupport.swiftis on the branch):ReviewCoreBackpressureTests.swiftfix: a client's large messages go in pieces, and a client that does n…
manhpham90vn commentedon Nov 23, 2026
Measured first (PERF-12,
docs/PERFORMANCE.md), the real host with a state folder and port of its own, a session runningyes, a client that does not read: 115 MB at 0 s, 579 MB at 10 s, then the host was gone.Fixed in 753bd75, as the owner decided: the host counts what waits to go to each client; past
HostServer.mostUnsent(8 MB) the client is no longer sent its sessions' output, and once less than 1 MB waits it is caught up from where it had all (the scrollback, or the screen as on attaching). Nothing is said to the user. REMOTE-35 (SlowReaderTests): with the bound turned off the host kept 19 MB for the client; with it, at most the bound, and once it read the client got every one of 400,000 lines once. After it and #327, the same measure stays at 47 MB.docs/DESIGN.mdanddocs/TESTING.mdsay it.Run on
milestone-0.15.1, 2026-11-23:swift build,swift test,scripts/test-mac.shin full,scripts/test-ios.sh "iPhone Air"andscripts/test-both.sh "iPhone Air"passing on 022aa66;make lint; CI on the branch green (run 45152397234).Whether iOS keeps a suspended app's connection open without reading is for a real iPhone, noted in the pull request (#326). Done in full: closed.