selfpipe

Easy safe signal handling

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version Unknown
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-22 05:29

Installation

nimble install selfpipe
choosenim install selfpipe
git clone https://github.com/tdely/selfpipe

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
selfpipe - - - - - - -

Source

Repository https://github.com/tdely/selfpipe
Homepage https://github.com/tdely/selfpipe
Registry Source nimble_official

README

selfpipe

Signal handlers are tricky and limited in what functions they can perform safely; you can not safely use non-reentrant functions or access global variables from within a signal handler. A solution to this problem is to use D. J. Bernsteins selfpipe trick.

This library implements a simple interface for using selfpipes in your projects. Set the signals you want to catch and the proc that each of the signals should trigger with addSignal, call init() to set up the non-blocking pipe and start listening for the signals, then call checkSignals() where you want to handle the signals.

import std/[posix, os]
import selfpipe

var stop: bool

proc halt() =
  stop = true

addSignal(SIGINT, halt)
if (let e = init(); e != 0):
  quit("failed to initialize selfpipe", e)
try:
  while not stop:
    sleep(1000)
    checkSignals()
finally:
  finish()