From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 Received: (qmail 9377 invoked from network); 3 Apr 2021 15:51:47 -0000 Received: from 1ess.inri.net (216.126.196.35) by inbox.vuxu.org with ESMTPUTF8; 3 Apr 2021 15:51:47 -0000 Received: from mail-il1-f172.google.com ([209.85.166.172]) by 1ess; Sat Apr 3 00:58:25 -0400 2021 Received: by mail-il1-f172.google.com with SMTP id c18so2245597iln.7 for <9front@9front.org>; Fri, 02 Apr 2021 21:58:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=HoTOx4PhjWuja+pmMfE3yQH73JGbJph7/tP2S4jNQ4s=; b=Z88xR9EH/6fON/sA3eNjHs0zIgkiaNC24VCihpSkY30206Y6EiCqF4pPEEb49eaHBa AZEJ6Bt760SsOcsUzrWHKUBrOsaXncPCbP/kYikTXBzwfnRfDO/TJudouTCN1HJ69vaO OEu4X+CjD3fBlNlvU3JLOvalhsW0Flr3xDgBRaMosZgP76FtCVPr5A1ndpTJhc8qzcHY BrCqK/OMh8jUfHCByho5JtpIBqAp7v9AaGi+VaXF4YEyfsbtf9eEc57a9vT6YuBu/oBY lfJHeWCbVNF2HHa/yup3VjvUs6tg0sXiHPXzTfA0WfiA2G1Yny68H40rSJj1/d+PhYUV TKxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:message-id:date:to; bh=HoTOx4PhjWuja+pmMfE3yQH73JGbJph7/tP2S4jNQ4s=; b=nvAueMLDzVcALKvOP47Xx+7RZ5Rt7HAmbB2fqaUck4nlNSxDgActk05K1OCj4By9Xz b6jg72MHDcIo2TUx0sNQTZwWHdlEWBRjViQ0ITRNo8CsNj24eWIsFLF3sGTqp9LZJHsp RH72oK5DpFfwzD3o/RTeFwFhG9iklQj3IYhyYS9KcXYXwAxqcQT11KKj8mxtLpV+MVMm 6jTm+wf/D7LI3MNukShGck9nKmsjqrnH6L2bV9cHGERFI877Jj2/Klvhb2e88wAcBmi+ TtRcLpqBJYQd5ye/QoQGDwGp4WyuBVN3n+TLdJ/qgPmPlcijea4u0gJDzZVuTKYl8x08 Totw== X-Gm-Message-State: AOAM531o6Px3L4o26Gus3ggJ+8Wz2iLtgPZlHzXT7ETipqjIX1V/Yfrc 8KsBtxgBdJjfm5bFANQvyF9lbb/wm5A= X-Google-Smtp-Source: ABdhPJyV713SNjN8IGsGS7cA390nD+s0/HGPaR6PjIcDbM8ddrYWQCLzMYayQ/HUDG4ubIGZLJ7AAA== X-Received: by 2002:a05:6602:2056:: with SMTP id z22mr13535243iod.21.1617425475107; Fri, 02 Apr 2021 21:51:15 -0700 (PDT) Return-Path: Received: from [10.0.1.7] (c-67-184-39-205.hsd1.il.comcast.net. [67.184.39.205]) by smtp.gmail.com with ESMTPSA id t1sm5412274ilj.5.2021.04.02.21.51.14 for <9front@9front.org> (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Apr 2021 21:51:14 -0700 (PDT) From: Xiao-Yong Jin Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Message-Id: <128FD9E8-194B-4391-82A1-BB69ED4951BA@gmail.com> Date: Fri, 2 Apr 2021 23:51:13 -0500 To: 9front@9front.org X-Mailer: Apple Mail (2.3654.60.0.2.21) List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: package persistence-based ActivityPub over SQL just-in-time realtime-java grid-scale database Subject: [9front] acme/win: avoid winsetaddr error killing the process, reorder text for eventual consistency Reply-To: 9front@9front.org Precedence: bulk The issue stems from keyboard/mouse insertion/deletion happening at the same time as the process writing the output. When the S message from our output handling process comes while acme handles input, the variable hostpt no longer points to the actual output location. In this case, the K/M D/I messages coming from acme would be after the S message, but before the F message induced by the write in S message handling code. The patch avoid bad messages in S, and correct the output ordering in F. The change in the line with if(!winsetaddr(w, tmp, 1)) protects the process from getting killed because of an out of range address. The call to reorder corrects the wrong stdout text order due to outdated hostpt. To reproduce: 0. start a win in acme 1. execute inside the win window: while()echo ahhhh 2. try mouse/keyboard input/delete some text from the win window. - Middle-click 'Edit ,d' - 1-2 chord a range of text diff -r cc26fdf57bb2 acme/bin/source/win/main.c --- a/acme/bin/source/win/main.c Sat Mar 13 14:57:53 2021 +0100 +++ b/acme/bin/source/win/main.c Fri Apr 02 23:27:19 2021 -0500 @@ -377,6 +377,45 @@ free(s); } =20 +static void +reorder(Window *w, ulong hostpt, ulong s0, ulong s1) +{ + /* If insertion/deletion happens between the S and F messages, + * acme likely handels the insertion/deletion before the write + * with a dated hostpt in S. When we get the M/K D/I messages + * it is already too late, the order of stdout could be = scrambled. + * We use the info in addr to reorder the text. + */ + int n; + char tmp[32], *s; + + if(debug) + fprint(2, "reorder range %lud-%lud to %lud\n", s0, s1, = hostpt); + if(hostpt < s0){ + /* deletion happened before S */ + s =3D emalloc((s0-hostpt)*UTFmax+1); + n =3D winread(w, hostpt, s0, s); + sprint(tmp, "#%lud,#%lud", hostpt, s0); + winsetaddr(w, tmp, 0); + write(w->data, s, 0); + sprint(tmp, "#%lud", hostpt+s1-s0); + winsetaddr(w, tmp, 0); + write(w->data, s, n); + free(s); + }else if(s0 < hostpt){ + /* insertion happened before S */ + s =3D emalloc((hostpt-s0)*UTFmax+1); + n =3D winread(w, s1, hostpt+s1-s0, s); + sprint(tmp, "#%lud,#%lud", s1, hostpt+s1-s0); + winsetaddr(w, tmp, 0); + write(w->data, s, 0); + sprint(tmp, "#%lud", s0); + winsetaddr(w, tmp, 0); + write(w->data, s, n); + free(s); + } +} + int hasboundary(Rune *r, int nr) { @@ -427,7 +466,14 @@ =20 case 'S': /* output to stdout */ sprint(tmp, "#%lud", hostpt); - winsetaddr(w, tmp, 0); + if(!winsetaddr(w, tmp, 1)){ /* Ignore error = from winsetaddr for S msg could arrive before K/M D/I. */ + winsetaddr(w, "$", 0); + if(debug){ + seek(w->addr, 0UL, 0); + if(read(w->addr, tmp, 2*12) =3D=3D= 2*12) + fprint(2, "reset output = to the end #%lud\n", atol(tmp)); + } + } write(w->data, e->b, e->nb); pendingS +=3D e->nr; break; @@ -453,12 +499,11 @@ /* we know about the delete by = _sendinput */ break; } - if(e->c2=3D=3D'I'){ - pendingS -=3D e->q1 - e->q0; - if(pendingS < 0) - fprint(2, "win: pendingS =3D = %d\n", pendingS); + /* If pendingS differ, we likely originated from = reorder. */ + if(e->c2=3D=3D'I' && pendingS =3D=3D e->q1 - = e->q0){ + pendingS =3D 0; if(e->q0 !=3D hostpt) - fprint(2, "win: insert at %d = expected %lud\n", e->q0, hostpt); + reorder(w, hostpt, e->q0, = e->q1); endpt +=3D delta; hostpt +=3D delta; sendp(writechan, nil);