From c8e9ab4ca91a0d81063c271c4353da656f827b6b Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 12 Mar 2024 09:40:51 +0100 Subject: [PATCH] kernel: kill subprocess before closing pipe to it (#5666) Otherwise we see issues like this on Linux: gap> d := DirectoryCurrent();; gap> f := Filename(DirectoriesSystemPrograms(), "rev");; gap> s := InputOutputLocalProcess(d,f,[]);; gap> Sleep(1); gap> CloseStream(s); Print("\n"); rev: stdin --- src/iostream.c | 2 +- tst/testspecial/child-process.g | 5 +++++ tst/testspecial/child-process.g.out | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tst/testspecial/child-process.g create mode 100644 tst/testspecial/child-process.g.out diff --git a/src/iostream.c b/src/iostream.c index 60ac7db611..18db099015 100644 --- a/src/iostream.c +++ b/src/iostream.c @@ -1010,10 +1010,10 @@ static Obj FuncCLOSE_PTY_IOSTREAM(Obj self, Obj stream) // Close down the child int status; + kill(PtyIOStreams[pty].childPID, SIGTERM); int retcode = close(PtyIOStreams[pty].ptyFD); if (retcode) Pr("Strange close return code %d\n", retcode, 0); - kill(PtyIOStreams[pty].childPID, SIGTERM); // GAP (or another library) might wait on this PID before // we handle it. If that happens, waitpid will return -1. retcode = waitpid(PtyIOStreams[pty].childPID, &status, WNOHANG); diff --git a/tst/testspecial/child-process.g b/tst/testspecial/child-process.g new file mode 100644 index 0000000000..caf1f71ae8 --- /dev/null +++ b/tst/testspecial/child-process.g @@ -0,0 +1,5 @@ +d := DirectoryCurrent();; +f := Filename(DirectoriesSystemPrograms(), "rev");; +s := InputOutputLocalProcess(d,f,[]);; +Sleep(1); +CloseStream(s); Print("\n"); diff --git a/tst/testspecial/child-process.g.out b/tst/testspecial/child-process.g.out new file mode 100644 index 0000000000..27291b989a --- /dev/null +++ b/tst/testspecial/child-process.g.out @@ -0,0 +1,7 @@ +gap> d := DirectoryCurrent();; +gap> f := Filename(DirectoriesSystemPrograms(), "rev");; +gap> s := InputOutputLocalProcess(d,f,[]);; +gap> Sleep(1); +gap> CloseStream(s); Print("\n"); + +gap> QUIT;