Skip to content

Signal handing can result in re-entering the interpreter loop in an unsafe fashion. #695

Open

Description

@CheyenneWills

Ran into a problem where a program was being flooded with signals (SIGWINCH) that resulted in either a hang or a segfault.

I did a little digging and found the root cause. I don't have the time at the moment to write up a fix for it however.

The problem is that the signal_dispatcher() in rposix.r calls calliconproc() from inside the OS signal handler which reenters interp() at some arbitrary point with a stale sp.

The following can be used to reproduce the problem (the demo uses fork):

--[ bug.icn ] --

procedure handler(s)
end

procedure main()
   trap("SIGWINCH", handler)
   parentpid := getpid()
   if fork() = 0 then {
      while kill(parentpid, "SIGWINCH") 
      exit()
      }
   every i := 1 to 5000000 do
      x := [i, string(i)]
   write("finished normally")
end

$ git describe
13.2-1321-gb9f0fdd9

b9f0fdd Merge pull request #694 from Jafaral/runtime-ipl-fixes

With a freshly built unicon

bin/unicon bug.icn

gdb bin/iconx

(gdb) set args ./bug
(gdb) run

Starting program: /home/ccw/src/unicon-unicon/bin/iconx ./bug
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib64/libthread_db.so.1".
[Detaching after fork from child process 611583]
^C
Program received signal SIGINT, Interrupt.
0x00007ffff6ec3757 in __internal_syscall_cancel () from /usr/lib64/libc.so.6
(gdb) bt
#0  0x00007ffff6ec3757 in __internal_syscall_cancel () from /usr/lib64/libc.so.6
#1  0x00007ffff6ec3a2c in __futex_abstimed_wait_common () from /usr/lib64/libc.so.6
#2  0x00007ffff6ec62aa in pthread_cond_wait () from /usr/lib64/libc.so.6
#3  0x00005555555f1dca in thread_control ()
#4  0x00005555555cdc3e in interp_0 ()
#5  0x0000555555666f9e in calliconproc ()
#6  0x0000555555667154 in signal_dispatcher ()
#7  <signal handler called>
#8  0x00007ffff6eb1440 in fwrite () from /usr/lib64/libc.so.6
#9  0x0000555555674765 in longwrite ()
#10 0x000055555560a2f9 in putstr ()
#11 0x00005555555f5129 in tracebk ()
#12 0x000055555558b841 in err_msg ()
#13 0x00005555555d1411 in invoke ()
#14 0x00005555555cf9a8 in interp_0 ()
#15 0x00005555555e430c in Otoby ()
#16 0x00005555555cf7a9 in interp_0 ()
#17 0x0000555555587268 in main ()

Stack entries 4-7 show the problem where interp() reentered from calliconproc()

Stack entries 8-13 show the effect. A runtime error that should never have occurred which was the result of corruption from an earlier signal.

Possible solution is to have the signal_dispatcher() within rposix.r set 2 flags, one that there is a signal pending and a signal specific flag to indicate which signal was received. Then at the top of interp() do something like:

if (sigs_pending) {
     ExInterp_sp;
     run_pending_signals();
     EntInterp_sp;
}

The run_pending_signals() routine would clear the sigs_pending flag, check each signal specific flag if true then clear that flag and do the calliconproc with the appropriate trap handler with some guards to prevent nesting (any signal that comes in during a handler waits until the current handler is finished instead of doing a recursive call).

This changes when a trap handler is invoked. Instead of being invoked at the time of the interrupt (e.g. within a system call), the trap handler is only called at the top of the interp loop. Some signal processing would be delayed until after a blocking system call has finished.

Activity

  1. CheyenneWills commented on Nov 6, 2026

    @CheyenneWills
    Author

    It's not isolated to SIGWINCH, it's just that was the one that tripped the bug for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions