Skip to content

Commit 1b82fc6

Browse files
committed
Merge remote-tracking branch 'origin/fast/integration' into backplane/far-feed-lazy
2 parents acc7523 + 0e97abe commit 1b82fc6

3 files changed

Lines changed: 102 additions & 18 deletions

File tree

‎src/server/effects/pty.js‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ function bp_conpty_lib() {
298298
GetExitCodeProcess: { args: ["ptr", "ptr"], returns: "i32" },
299299
TerminateProcess: { args: ["ptr", "u32"], returns: "i32" },
300300
GetLastError: { args: [], returns: "u32" },
301+
SetNamedPipeHandleState: { args: ["ptr", "ptr", "ptr", "ptr"], returns: "i32" },
301302
}).symbols;
302303
globalThis.BP_CONPTY = { k, ptr: ffi.ptr, read: ffi.read };
303304
return globalThis.BP_CONPTY;
@@ -357,6 +358,11 @@ function bp_conpty(argv, cwd, env, cols, rows) {
357358
throw fail("CreatePseudoConsole");
358359
}
359360
const pc = handle(hpc);
361+
// the input pipe never blocks the hub: PIPE_NOWAIT makes WriteFile take
362+
// what fits and answer at once; the rest waits in a queue the 15 ms
363+
// timer drains (a big paste into a program that is not reading stalled
364+
// the whole loop on a blocking write)
365+
k.SetNamedPipeHandleState(handle(inW), ptr(new Uint32Array([1])), null, null);
360366
const need = new BigUint64Array(1);
361367
k.InitializeProcThreadAttributeList(null, 1, 0, ptr(need));
362368
const list = new Uint8Array(Number(need[0]) + 64);
@@ -449,6 +455,7 @@ function bp_conpty(argv, cwd, env, cols, rows) {
449455
}
450456
};
451457
const tick = () => {
458+
pump();
452459
// what the console wrote, then whether the process ended
453460
while (k.PeekNamedPipe(handle(outR), null, 0, null, ptr(avail), null) && avail[0] > 0) {
454461
if (!k.ReadFile(handle(outR), ptr(buf), Math.min(avail[0], buf.length), ptr(n), null) || n[0] === 0) {
@@ -474,13 +481,35 @@ function bp_conpty(argv, cwd, env, cols, rows) {
474481
}
475482
};
476483
const timer = setInterval(tick, 15);
484+
const queue = [];
485+
const wrote = new Uint32Array(1);
486+
const pump = () => {
487+
while (queue.length > 0 && !term.closed) {
488+
const head = queue[0];
489+
const part = head.subarray(0, 4096);
490+
if (!k.WriteFile(handle(inW), ptr(part), part.length, ptr(wrote), null)) {
491+
// the console is gone (ERROR_NO_DATA): what is left goes nowhere
492+
queue.length = 0;
493+
return;
494+
}
495+
if (wrote[0] === 0) {
496+
return;
497+
}
498+
if (wrote[0] >= head.length) {
499+
queue.shift();
500+
} else {
501+
queue[0] = head.subarray(wrote[0]);
502+
}
503+
}
504+
};
477505
term.write = (b) => {
478506
if (term.closed) {
479507
return 0;
480508
}
481-
const u = typeof b === "string" ? Buffer.from(b) : b;
482-
k.WriteFile(handle(inW), ptr(u), u.length, ptr(n), null);
483-
return n[0];
509+
const u = typeof b === "string" ? Buffer.from(b) : new Uint8Array(b);
510+
queue.push(u);
511+
pump();
512+
return u.length;
484513
};
485514
term.resize = (c, r) => {
486515
if (!term.closed) {

‎test/js_host_effects_test.bend‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ def procs(+dir: String) -> IO(Unit):
122122
do IO<Unit>:
123123
r1 : P.Ran <- sh("echo hi; exit 3")
124124
C.Check.str("run status and output", ran.show(r1), "3:hi")
125-
r2 : P.Ran <- P.Proc.run("pwd", [], dir)
126-
C.Check.str("run in cwd", ran.show(r2), "0:" ++ dir)
125+
+r2 : P.Ran <- P.Proc.run("pwd", [], dir)
126+
# the folder's own path, whatever is in front of it (Cygwin's pwd on
127+
# Windows says /cygdrive/c/tmp/... for /tmp/...)
128+
C.Check.str("run in cwd", Bool.show(Bool.and(String.starts_with(ran.show(r2), "0:"), String.ends_with(ran.show(r2), String.drop(dir, 4n)))), "True")
127129
s3 : Result<&1, &1, U32 & String, U32 & Socket> <- H.Proc.spawn("/nonexistent/bp-js-x", [], "", 0)
128130
c3 : String <- spawned.code(s3)
129131
C.Check.str("spawn of a missing program fails ENOENT", c3, "fail 2")

‎test/js_pty_test.bend‎

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ def Rd.go(fuel: Nat, s: Socket, st: Rd, +want: U32) -> IO(Socket & Rd):
131131
IO.bind(Socket & Result<&1, &1, U32 & String, List<&2, U32>>, Socket & Rd, H.Sock.recv_bytes(s, 65536),
132132
r => Rd.got(r, st, want, s2 => st2 => Rd.go(f, s2, st2, want)))
133133

134+
# reading until the screen shows text (a byte count is not enough: a
135+
# Windows pseudoconsole sends mode escapes before the program's first line)
136+
def Rd.till.k(+st: Rd, s: Socket, +text: String, k: Socket -> Rd -> IO(Socket & Rd)) -> IO(Socket & Rd):
137+
match st:
138+
case Rd{+v, +n}:
139+
Rd.more(has(screen(v), text), s, Rd{v, n}, k)
140+
141+
def Rd.till.got(r: Socket & Result<&1, &1, U32 & String, List<&2, U32>>, st: Rd, +text: String, k: Socket -> Rd -> IO(Socket & Rd)) -> IO(Socket & Rd):
142+
(s, res) = r
143+
match res st:
144+
case Done{Nil{}} _:
145+
IO.pure(Socket & Rd, (s, st))
146+
case Done{bs} Rd{v, +n}:
147+
Rd.till.k(Rd.fed(v, n, bs), s, text, k)
148+
case Fail{_} _:
149+
IO.pure(Socket & Rd, (s, st))
150+
151+
def Rd.till(fuel: Nat, s: Socket, st: Rd, +text: String) -> IO(Socket & Rd):
152+
match fuel:
153+
case 0n:
154+
IO.pure(Socket & Rd, (s, st))
155+
case 1n+f:
156+
IO.bind(Socket & Result<&1, &1, U32 & String, List<&2, U32>>, Socket & Rd, H.Sock.recv_bytes(s, 65536),
157+
r => Rd.till.got(r, st, text, s2 => st2 => Rd.till(f, s2, st2, text)))
158+
134159
def Rd.vt(st: Rd) -> V.Vt:
135160
match st:
136161
case Rd{v, _}:
@@ -199,7 +224,7 @@ def case.size.more(r: Socket & V.Vt, +pid: U32) -> IO(U32):
199224
def case.size.k(started: U32 & Socket) -> IO(U32):
200225
(+pid, s) = started
201226
do IO<U32>:
202-
r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.go(1000n, s, Rd{V.Vt.new(100, 30), 0}, 6), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
227+
r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.till(1000n, s, Rd{V.Vt.new(100, 30), 0}, "24 80"), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
203228
a : U32 <- case.size.more(r1, pid)
204229
return a
205230

@@ -229,27 +254,35 @@ def case.env(r: Result<&1, &1, U32 & String, U32 & Socket>) -> IO(U32):
229254
case Fail{e}:
230255
fail("pty env", e)
231256

232-
# closing the master hangs up the shell (SIGHUP: 129)
233-
def case.close.more(r: Socket & V.Vt, +pid: U32) -> IO(U32):
257+
# closing the master hangs up the shell (SIGHUP: 129; on Windows the
258+
# pseudoconsole closes and the shell ends with a code of its own)
259+
def case.close.code(win: Bool, +code: U32) -> IO(U32):
260+
match win:
261+
case True{}:
262+
check("pty close ends the child", Bool.show(Bool.not(U32.is_eq(code, 0))), "True")
263+
case False{}:
264+
check("pty close kills child", U32.show(code), "129")
265+
266+
def case.close.more(r: Socket & V.Vt, +pid: U32, win: Bool) -> IO(U32):
234267
(s, +v) = r
235268
do IO<U32>:
236269
Socket.close(s)
237270
code : U32 <- H.Proc.wait(pid)
238271
a : U32 <- check.has("pty child up", screen(v), "up")
239-
b : U32 <- check("pty close kills child", U32.show(code), "129")
272+
b : U32 <- case.close.code(win, code)
240273
return (a + b : U32)
241274

242-
def case.close.k(started: U32 & Socket) -> IO(U32):
275+
def case.close.k(started: U32 & Socket, win: Bool) -> IO(U32):
243276
(+pid, s) = started
244277
do IO<U32>:
245-
r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.go(1000n, s, Rd{V.Vt.new(80, 24), 0}, 3), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
246-
a : U32 <- case.close.more(r1, pid)
278+
r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.till(1000n, s, Rd{V.Vt.new(80, 24), 0}, "up"), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
279+
a : U32 <- case.close.more(r1, pid, win)
247280
return a
248281

249-
def case.close(r: Result<&1, &1, U32 & String, U32 & Socket>) -> IO(U32):
282+
def case.close(win: Bool, r: Result<&1, &1, U32 & String, U32 & Socket>) -> IO(U32):
250283
match r:
251284
case Done{started}:
252-
case.close.k(started)
285+
case.close.k(started, win)
253286
case Fail{e}:
254287
fail("pty close", e)
255288

@@ -267,7 +300,7 @@ def case.hup.more(r: Socket & V.Vt, +pid: U32) -> IO(U32):
267300
def case.hup.k(started: U32 & Socket) -> IO(U32):
268301
(+pid, s) = started
269302
do IO<U32>:
270-
r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.go(1000n, s, Rd{V.Vt.new(80, 24), 0}, 3), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
303+
r1 : Socket & V.Vt <- IO.bind(Socket & Rd, Socket & V.Vt, Rd.till(1000n, s, Rd{V.Vt.new(80, 24), 0}, "up"), r => IO.pure(Socket & V.Vt, Rd.keep(r)))
271304
a : U32 <- case.hup.more(r1, pid)
272305
return a
273306

@@ -395,19 +428,39 @@ def done.k(ok: Bool, +n: U32) -> IO(Unit):
395428
def done(+n: U32) -> IO(Unit):
396429
done.k(U32.is_eq(n, 0), n)
397430

431+
# a case that needs a POSIX host (/tmp as a path, /proc), named as skipped on
432+
# Windows
433+
def posix.only(win: Bool, name: String, run: IO(U32)) -> IO(U32):
434+
match win:
435+
case True{}:
436+
do IO<U32>:
437+
IO.print("ok " ++ name ++ " skipped on Windows")
438+
return 0
439+
case False{}:
440+
run
441+
442+
def is.win(r: Result<&1, &1, U32 & String, String>) -> Bool:
443+
match r:
444+
case Done{os}:
445+
String.eq(os, "Windows_NT")
446+
case Fail{_}:
447+
False{}
448+
398449
def main() -> IO(Unit):
399450
do IO<Unit>:
400451
# the runtime's loop (loop.js) is part of the program, as in the hub:
401452
# on Windows nothing spawns without it
402453
rt : String <- LP.Rt.loop()
403454
IO.print("js pty: starting (" ++ rt ++ " loop)")
455+
os : Result<&1, &1, U32 & String, String> <- IO.get_env("OS")
456+
+win : Bool = is.win(os)
404457
a : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", [], "", 80, 24), case.shell)
405458
b : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "stty size; read x; stty size"], "", 80, 24), case.size)
406-
c : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo $TERM; echo $COLORTERM; echo $LINES$COLUMNS; pwd; [ -t 0 ] && printf tty; : </dev/tty && echo ' ctty'; echo $(ls /dev/fd/)"], "/tmp", 80, 24), case.env)
407-
d : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo up; sleep 30; echo done"], "", 80, 24), case.close)
459+
c : U32 <- posix.only(win, "pty env cwd ctty", IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo $TERM; echo $COLORTERM; echo $LINES$COLUMNS; pwd; [ -t 0 ] && printf tty; : </dev/tty && echo ' ctty'; echo $(ls /dev/fd/)"], "/tmp", 80, 24), case.env))
460+
d : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo up; sleep 30; echo done"], "", 80, 24), r => case.close(win, r))
408461
e : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("definitely-not-a-command", [], "", 80, 24), case.missing)
409462
f : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "true"], "/definitely/not/a/dir", 80, 24), case.badcwd)
410463
g : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "echo up; sleep 30; echo done"], "", 80, 24), case.hup)
411464
h : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "stty raw -echo; sleep 1; head -c 20000 | wc -c"], "", 80, 24), case.full)
412-
i : U32 <- IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "c=$(ulimit -s); p=$(awk '/Max stack size/{print $4}' /proc/$PPID/limits); if [ \"$p\" = unlimited ] || [ \"$p\" -gt 8388608 ]; then w=8192; else w=$((p/1024)); fi; [ \"$c\" = \"$w\" ] && echo stack-ok || echo \"child $c hub $p\""], "", 80, 24), case.stack)
465+
i : U32 <- posix.only(win, "pty stack limit", IO.bind(Result<&1, &1, U32 & String, U32 & Socket>, U32, P.Pty.spawn("sh", ["-c", "c=$(ulimit -s); p=$(awk '/Max stack size/{print $4}' /proc/$PPID/limits); if [ \"$p\" = unlimited ] || [ \"$p\" -gt 8388608 ]; then w=8192; else w=$((p/1024)); fi; [ \"$c\" = \"$w\" ] && echo stack-ok || echo \"child $c hub $p\""], "", 80, 24), case.stack))
413466
done((a + b + c + d + e + f + g + h + i : U32))

0 commit comments

Comments
 (0)