This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-10-quiet-head
in repository terminology.
View the commit online.
commit 71aa09b55f27acc55aafaeaca58a0aba8c1af159
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 03:05:57 2026 -0600
termtymux: queue messages the socket could not take
The client's socket is non-blocking, so a REQ, RELEASE or INPUT sent
while it is full stopped at EAGAIN: either part of a message went out,
throwing the daemon's reader out of step with the stream, or the
message was lost. A paste larger than what the socket buffers while the
daemon is busy is enough to hit it.
Queue what the socket does not take and flush it when the socket is
writable, as tymuxd does for its clients. A queued message counts as
sent.
Input is never refused for its size, as the write buffer of a local pty
is not either, so a paste is never cut short. Pasted bytes wait
unframed and are framed into INPUT messages only while less than one
message's worth is queued: the paste goes out as fast as the daemon
takes it, and a page or map request sent meanwhile does not wait behind
megabytes of it. Input keeps its order. Control messages are still
bounded at 1 MiB, the bound tymuxd puts on a client.
A daemon is deemed stuck when it takes nothing of the queue for 20 s,
whatever its size: a size bound would measure how fast we produce
input, not whether the daemon reads. The session then detaches through
a shutdown of the socket, so cb.exited fires from the main loop and not
from inside a view callback still holding ts, and anything the daemon
sent before, such as an EXIT, is still read first.
tymux's own client is not affected: it shares termsrv_msg_send(), but
its socket is blocking.
---
src/bin/termtymux.c | 913 +++++++++++++++++++++++++++++++++++++++++++++++--
src/bin/termtymux.h | 8 +-
src/bin/tytest.c | 10 +
src/bin/tytest_tymux.c | 75 ++++
src/bin/tytest_tymux.h | 1 +
src/bin/unit_tests.h | 7 +
tests/meson.build | 2 +-
7 files changed, 984 insertions(+), 32 deletions(-)
diff --git a/src/bin/termtymux.c b/src/bin/termtymux.c
index 158a1259..04464e3a 100644
--- a/src/bin/termtymux.c
+++ b/src/bin/termtymux.c
@@ -184,6 +184,328 @@ _shadow_pty_new(int cols, int rows, Config *config)
return ty;
}
+/* ── sending ────────────────────────────────────────────────────────── */
+
+/* What control messages may queue up before the daemon is deemed stuck,
+ * the bound tymuxd puts on a client that does not keep up. Input is not
+ * bounded, see _input_send(). */
+#define OUT_QUEUE_MAX (1u << 20)
+
+/* How long the queue may wait without the daemon taking any of it before
+ * it is deemed stuck. Not a constant so that the tests can shorten it. */
+static double _out_stuck_timeout = 20.0;
+
+static size_t
+_out_length(const TermTymux *ts)
+{
+ return ts->out ? eina_binbuf_length_get(ts->out) : 0;
+}
+
+static size_t
+_input_length(const TermTymux *ts)
+{
+ return ts->input ? eina_binbuf_length_get(ts->input) - ts->input_off : 0;
+}
+
+/* Returns how many bytes of iov the socket took without blocking, or -1 if
+ * the connection failed. */
+static ssize_t
+_sock_write(int fd, struct iovec *iov, size_t iov_count)
+{
+ struct msghdr msg = { .msg_iov = iov, .msg_iovlen = iov_count };
+ ssize_t n;
+
+ do
+ n = sendmsg(fd, &msg, MSG_DONTWAIT | MSG_NOSIGNAL);
+ while (n < 0 && errno == EINTR);
+ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
+ return 0;
+ return n;
+}
+
+static void
+_send_error_log(const TermTymux *ts, int err)
+{
+ /* the daemon hung up: the end of the stream reports it */
+ if (err == EPIPE || err == ECONNRESET)
+ DBG("termtymux: tymuxd for '%s' hung up: %s", ts->name, strerror(err));
+ else
+ ERR("termtymux: send to tymuxd for '%s': %s", ts->name, strerror(err));
+}
+
+/* Drops the input not framed yet: the daemon reads nothing after DETACH */
+static void
+_input_drop(TermTymux *ts)
+{
+ if (ts->input) eina_binbuf_reset(ts->input);
+ ts->input_off = 0;
+}
+
+static void
+_out_clear(TermTymux *ts)
+{
+ if (ts->out) eina_binbuf_reset(ts->out);
+ _input_drop(ts);
+ if (ts->out_timer)
+ {
+ ecore_timer_del(ts->out_timer);
+ ts->out_timer = NULL;
+ }
+}
+
+/* Stops sending and makes the main loop see the end of the stream, where
+ * _cb_fd() detaches: this may run within a callback whose caller still
+ * holds ts, so it must not fire cb.exited itself. The daemon sees the
+ * client go as well. */
+static void
+_send_fail(TermTymux *ts)
+{
+ ts->send_failed = EINA_TRUE;
+ _out_clear(ts);
+ shutdown(ts->sock_fd, SHUT_RDWR);
+ if (ts->handler)
+ ecore_main_fd_handler_active_set(ts->handler,
+ ECORE_FD_READ | ECORE_FD_ERROR);
+}
+
+static Eina_Bool _out_send(TermTymux *ts, Eina_Bool *progress);
+
+static Eina_Bool
+_cb_out_stuck(void *data)
+{
+ TermTymux *ts = data;
+ Eina_Bool progress = EINA_FALSE;
+
+ ts->out_timer = NULL;
+ /* the socket may have taken more since the write callback last ran */
+ if (!_out_send(ts, &progress) || progress)
+ return ECORE_CALLBACK_CANCEL;
+ ERR("termtymux: tymuxd for '%s' took nothing for %.0f s, detaching",
+ ts->name, _out_stuck_timeout);
+ _send_fail(ts);
+ return ECORE_CALLBACK_CANCEL;
+}
+
+/* Watches for the socket to take what waits, and for the daemon to stop
+ * taking it */
+static void
+_out_watch(TermTymux *ts)
+{
+ Eina_Bool waiting = _out_length(ts) > 0 || _input_length(ts) > 0;
+
+ if (ts->handler)
+ ecore_main_fd_handler_active_set(ts->handler,
+ ECORE_FD_READ | ECORE_FD_ERROR |
+ (waiting ? ECORE_FD_WRITE : 0));
+ if (!waiting && ts->out_timer)
+ {
+ ecore_timer_del(ts->out_timer);
+ ts->out_timer = NULL;
+ }
+ else if (waiting && !ts->out_timer)
+ ts->out_timer = ecore_timer_add(_out_stuck_timeout, _cb_out_stuck, ts);
+}
+
+/* Queues the bytes of iov past the first skip ones */
+static Eina_Bool
+_out_append(TermTymux *ts, const struct iovec *iov, size_t iov_count,
+ size_t skip)
+{
+ size_t i;
+
+ if (!ts->out)
+ ts->out = eina_binbuf_new();
+ if (!ts->out)
+ goto fail;
+ for (i = 0; i < iov_count; i++)
+ {
+ if (skip >= iov[i].iov_len)
+ {
+ skip -= iov[i].iov_len;
+ continue;
+ }
+ if (!eina_binbuf_append_length(ts->out,
+ (const unsigned char *)iov[i].iov_base +
+ skip, iov[i].iov_len - skip))
+ goto fail;
+ skip = 0;
+ }
+ return EINA_TRUE;
+
+fail:
+ ERR("termtymux: out of memory queuing for tymuxd for '%s'", ts->name);
+ _send_fail(ts);
+ return EINA_FALSE;
+}
+
+/* Frames unframed input into INPUT messages while the queue holds less
+ * than a message's worth, so that control messages queued meanwhile do not
+ * wait behind all of a paste. */
+static Eina_Bool
+_input_frame(TermTymux *ts)
+{
+ size_t len;
+
+ while ((len = _input_length(ts)) > 0 &&
+ _out_length(ts) < TERMSRV_MAX_MSG_PAYLOAD)
+ {
+ TermSrvMsgHdr hdr = { .type = TSRV_MSG_INPUT,
+ .size = MIN(len, TERMSRV_MAX_MSG_PAYLOAD) };
+ struct iovec iov[2] = {
+ { .iov_base = &hdr, .iov_len = sizeof(hdr) },
+ { .iov_base = (void *)(eina_binbuf_string_get(ts->input) +
+ ts->input_off),
+ .iov_len = hdr.size },
+ };
+
+ if (!_out_append(ts, iov, 2, 0))
+ return EINA_FALSE;
+ ts->input_off += hdr.size;
+ }
+ if (ts->input_off > 0 &&
+ ts->input_off >= eina_binbuf_length_get(ts->input) / 2)
+ {
+ eina_binbuf_remove(ts->input, 0, ts->input_off);
+ ts->input_off = 0;
+ }
+ return EINA_TRUE;
+}
+
+/* Sends what waits, as much as the socket takes, and watches for it to
+ * take more. *progress tells whether it took any. Returns EINA_FALSE if
+ * the connection failed. */
+static Eina_Bool
+_out_send(TermTymux *ts, Eina_Bool *progress)
+{
+ *progress = EINA_FALSE;
+ for (;;)
+ {
+ struct iovec iov;
+ ssize_t n;
+
+ if (!_input_frame(ts))
+ return EINA_FALSE;
+ iov.iov_len = _out_length(ts);
+ if (iov.iov_len == 0)
+ break;
+ iov.iov_base = (void *)eina_binbuf_string_get(ts->out);
+ n = _sock_write(ts->sock_fd, &iov, 1);
+ if (n < 0)
+ {
+ _send_error_log(ts, errno);
+ _send_fail(ts);
+ return EINA_FALSE;
+ }
+ if (n == 0)
+ break;
+ eina_binbuf_remove(ts->out, 0, (size_t)n);
+ *progress = EINA_TRUE;
+ }
+ if (*progress && ts->out_timer)
+ ecore_timer_reset(ts->out_timer);
+ _out_watch(ts);
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+_out_flush(TermTymux *ts)
+{
+ Eina_Bool progress;
+
+ return _out_send(ts, &progress);
+}
+
+/* Sends a message, or queues what the socket does not take of it, behind
+ * which every later message waits so that the daemon reads them whole and
+ * in order. A bounded message is refused once OUT_QUEUE_MAX bytes wait.
+ * Returns EINA_TRUE once the message is sent or queued, and EINA_FALSE if
+ * nothing can be sent any more: the session then detaches from the main
+ * loop. */
+static Eina_Bool
+_msg_write(TermTymux *ts, TermSrvMsgType type,
+ const void *payload, uint32_t size, Eina_Bool bounded)
+{
+ TermSrvMsgHdr hdr = { .type = type, .size = payload ? size : 0 };
+ struct iovec iov[2] = {
+ { .iov_base = &hdr, .iov_len = sizeof(hdr) },
+ { .iov_base = (void *)payload, .iov_len = hdr.size },
+ };
+ size_t iov_count = hdr.size ? 2 : 1;
+ size_t queued, sent = 0, total = sizeof(hdr) + hdr.size;
+
+ if (ts->sock_fd < 0 || ts->send_failed)
+ return EINA_FALSE;
+ queued = _out_length(ts);
+ /* only when nothing is queued, or this message would overtake it */
+ if (queued == 0)
+ {
+ ssize_t n = _sock_write(ts->sock_fd, iov, iov_count);
+
+ if (n < 0)
+ {
+ _send_error_log(ts, errno);
+ _send_fail(ts);
+ return EINA_FALSE;
+ }
+ sent = (size_t)n;
+ if (sent == total)
+ return EINA_TRUE;
+ }
+ if (bounded && queued + total - sent > OUT_QUEUE_MAX)
+ {
+ ERR("termtymux: tymuxd for '%s' does not take what we send, "
+ "detaching", ts->name);
+ _send_fail(ts);
+ return EINA_FALSE;
+ }
+ if (!_out_append(ts, iov, iov_count, sent))
+ return EINA_FALSE;
+ _out_watch(ts);
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+_msg_send(TermTymux *ts, TermSrvMsgType type,
+ const void *payload, uint32_t size)
+{
+ return _msg_write(ts, type, payload, size, EINA_TRUE);
+}
+
+/* Sends input after the input before it. It is never refused for its
+ * size, as a local pty's write buffer is not either, so that a paste is
+ * never cut: what cannot be framed yet waits in ts->input, and goes out as
+ * the daemon takes the rest. */
+static Eina_Bool
+_input_send(TermTymux *ts, const char *buf, size_t len)
+{
+ if (ts->sock_fd < 0 || ts->send_failed)
+ return EINA_FALSE;
+ while (len > 0 && _input_length(ts) == 0 &&
+ _out_length(ts) < TERMSRV_MAX_MSG_PAYLOAD)
+ {
+ uint32_t chunk = MIN(len, TERMSRV_MAX_MSG_PAYLOAD);
+
+ if (!_msg_write(ts, TSRV_MSG_INPUT, buf, chunk, EINA_FALSE))
+ return EINA_FALSE;
+ buf += chunk;
+ len -= chunk;
+ }
+ if (len == 0)
+ return EINA_TRUE;
+ if (!ts->input)
+ ts->input = eina_binbuf_new();
+ if (!ts->input ||
+ !eina_binbuf_append_length(ts->input, (const unsigned char *)buf, len))
+ {
+ ERR("termtymux: out of memory queuing input for tymuxd for '%s'",
+ ts->name);
+ _send_fail(ts);
+ return EINA_FALSE;
+ }
+ _out_watch(ts);
+ return EINA_TRUE;
+}
+
/* ── backlog pages ──────────────────────────────────────────────────── */
static Eina_Bool
@@ -191,15 +513,7 @@ _page_id_send(TermTymux *ts, TermSrvMsgType type, uint32_t id)
{
TermSrvMsgBacklogPageId msg = { id };
- if (ts->sock_fd < 0)
- return EINA_FALSE;
- if (termsrv_msg_send(ts->sock_fd, type, &msg, sizeof(msg)) < 0)
- {
- ERR("termtymux: send page %u message %d: %s", id, type,
- strerror(errno));
- return EINA_FALSE;
- }
- return EINA_TRUE;
+ return _msg_send(ts, type, &msg, sizeof(msg));
}
static Eina_Bool
@@ -224,15 +538,7 @@ _backlog_map_request(void *data, uint32_t first, uint32_t count)
TermTymux *ts = data;
TermSrvMsgBacklogMapReq req = { first, count };
- if (ts->sock_fd < 0)
- return EINA_FALSE;
- if (termsrv_msg_send(ts->sock_fd, TSRV_MSG_BACKLOG_MAP_REQ,
- &req, sizeof(req)) < 0)
- {
- ERR("termtymux: send MAP_REQ: %s", strerror(errno));
- return EINA_FALSE;
- }
- return EINA_TRUE;
+ return _msg_send(ts, TSRV_MSG_BACKLOG_MAP_REQ, &req, sizeof(req));
}
static const BacklogViewSource _backlog_source =
@@ -362,11 +668,7 @@ _map_received(TermTymux *ts, const void *payload, uint32_t size)
static Eina_Bool
_write_chunk_send(void *data, const char *buf, int len)
{
- TermTymux *ts = data;
-
- if (termsrv_msg_send(ts->sock_fd, TSRV_MSG_INPUT, buf, (uint32_t)len) < 0)
- return EINA_FALSE;
- return EINA_TRUE;
+ return _input_send(data, buf, (size_t)len);
}
typedef enum
@@ -437,13 +739,15 @@ _write_hook(void *data, Termpty *ty, const char *input, int len)
{
case TERMTYMUX_INPUT_DETACH:
/* Matches tymux.c's _detach(): whatever preceded the chord within
- * this same call is not forwarded either. The daemon drops this
+ * this same call is not forwarded either, nor is the input still
+ * waiting to be framed. The daemon drops this
* client once it sees DETACH (termd_tymux.c
* _client_msg_handle()) instead of answering, so the detach is
* only complete once the socket closes: _cb_fd() then sees EOF
* and fires cb.exited with end_reason left at its default,
* TERMTYMUX_END_DETACHED. */
- termsrv_msg_send(ts->sock_fd, TSRV_MSG_DETACH, NULL, 0);
+ _input_drop(ts);
+ _msg_send(ts, TSRV_MSG_DETACH, NULL, 0);
break;
case TERMTYMUX_INPUT_DROP:
/* termio shows the pause overlay (cb_pause, wired in termio.c);
@@ -559,6 +863,7 @@ _fire_exited(TermTymux *ts)
}
/* nothing is sent to the daemon any more, page releases included */
backlog_view_source_lost(ts->view);
+ _out_clear(ts);
if (ts->sock_fd >= 0)
{
close(ts->sock_fd);
@@ -568,13 +873,19 @@ _fire_exited(TermTymux *ts)
}
static Eina_Bool
-_cb_fd(void *data, Ecore_Fd_Handler *handler EINA_UNUSED)
+_cb_fd(void *data, Ecore_Fd_Handler *handler)
{
TermTymux *ts = data;
void *payload;
uint32_t size;
int type, fd;
+ /* a failed flush ends the stream: the read below then detaches */
+ if (ecore_main_fd_handler_active_get(handler, ECORE_FD_WRITE))
+ _out_flush(ts);
+ if (!ecore_main_fd_handler_active_get(handler, ECORE_FD_READ |
+ ECORE_FD_ERROR))
+ return ECORE_CALLBACK_RENEW;
if (termsrv_reader_fill(&ts->reader, ts->sock_fd) < 0)
{
ts->shadow_pty->pid = -1;
@@ -738,10 +1049,17 @@ termtymux_free(TermTymux *ts)
}
if (ts->sock_fd >= 0)
{
- termsrv_msg_send(ts->sock_fd, TSRV_MSG_DETACH, NULL, 0);
+ /* one last chance for what is framed, the daemon taking an
+ * unfinished message cut by the close as a hang-up */
+ _input_drop(ts);
+ if (_msg_send(ts, TSRV_MSG_DETACH, NULL, 0))
+ _out_flush(ts);
close(ts->sock_fd);
ts->sock_fd = -1;
}
+ _out_clear(ts);
+ if (ts->out) eina_binbuf_free(ts->out);
+ if (ts->input) eina_binbuf_free(ts->input);
if (ts->shm) munmap(ts->shm, TERMSRV_SHM_TOTAL_SIZE);
backlog_view_free(ts->view);
termsrv_reader_clear(&ts->reader);
@@ -833,14 +1151,13 @@ termtymux_resize(TermTymux *ts, int cols, int rows)
resize.cols = (uint32_t)cols;
resize.rows = (uint32_t)rows;
- if (termsrv_msg_send(ts->sock_fd, TSRV_MSG_RESIZE,
- &resize, sizeof(resize)) < 0)
- ERR("termtymux: send RESIZE: %s", strerror(errno));
+ _msg_send(ts, TSRV_MSG_RESIZE, &resize, sizeof(resize));
}
#if defined(BINARY_TYTEST)
#include <assert.h>
+#include <sys/ioctl.h>
static Termpty *
_tt_test_pty_new(int w, int h)
@@ -1641,4 +1958,540 @@ tytest_termtymux_title_compose(void)
return 0;
}
+/* A client whose socket takes as little as the kernel allows, watched by
+ * the main loop as termtymux_new() does, talking to *peer. */
+static void
+_tt_tymux_small_connect(TermTymux *ts, Termpty *ty, int *peer)
+{
+ int sv[2], size = 0;
+
+ ecore_init();
+ memset(ts, 0, sizeof(*ts));
+ ts->shadow_pty = ty;
+ ts->view = backlog_view_new(ty, &_backlog_source, ts);
+ assert(ts->view);
+ assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == 0);
+ assert(setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) == 0);
+ assert(fcntl(sv[0], F_SETFL, O_NONBLOCK) == 0);
+ assert(fcntl(sv[1], F_SETFL, O_NONBLOCK) == 0);
+ ts->sock_fd = sv[0];
+ ts->handler = ecore_main_fd_handler_add(sv[0],
+ ECORE_FD_READ | ECORE_FD_ERROR,
+ _cb_fd, ts, NULL, NULL);
+ assert(ts->handler);
+ *peer = sv[1];
+}
+
+static void
+_tt_tymux_small_close(TermTymux *ts, int peer)
+{
+ _out_clear(ts);
+ if (ts->handler) ecore_main_fd_handler_del(ts->handler);
+ if (ts->sock_fd >= 0) close(ts->sock_fd);
+ close(peer);
+ if (ts->out) eina_binbuf_free(ts->out);
+ if (ts->input) eina_binbuf_free(ts->input);
+ termsrv_reader_clear(&ts->reader);
+ backlog_view_free(ts->view);
+ ecore_shutdown();
+}
+
+#define _TT_SEND_COUNT 300
+
+/* Message i of tytest_termtymux_send_partial() cycles through every kind
+ * the client sends. They add up to less than one INPUT's worth, so none of
+ * the input is held back unframed and every message keeps its place. */
+static int
+_tt_send_type(int i)
+{
+ static const int types[] = {
+ TSRV_MSG_INPUT, TSRV_MSG_BACKLOG_PAGE_REQ, TSRV_MSG_INPUT,
+ TSRV_MSG_BACKLOG_PAGE_RELEASE, TSRV_MSG_BACKLOG_MAP_REQ
+ };
+
+ return types[i % EINA_C_ARRAY_LENGTH(types)];
+}
+
+static uint32_t
+_tt_send_size(int i)
+{
+ return 1 + ((uint32_t)i * 97u) % 300u;
+}
+
+static void
+_tt_send_fill(unsigned char *buf, int i, uint32_t size)
+{
+ uint32_t k;
+
+ for (k = 0; k < size; k++)
+ buf[k] = (unsigned char)(i * 7 + k);
+}
+
+static void
+_tt_send_one(TermTymux *ts, int i, Eina_Bool *all_sent, size_t *total)
+{
+ unsigned char buf[300];
+ uint32_t size = sizeof(TermSrvMsgBacklogPageId);
+
+ switch (_tt_send_type(i))
+ {
+ case TSRV_MSG_INPUT:
+ size = _tt_send_size(i);
+ _tt_send_fill(buf, i, size);
+ *all_sent &= _write_chunk_send(ts, (const char *)buf, (int)size);
+ break;
+ case TSRV_MSG_BACKLOG_PAGE_REQ:
+ *all_sent &= _backlog_page_request(ts, (uint32_t)i);
+ break;
+ case TSRV_MSG_BACKLOG_PAGE_RELEASE:
+ _backlog_page_release(ts, (uint32_t)i);
+ break;
+ case TSRV_MSG_BACKLOG_MAP_REQ:
+ *all_sent &= _backlog_map_request(ts, (uint32_t)i, 1);
+ size = sizeof(TermSrvMsgBacklogMapReq);
+ break;
+ }
+ *total += sizeof(TermSrvMsgHdr) + size;
+}
+
+/* Takes what peer has of the messages of tytest_termtymux_send_partial(),
+ * checking each is the next one, whole */
+static void
+_tt_partial_read(TermSrvReader *reader, int peer, int *got)
+{
+ unsigned char buf[300];
+ void *payload;
+ uint32_t size;
+ int type;
+
+ assert(termsrv_reader_fill(reader, peer) == 0);
+ while ((type = termsrv_reader_next(reader, &payload, &size)) > 0)
+ {
+ assert(*got < _TT_SEND_COUNT);
+ assert(type == _tt_send_type(*got));
+ switch (type)
+ {
+ case TSRV_MSG_INPUT:
+ assert(size == _tt_send_size(*got));
+ _tt_send_fill(buf, *got, size);
+ assert(!memcmp(payload, buf, size));
+ break;
+ case TSRV_MSG_BACKLOG_MAP_REQ:
+ assert(size == sizeof(TermSrvMsgBacklogMapReq));
+ assert(((TermSrvMsgBacklogMapReq *)payload)->first_page_id ==
+ (uint32_t)*got);
+ assert(((TermSrvMsgBacklogMapReq *)payload)->count == 1);
+ break;
+ default:
+ assert(size == sizeof(TermSrvMsgBacklogPageId));
+ assert(((TermSrvMsgBacklogPageId *)payload)->page_id ==
+ (uint32_t)*got);
+ break;
+ }
+ free(payload);
+ (*got)++;
+ }
+ assert(type == 0);
+}
+
+/* Test: messages of every kind sent while the socket cannot take them all
+ * wait their turn, through flushes that only partly empty the queue, and
+ * the daemon gets every one whole and in order. */
+int
+tytest_termtymux_send_partial(void)
+{
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ TermSrvReader reader = { 0 };
+ Eina_Bool all_sent = EINA_TRUE, queued = EINA_FALSE;
+ size_t total = 0;
+ TermTymux ts;
+ int i, got = 0, rounds, peer, unread = 0;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ _tt_tymux_small_connect(&ts, ty, &peer);
+
+ for (i = 0; i < _TT_SEND_COUNT; i++)
+ {
+ _tt_send_one(&ts, i, &all_sent, &total);
+ queued |= _out_length(&ts) > 0;
+ if (i % 25 == 24)
+ {
+ _tt_partial_read(&reader, peer, &got);
+ ecore_main_loop_iterate();
+ }
+ }
+ assert(queued);
+ assert(_input_length(&ts) == 0);
+
+ for (rounds = 0; got < _TT_SEND_COUNT && rounds < 100000; rounds++)
+ {
+ _tt_partial_read(&reader, peer, &got);
+ ecore_main_loop_iterate();
+ }
+ assert(got == _TT_SEND_COUNT);
+ assert(all_sent);
+
+ /* nothing more comes */
+ ecore_main_loop_iterate();
+ assert(ioctl(peer, FIONREAD, &unread) == 0);
+ assert(unread == 0);
+ assert(reader.base + reader.start == total);
+ assert(!ts.out_timer);
+
+ termsrv_reader_clear(&reader);
+ _tt_tymux_small_close(&ts, peer);
+ termpty_free(ty);
+ return 0;
+}
+
+/* What the peer of a client got: the bytes of every INPUT, and how many of
+ * them came before the first PAGE_REQ */
+typedef struct
+{
+ TermSrvReader reader;
+ Eina_Binbuf *input;
+ size_t input_before_req;
+ Eina_Bool req_seen;
+} Tt_Received;
+
+static void
+_tt_received_read(Tt_Received *received, int peer)
+{
+ void *payload;
+ uint32_t size;
+ int type;
+
+ assert(termsrv_reader_fill(&received->reader, peer) == 0);
+ while ((type = termsrv_reader_next(&received->reader,
+ &payload, &size)) > 0)
+ {
+ if (type == TSRV_MSG_INPUT)
+ {
+ assert(size > 0 && size <= TERMSRV_MAX_MSG_PAYLOAD);
+ assert(eina_binbuf_append_length(received->input, payload,
+ size));
+ }
+ else
+ {
+ assert(type == TSRV_MSG_BACKLOG_PAGE_REQ);
+ assert(!received->req_seen);
+ received->req_seen = EINA_TRUE;
+ received->input_before_req =
+ eina_binbuf_length_get(received->input);
+ }
+ free(payload);
+ }
+ assert(type == 0);
+}
+
+/* Reads from peer, letting ts send, until want bytes of input came */
+static void
+_tt_received_wait(Tt_Received *received, int peer, size_t want)
+{
+ int rounds;
+
+ for (rounds = 0;
+ eina_binbuf_length_get(received->input) < want && rounds < 1000000;
+ rounds++)
+ {
+ _tt_received_read(received, peer);
+ ecore_main_loop_iterate();
+ }
+ _tt_received_read(received, peer);
+}
+
+#define _TT_PASTE_SIZE (4u << 20)
+
+static char *
+_tt_paste_new(void)
+{
+ char *paste = malloc(_TT_PASTE_SIZE);
+ size_t k;
+
+ assert(paste);
+ for (k = 0; k < _TT_PASTE_SIZE; k++)
+ paste[k] = 'a' + (char)((k + k / 4099) % 26);
+ return paste;
+}
+
+static int _tt_exited_count;
+
+static void
+_tt_exited_cb(void *data EINA_UNUSED)
+{
+ _tt_exited_count++;
+}
+
+/* Test: a paste far bigger than the queue bound reaches a daemon that
+ * keeps reading, every byte in order in whole INPUT messages, and the
+ * session stays attached. */
+int
+tytest_termtymux_send_paste(void)
+{
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ Tt_Received received = { 0 };
+ char *paste = _tt_paste_new();
+ TermTymux ts;
+ int peer;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ _tt_tymux_small_connect(&ts, ty, &peer);
+ ts.chord_out = eina_binbuf_new();
+ received.input = eina_binbuf_new();
+ assert(ts.chord_out && received.input);
+ ty->cb.exited.func = _tt_exited_cb;
+ _tt_exited_count = 0;
+
+ ty->paste_active = 1;
+ assert(_write_hook(&ts, ty, paste, (int)_TT_PASTE_SIZE));
+ ty->paste_active = 0;
+ assert(_input_length(&ts) > 0);
+ _tt_received_wait(&received, peer, _TT_PASTE_SIZE);
+
+ assert(eina_binbuf_length_get(received.input) == _TT_PASTE_SIZE);
+ assert(!memcmp(eina_binbuf_string_get(received.input), paste,
+ _TT_PASTE_SIZE));
+ assert(_tt_exited_count == 0);
+ assert(ts.sock_fd >= 0 && !ts.send_failed);
+ assert(_out_length(&ts) == 0 && _input_length(&ts) == 0);
+ assert(!ts.out_timer);
+
+ termsrv_reader_clear(&received.reader);
+ eina_binbuf_free(received.input);
+ eina_binbuf_free(ts.chord_out);
+ _tt_tymux_small_close(&ts, peer);
+ termpty_free(ty);
+ free(paste);
+ return 0;
+}
+
+/* Test: a request sent during a paste overtakes the part of it still
+ * waiting to be framed, and the paste still arrives whole and in order. */
+int
+tytest_termtymux_send_control_first(void)
+{
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ Tt_Received received = { 0 };
+ char *paste = _tt_paste_new();
+ TermTymux ts;
+ int peer;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ _tt_tymux_small_connect(&ts, ty, &peer);
+ received.input = eina_binbuf_new();
+ assert(received.input);
+
+ assert(_input_send(&ts, paste, _TT_PASTE_SIZE));
+ assert(_backlog_page_request(&ts, 7));
+ _tt_received_wait(&received, peer, _TT_PASTE_SIZE);
+
+ assert(received.req_seen);
+ /* behind what was framed already, at most two INPUTs' worth */
+ assert(received.input_before_req <= 2 * TERMSRV_MAX_MSG_PAYLOAD);
+ assert(eina_binbuf_length_get(received.input) == _TT_PASTE_SIZE);
+ assert(!memcmp(eina_binbuf_string_get(received.input), paste,
+ _TT_PASTE_SIZE));
+
+ termsrv_reader_clear(&received.reader);
+ eina_binbuf_free(received.input);
+ _tt_tymux_small_close(&ts, peer);
+ termpty_free(ty);
+ free(paste);
+ return 0;
+}
+
+/* Iterates the main loop until cb.exited fired or timeout seconds went */
+static void
+_tt_exited_wait(double timeout)
+{
+ double end = ecore_time_get() + timeout;
+
+ while (!_tt_exited_count && ecore_time_get() < end)
+ {
+ ecore_main_loop_iterate();
+ usleep(1000);
+ }
+}
+
+/* Test: a daemon that takes nothing of the queue for the stuck timeout
+ * gets the client detached once, from the main loop rather than from
+ * within a send whose caller still holds ts. Input is never refused for
+ * its size before that. */
+int
+tytest_termtymux_send_stuck(void)
+{
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ char *paste = _tt_paste_new();
+ double timeout = _out_stuck_timeout;
+ char drain[4096];
+ TermTymux ts;
+ int peer, i;
+ ssize_t n;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ _tt_tymux_small_connect(&ts, ty, &peer);
+ ty->cb.exited.func = _tt_exited_cb;
+ _tt_exited_count = 0;
+ _out_stuck_timeout = 0.2;
+
+ for (i = 0; i < 4; i++)
+ assert(_input_send(&ts, paste, _TT_PASTE_SIZE));
+ assert(_backlog_page_request(&ts, 1));
+ assert(_tt_exited_count == 0);
+
+ _tt_exited_wait(5.0);
+ assert(_tt_exited_count == 1);
+ assert(ts.sock_fd == -1 && !ts.handler && !ts.out_timer);
+ assert(_out_length(&ts) == 0 && _input_length(&ts) == 0);
+ assert(termtymux_end_reason_get(&ts) == TERMTYMUX_END_DETACHED);
+ assert(!_backlog_page_request(&ts, 2));
+ assert(!_input_send(&ts, "x", 1));
+ for (i = 0; i < 50; i++)
+ {
+ ecore_main_loop_iterate();
+ usleep(10000);
+ }
+ assert(_tt_exited_count == 1);
+
+ /* the daemon sees the client go */
+ while ((n = read(peer, drain, sizeof(drain))) > 0)
+ ;
+ assert(n == 0);
+
+ _out_stuck_timeout = timeout;
+ _tt_tymux_small_close(&ts, peer);
+ termpty_free(ty);
+ free(paste);
+ return 0;
+}
+
+/* Test: the stuck timer only detaches from a daemon that still takes
+ * nothing: what the socket took since the write callback last ran keeps
+ * the client attached, and its queue going out. */
+int
+tytest_termtymux_send_stuck_flush(void)
+{
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ Tt_Received received = { 0 };
+ char *paste = _tt_paste_new();
+ TermTymux ts;
+ int peer;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ _tt_tymux_small_connect(&ts, ty, &peer);
+ received.input = eina_binbuf_new();
+ assert(received.input);
+ ty->cb.exited.func = _tt_exited_cb;
+ _tt_exited_count = 0;
+
+ assert(_input_send(&ts, paste, _TT_PASTE_SIZE));
+ assert(ts.out_timer);
+ /* the daemon reads, but the main loop does not run before the timer */
+ _tt_received_read(&received, peer);
+ assert(_cb_out_stuck(&ts) == ECORE_CALLBACK_CANCEL);
+ assert(!ts.send_failed && ts.out_timer);
+ _tt_received_wait(&received, peer, _TT_PASTE_SIZE);
+ assert(eina_binbuf_length_get(received.input) == _TT_PASTE_SIZE);
+ assert(_tt_exited_count == 0 && !ts.send_failed);
+
+ termsrv_reader_clear(&received.reader);
+ eina_binbuf_free(received.input);
+ _tt_tymux_small_close(&ts, peer);
+ termpty_free(ty);
+ free(paste);
+ return 0;
+}
+
+/* Test: the detach chord drops the input still waiting to be framed, as
+ * the daemon reads nothing after DETACH: the client sends DETACH last. */
+int
+tytest_termtymux_send_detach_drops_input(void)
+{
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ char *paste = _tt_paste_new();
+ TermSrvReader reader = { 0 };
+ void *payload;
+ uint32_t size;
+ TermTymux ts;
+ int peer, type, last = 0, rounds;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ _tt_tymux_small_connect(&ts, ty, &peer);
+ ts.chord_out = eina_binbuf_new();
+ assert(ts.chord_out);
+
+ assert(_input_send(&ts, paste, _TT_PASTE_SIZE));
+ assert(_input_length(&ts) > 0);
+ assert(_write_hook(&ts, ty, "\x02" "d", 2));
+ assert(_input_length(&ts) == 0);
+ for (rounds = 0; _out_length(&ts) > 0 && rounds < 100000; rounds++)
+ {
+ assert(termsrv_reader_fill(&reader, peer) == 0);
+ while ((type = termsrv_reader_next(&reader, &payload, &size)) > 0)
+ {
+ last = type;
+ free(payload);
+ }
+ ecore_main_loop_iterate();
+ }
+ assert(termsrv_reader_fill(&reader, peer) == 0);
+ while ((type = termsrv_reader_next(&reader, &payload, &size)) > 0)
+ {
+ last = type;
+ free(payload);
+ }
+ assert(last == TSRV_MSG_DETACH);
+
+ termsrv_reader_clear(&reader);
+ eina_binbuf_free(ts.chord_out);
+ _tt_tymux_small_close(&ts, peer);
+ termpty_free(ty);
+ free(paste);
+ return 0;
+}
+
+/* Test: control messages are bounded, and an EXIT the daemon sent before
+ * the client gave up on it is still read: the session ended, it was not
+ * detached. */
+int
+tytest_termtymux_send_exit_wins(void)
+{
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ TermSrvMsgExit exit_msg = { 3 };
+ size_t accepted = 0;
+ TermTymux ts;
+ int peer;
+
+ assert(ty);
+ termpty_backlog_size_set(ty, 32);
+ _tt_tymux_small_connect(&ts, ty, &peer);
+ ty->cb.exited.func = _tt_exited_cb;
+ _tt_exited_count = 0;
+
+ assert(termsrv_msg_send(peer, TSRV_MSG_EXIT,
+ &exit_msg, sizeof(exit_msg)) == 0);
+ while (_backlog_page_request(&ts, 1))
+ {
+ accepted += sizeof(TermSrvMsgHdr) + sizeof(TermSrvMsgBacklogPageId);
+ assert(accepted < 2 * OUT_QUEUE_MAX);
+ }
+ assert(accepted >= OUT_QUEUE_MAX);
+ assert(_tt_exited_count == 0);
+
+ _tt_exited_wait(5.0);
+ assert(_tt_exited_count == 1);
+ assert(termtymux_end_reason_get(&ts) == TERMTYMUX_END_EXITED);
+ assert(ty->exit_code == 3);
+
+ _tt_tymux_small_close(&ts, peer);
+ termpty_free(ty);
+ return 0;
+}
+
#endif /* BINARY_TYTEST */
diff --git a/src/bin/termtymux.h b/src/bin/termtymux.h
index 8a931543..a6bdb167 100644
--- a/src/bin/termtymux.h
+++ b/src/bin/termtymux.h
@@ -25,7 +25,8 @@ typedef struct _TermTymux TermTymux;
* exit. */
typedef enum
{
- TERMTYMUX_END_DETACHED, /* our own chord, or the daemon/connection gone */
+ TERMTYMUX_END_DETACHED, /* our own chord, the daemon/connection gone, or
+ * the daemon not taking what we send */
TERMTYMUX_END_EXITED /* TSRV_MSG_EXIT: the session's shell exited */
} TermTymuxEndReason;
@@ -37,6 +38,11 @@ struct _TermTymux
Termpty *shadow_pty;
Ecore_Fd_Handler *handler;
TermSrvReader reader;
+ Eina_Binbuf *out; /* messages the socket could not take yet */
+ Eina_Binbuf *input; /* input waiting to be framed into out */
+ size_t input_off; /* how much of input is framed already */
+ Ecore_Timer *out_timer; /* the daemon stuck taking nothing of out */
+ Eina_Bool send_failed; /* nothing goes out, the detach is coming */
TermSrvCell *frame_cells; /* scratch buffer for termtymux_frame_read() */
size_t frame_cells_cap; /* cells frame_cells holds */
const char *name; /* eina_stringshare; the session name */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 8bb76abd..40b7b4d7 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -241,6 +241,15 @@ static struct {
{ "termtymux_input_action_get", tytest_termtymux_input_action_get},
{ "termtymux_input_filter_long", tytest_termtymux_input_filter_long},
{ "termtymux_title_compose", tytest_termtymux_title_compose},
+ { "termtymux_send_partial", tytest_termtymux_send_partial},
+ { "termtymux_send_stuck", tytest_termtymux_send_stuck},
+ { "termtymux_send_stuck_flush", tytest_termtymux_send_stuck_flush},
+ { "termtymux_send_detach_drops_input",
+ tytest_termtymux_send_detach_drops_input},
+ { "termtymux_send_paste", tytest_termtymux_send_paste},
+ { "termtymux_send_control_first",
+ tytest_termtymux_send_control_first},
+ { "termtymux_send_exit_wins", tytest_termtymux_send_exit_wins},
#endif
{ NULL, NULL},
};
@@ -264,6 +273,7 @@ static const struct {
{ "tymux_live_scrollback_zero", tytest_tymux_live_scrollback_zero},
{ "tymux_live_fd_budget", tytest_tymux_live_fd_budget},
{ "tymux_live_page_async", tytest_tymux_live_page_async},
+ { "tymux_live_paste", tytest_tymux_live_paste},
#endif
{ NULL, NULL},
};
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index bccfd524..6b6b7910 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -1787,3 +1787,78 @@ tytest_tymux_live_page_async(void)
{
return tytest_tymux_live_run(_live_page_async_body);
}
+
+/* lines short enough for the tty's line buffer */
+#define PASTE_LINE 64
+#define PASTE_SIZE (2u << 20)
+
+static Eina_Bool
+_logical_line_done(Termpty *ty, const char *text)
+{
+ Eina_List *lines = _logical_lines(ty);
+ Eina_Bool found = EINA_FALSE;
+ char *line;
+
+ EINA_LIST_FREE(lines, line)
+ {
+ if (!strcmp(line, text)) found = EINA_TRUE;
+ free(line);
+ }
+ return found;
+}
+
+static Eina_Bool
+_paste_ready_done(void *data)
+{
+ return _logical_line_done(data, "ready-2");
+}
+
+static Eina_Bool
+_paste_counted_done(void *data)
+{
+ char count[32];
+
+ snprintf(count, sizeof(count), "%u", PASTE_SIZE);
+ return _logical_line_done(data, count);
+}
+
+/* A paste much bigger than what the client queues for control messages
+ * reaches the shell whole, and the client stays attached. */
+static int
+_live_paste_body(void)
+{
+ TermTymux *ts;
+ Termpty *ty;
+ char *paste = malloc(PASTE_SIZE);
+ size_t k;
+
+ assert(paste);
+ for (k = 0; k < PASTE_SIZE; k++)
+ paste[k] = (k % PASTE_LINE == PASTE_LINE - 1) ? '\n' : 'a' + k % 26;
+ assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+ termtymux_resize(ts, 80, SCREEN_ROWS);
+ assert(tytest_tymux_wait(_screen_rows_done, ty, 5.0));
+ assert(tytest_tymux_wait(_prompt_done, ty, 5.0));
+ assert(tytest_tymux_type(ts, ty,
+ "stty -echo; echo ready-$((1+1)); wc -c; "
+ "stty echo\n"));
+ assert(tytest_tymux_wait(_paste_ready_done, ty, 5.0));
+
+ ty->paste_active = 1;
+ termpty_write(ty, paste, PASTE_SIZE);
+ ty->paste_active = 0;
+ /* end of file for wc, at the start of a line */
+ assert(tytest_tymux_type(ts, ty, "\x04"));
+ assert(tytest_tymux_wait(_paste_counted_done, ty, 30.0));
+ assert(ts->sock_fd >= 0 && !ts->send_failed);
+
+ tytest_tymux_stop(ts);
+ free(paste);
+ return 0;
+}
+
+int
+tytest_tymux_live_paste(void)
+{
+ return tytest_tymux_live_run(_live_paste_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
index e85a36a1..0b422606 100644
--- a/src/bin/tytest_tymux.h
+++ b/src/bin/tytest_tymux.h
@@ -55,5 +55,6 @@ int tytest_tymux_live_scrollback_config(void);
int tytest_tymux_live_scrollback_zero(void);
int tytest_tymux_live_fd_budget(void);
int tytest_tymux_live_page_async(void);
+int tytest_tymux_live_paste(void);
#endif
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 65ab529b..97fa8a04 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -194,6 +194,13 @@ int tytest_termtymux_backlog_ring_size(void);
int tytest_termtymux_input_action_get(void);
int tytest_termtymux_input_filter_long(void);
int tytest_termtymux_title_compose(void);
+int tytest_termtymux_send_partial(void);
+int tytest_termtymux_send_stuck(void);
+int tytest_termtymux_send_stuck_flush(void);
+int tytest_termtymux_send_detach_drops_input(void);
+int tytest_termtymux_send_paste(void);
+int tytest_termtymux_send_control_first(void);
+int tytest_termtymux_send_exit_wins(void);
#endif
#endif
diff --git a/tests/meson.build b/tests/meson.build
index d7ca73f4..28c5662c 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -105,7 +105,7 @@ if tests
'tymux_live_compress', 'tymux_live_resize_no_dup',
'tymux_live_clear', 'tymux_live_scrollback_config',
'tymux_live_scrollback_zero', 'tymux_live_fd_budget',
- 'tymux_live_page_async'],
+ 'tymux_live_page_async', 'tymux_live_paste'],
depends: [tymuxd],
timeout: 120)
endif
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.