test-signal-fork 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. ;; Test for one of the bugs described at <https://bugs.gnu.org/41948>:
  23. ;; when forking a Guile process that has its signal thread up and
  24. ;; running, the 'sleep_pipe' of the main thread would end up being
  25. ;; shared between the child and parent processes, leading to a race
  26. ;; condition. This test checks for the presence of that race condition.
  27. (use-modules (ice-9 match))
  28. (unless (provided? 'fork)
  29. (exit 77))
  30. (setvbuf (current-output-port) 'none)
  31. (sigaction SIGCHLD pk) ;start signal thread
  32. (match (primitive-fork)
  33. (0
  34. (format #t "child: ~a~%" (getpid))
  35. (unless (zero? (sleep 5))
  36. ;; If this happens, it means the select(2) call in 'scm_std_select'
  37. ;; returned because one of our file descriptors had input data
  38. ;; available (which shouldn't happen).
  39. (format #t "child woken up!~%")
  40. ;; Terminate the parent so the test fails.
  41. (kill (getppid) SIGKILL)
  42. (primitive-exit 1)))
  43. (pid
  44. (format #t "parent: ~a~%" (getpid))
  45. (sigaction SIGALRM (lambda _
  46. (display ".")))
  47. ;; Repeatedly send signals to self. Previously, the thread's
  48. ;; 'sleep_pipe' would wrongfully be shared between the parent and the
  49. ;; child, leading to a race condition: the child could end up reading
  50. ;; from the pipe in lieu of the parent.
  51. (let loop ((i 50))
  52. (kill (getpid) SIGALRM)
  53. (usleep 50000)
  54. (unless (zero? i)
  55. (loop (1- i))))
  56. ;; Terminate the child.
  57. (false-if-exception (kill pid SIGKILL))
  58. (format #t "~%completed~%")))