Debian Codegraph @

test-suite/standalone/test-signal-fork

raw
1 #!/bin/sh
2 guild compile "$0"
3 exec guile -q -s "$0" "$@"
4 !#
5 ;;; test-signal-fork --- Signal thread vs. fork. -*- Scheme -*-
6 ;;;
7 ;;; Copyright (C) 2021, 2022 Free Software Foundation, Inc.
8 ;;;
9 ;;; This library is free software; you can redistribute it and/or
10 ;;; modify it under the terms of the GNU Lesser General Public
11 ;;; License as published by the Free Software Foundation; either
12 ;;; version 3 of the License, or (at your option) any later version.
13 ;;;
14 ;;; This library is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;;; Lesser General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General Public
20 ;;; License along with this library; if not, write to the Free Software
21 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
23 ;; Test for one of the bugs described at <https://bugs.gnu.org/41948>:
24 ;; when forking a Guile process that has its signal thread up and
25 ;; running, the 'sleep_pipe' of the main thread would end up being
26 ;; shared between the child and parent processes, leading to a race
27 ;; condition. This test checks for the presence of that race condition.
28
29 (use-modules (ice-9 match))
30
31 (unless (provided? 'fork)
32 (exit 77))
33
34 (setvbuf (current-output-port) 'none)
35 (sigaction SIGCHLD pk) ;start signal thread
36
37 (match (primitive-fork)
38 (0
39 (format #t "child: ~a~%" (getpid))
40 (unless (zero? (sleep 5))
41 ;; If this happens, it means the select(2) call in 'scm_std_select'
42 ;; returned because one of our file descriptors had input data
43 ;; available (which shouldn't happen).
44 (format #t "child woken up!~%")
45
46 ;; Terminate the parent so the test fails.
47 (kill (getppid) SIGKILL)
48 (primitive-exit 1)))
49 (pid
50 (format #t "parent: ~a~%" (getpid))
51 (sigaction SIGALRM (lambda _
52 (display ".")))
53
54 ;; Repeatedly send signals to self. Previously, the thread's
55 ;; 'sleep_pipe' would wrongfully be shared between the parent and the
56 ;; child, leading to a race condition: the child could end up reading
57 ;; from the pipe in lieu of the parent.
58 (let loop ((i 50))
59 (kill (getpid) SIGALRM)
60 (usleep 50000)
61 (unless (zero? i)
62 (loop (1- i))))
63
64 ;; Terminate the child.
65 (false-if-exception (kill pid SIGKILL))
66 (format #t "~%completed~%")))