9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] acme/win: avoid winsetaddr error killing the process, reorder text for eventual consistency
@ 2021-04-03  4:51 Xiao-Yong Jin
  0 siblings, 0 replies; only message in thread
From: Xiao-Yong Jin @ 2021-04-03  4:51 UTC (permalink / raw)
  To: 9front

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);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-03 15:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-03  4:51 [9front] acme/win: avoid winsetaddr error killing the process, reorder text for eventual consistency Xiao-Yong Jin

9front - general discussion about 9front

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/9front

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 9front 9front/ http://inbox.vuxu.org/9front \
		9front@9front.org
	public-inbox-index 9front

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.9front


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git