Run registered signal handlers also when a port read is interrupted?

Open
#923 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Domain
backend

Research direction

Start with the cited EINTR handling in c/new-io.c around line 494, then trace how its result reaches check-interrupt in s/io.ss around line 996. The issue proposes returning an interrupt result for EINTR so the Scheme event loop can process registered signal handlers. Done means reproducing the port-read behavior and confirming arbitrary signal handlers run when a blocking read is interrupted.

Written by the indexing model from the issue text.

Description

I am trying to use (register-signal-handler) on Chez Scheme 10.0.0 and Linux/x86_64
to install signal handlers for the POSIX signals SIGQUIT, SIGTSTP, SIGCHLD and possibly others,
and I noticed an inconsistent behavior:

the installed handlers are called immediately if a long or infinite loop is running,
but are not called immediately if Scheme code is waiting inside a port read.

In practice: after executing

(define (my-signal-handler int)
  (let ((port (console-error-port)))
    (format port "received signal: ~a\n" int)
    (flush-output-port port))
  ;; a full implementation would call some custom logic.
  ;; for this example, keep it simple and always call (break)
  (break))


(register-signal-handler  3 my-signal-handler) ; SIGQUIT on Linux/x86_64
(register-signal-handler 17 my-signal-handler) ; SIGCHLD on Linux/x86_64
(register-signal-handler 20 my-signal-handler) ; SIGTSTP on Linux/x86_64

a long or infinite loop can be interrupted any of:

  • CTRL+C - generates SIGINT
  • CTRL+Z - generates SIGTSTP
  • CTRL+\ - generates SIGQUIT
> (do () (#f))
CTRL+C
break>
> (do () (#f))
CTRL+Z
received signal: 20
break>

i.e. (my-signal-handler) is invoked from the infinite loop.

> (do () (#f))
CTRL+\
received signal: 3
break>

idem.

A port read instead behaves differently:

> (get-string-all (current-input-port))
CTRL+C
break>
> (get-string-all (current-input-port))
CTRL+Z ; no effect
> (get-string-all (current-input-port))
CTRL+\ ; no effect

Examining the source code, I noticed c/new-io.c:494

if (Sboolean_value(KEYBOARDINTERRUPTPENDING(tc))) {
  return Sstring("interrupt");
}

and similar checks at lines 482 and 486 for keyboard interrupt i.e. CTRL+C on POSIX systems.

Wouldn't it be more useful to always return Sstring("interrupt") if errno == EINTR ?
In this way, the Scheme side s/io.ss:996

  (define-syntax check-interrupt
    (syntax-rules ()
      [(_ e)
       (let loop ()
         (let ([x e])
           (if (eq? x 'interrupt)
               (begin ($event) (loop))
               x)))]))

would see a value 'interrupt (there's a conversion "interrupt" -> 'interrupt in the meantime)
and always invoke ($event), which checks for all pending interrupts, expired timers etc.

Doing so would allow signal handlers installed by (register-signal-handler)
to run also when a blocking C call is interrupted by any signal,
while currently only long or infinite Scheme loops can be interrupted by an arbitrary signal.

Dominant language
Scheme
Stars
7.4k
Forks
1k
Avg merge
1d 20h
Merged PRs (30d)
19

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cisco/ChezScheme

All issues in cisco/ChezScheme

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.