9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Xiao-Yong Jin <meta.jxy@gmail.com>
To: 9front@9front.org
Subject: [9front] acme/win: avoid winsetaddr error killing the process, reorder text for eventual consistency
Date: Fri, 2 Apr 2021 23:51:13 -0500	[thread overview]
Message-ID: <128FD9E8-194B-4391-82A1-BB69ED4951BA@gmail.com> (raw)

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);
 }
 
+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 = emalloc((s0-hostpt)*UTFmax+1);
+		n = 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 = emalloc((hostpt-s0)*UTFmax+1);
+		n = 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 @@
 
 		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) == 2*12)
+						fprint(2, "reset output to the end #%lud\n", atol(tmp));
+				}
+			}
 			write(w->data, e->b, e->nb);
 			pendingS += e->nr;
 			break;
@@ -453,12 +499,11 @@
 				/* we know about the delete by _sendinput */
 				break;
 			}
-			if(e->c2=='I'){
-				pendingS -= e->q1 - e->q0;
-				if(pendingS < 0)
-					fprint(2, "win: pendingS = %d\n", pendingS);
+			/* If pendingS differ, we likely originated from reorder. */
+			if(e->c2=='I' && pendingS == e->q1 - e->q0){
+				pendingS = 0;
 				if(e->q0 != hostpt)
-					fprint(2, "win: insert at %d expected %lud\n", e->q0, hostpt);
+					reorder(w, hostpt, e->q0, e->q1);
 				endpt += delta;
 				hostpt += delta;
 				sendp(writechan, nil);


                 reply	other threads:[~2021-04-03 15:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=128FD9E8-194B-4391-82A1-BB69ED4951BA@gmail.com \
    --to=meta.jxy@gmail.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).