SBCL crashes when run-program is called repeatedly

Bug #1979841 reported by Thor Kristoffersen
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Fix Released
Undecided
Unassigned

Bug Description

Test case: The following function calls run-program repeatedly.

(defun bug (n)
  (dotimes (i n)
    (format t "~6D~%" i)
    (sb-ext:run-program "/usr/bin/touch" (list "/dev/null") :output :stream)))

When calling (bug 10000), SBCL crashes at iteration 1016 like this:

  fatal error encountered in SBCL pid 275652 tid 275652:
  can't run-program

  Welcome to LDB, a low-level debugger for the Lisp runtime environment.
  ldb> ba
  Backtrace:
     0: fp=0x7fd2e5bff140 pc=0x563afa1d1851 Foreign function (null)
     1: fp=0x7fd2e5bff2a0 pc=0x563afa1d2317 Foreign function ldb_monitor
     2: fp=0x7fd2e5bff2b0 pc=0x563afa1cd34a Foreign function (null)
     3: fp=0x7fd2e5bff3a0 pc=0x563afa1cd4e3 Foreign function (null)
     4: fp=0x7fd2e5bff490 pc=0x563afa1dc3e1 Foreign function spawn
     5: fp=0x7fd2e5bff590 pc=0x52f41b79 SB-IMPL::SPAWN
     6: fp=0x7fd2e5bff618 pc=0x52a2b220 (FLET "WITHOUT-INTERRUPTS-BODY-5" :IN SB-THREAD::CALL-WITH-SYSTEM-MUTEX)
     7: fp=0x7fd2e5bff6a0 pc=0x52a2b039 SB-THREAD::CALL-WITH-SYSTEM-MUTEX
     8: fp=0x7fd2e5bff8e8 pc=0x534281de SB-EXT::RUN-PROGRAM
     9: fp=0x7fd2e5bff920 pc=0x5342b33c CL-USER::BUG
    10: fp=0x7fd2e5bff9e8 pc=0x52b4c4b7 SB-INT::SIMPLE-EVAL-IN-LEXENV
    11: fp=0x7fd2e5bffa00 pc=0x52a49e23 EVAL
    12: fp=0x7fd2e5bffa50 pc=0x52ba0955 SB-EXT::INTERACTIVE-EVAL
    13: fp=0x7fd2e5bffaa0 pc=0x532c4724 SB-IMPL::REPL-FUN
    14: fp=0x7fd2e5bffba0 pc=0x52ba2b6b (LAMBDA () :IN SB-IMPL::TOPLEVEL-REPL)
    15: fp=0x7fd2e5bffc10 pc=0x52e347c8 SB-IMPL::%WITH-REBOUND-IO-SYNTAX
    16: fp=0x7fd2e5bffca8 pc=0x52ba289d SB-IMPL::TOPLEVEL-REPL
    17: fp=0x7fd2e5bffdc0 pc=0x52ba1eaa SB-IMPL::TOPLEVEL-INIT
    18: fp=0x7fd2e5bffe60 pc=0x52f458a9 (FLET SB-UNIX::BODY :IN SB-IMPL::START-LISP)
    19: fp=0x7fd2e5bfff28 pc=0x52f456a7 (FLET "WITHOUT-INTERRUPTS-BODY-3" :IN SB-IMPL::START-LISP)
    20: fp=0x7fd2e5bfffc8 pc=0x52f45453 SB-IMPL::%START-LISP

It doesn't matter what program is run by the function.
If :output is nil, the bug is not triggered.

SBCL version: 2.2.5

> uname -a
Linux calvin 5.16.0-0.bpo.4-amd64 #1 SMP PREEMPT Debian 5.16.12-1~bpo11+1 (2022-03-08) x86_64 GNU/Linux

*features*
(:X86-64 :GENCGC :64-BIT :ANSI-CL :COMMON-LISP :ELF :IEEE-FLOATING-POINT :LINUX
 :LITTLE-ENDIAN :PACKAGE-LOCAL-NICKNAMES :SB-LDB :SB-PACKAGE-LOCKS :SB-THREAD
 :SB-UNICODE :SBCL :UNIX)

Revision history for this message
Stas Boukarev (stassats) wrote :

You are running out of FDs for the streams. You need

(let ((process (sb-ext:run-program "/usr/bin/touch" (list "/dev/null") :output :stream)))
  (sb-ext:process-close process))

Revision history for this message
Stas Boukarev (stassats) wrote :

And you shouldn't use :wait t with :output :stream, when the pipe buffer fills up it'll get stuck.

Revision history for this message
Thor Kristoffersen (thorkr) wrote :

Yes, that fixes it. The bug was indeed mine. Thanks a lot, Stas!

Revision history for this message
Tomas Hlavaty (q-tom-o) wrote : Re: [Bug 1979841] Re: SBCL crashes when run-program is called repeatedly

On Fri 24 Jun 2022 at 23:28, Stas Boukarev <email address hidden> wrote:
> You are running out of FDs for the streams. You need

But he is not capturing the streams so if GC happened earlier than
running out of FDs, finalizers would do their job and this issue would
not happen.

Ideally, running out of FDs would be detected, trigger gc, run
finalizers and retry the failed open. Only if the second try failed
again, all hope is lost.

Revision history for this message
Douglas Katzman (dougk) wrote :

pretty sure the only ask here was not to crash so hard. A solution based on a super-smart garbage collector is out of the question.
Fixed by https://sourceforge.net/p/sbcl/sbcl/ci/e574a5bedf

Changed in sbcl:
status: New → Fix Committed
Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.