Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC Commits: 9008758c by Duncan Coutts at 2026-11-13T21:32:57+00:00 Drop use of sigprocmask in stg_sig_install It is somewhat unclear what the signal masking was trying to protect in stg_sig_install. The comment said // Block the signal until we figure out what to do There are no shared data structured between stg_sig_install and the generic_handler that it installs. In any case, masking signals does not in general work, so whatever it was doing was a false sense of security. This was the last use of sigprocmask within the RTS. This completes the fix for #27968. - - - - - 01f8fbf2 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Make startPendingSignalHandlers a per-platform, not common, API The direction of travel is that user-signal handling should be managed by the I/O manager(s). This is because signals (& win32 console events) are (a funny kind of) I/O, and also because they have to be involved in noticing signals and starting signal handlers. And how we communicate between the low level signal handler and starting the Haskell signal handler threads is also speific to the I/O manager implementation. There is no need for a common API here. These are just utilities shared between some I/O manager implementations. - - - - - b230cd53 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Unify freeSignalHandlers and finiUserSignals in RTS shutdown They were called at different stages of shutdown. Historically this might have made sense when the handler mechanism used stable pointers. Now there appears to be no good reason. On win32, freeSignalHandlers did nothing. On posix, finiUserSignals did nothing. There appears to be no dependency on the stage of shutdown. It is all local resources. - - - - - 08c06113 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Push checks on install_signal_handlers flag into signal functions Also, change the logic slightly. The docs for --install-signal-handlers say that it installs handlers for ctl-c etc. It does _not_ say that it disables the API for Haskell code to install signal handerls. Previously however the RTS startup code would guard initUserSignals behind the flag install_signal_handlers. All that code does is initalise variables properly. This meant that when user code installs a handler, it would be working with ill-defined userSignals value and an un-initialised mutex! Similarly, in the win32 console handling, it would skip initialising the hConsoleEvent, which certainly cannot lead to good outcomes. So now we only guard initDefaultHandlers and resetDefaultHandlers behind the install_signal_handlers flag. - - - - - a8ec757a by Duncan Coutts at 2026-11-13T21:34:00+00:00 Fix user guide reference to --install-signal-handlers While reviewing --install-signal-handlers docs, noticed a typo in the --generate-crash-dumps docs that referred to --install-signal-handlers when it means --install-seh-handlers. Yes I double-checked the code. - - - - - 1d15b5e5 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Split user signal functions out of posix/Signals into new file The direction of travel is that user-signal handling should be managed by the I/O manager(s). The user signal functions (i.e. to allow Haskell code to set signal handlers) should stop being first class functions called by the rest of the RTS and become utilities used by the I/O managers. Start here by splitting them out. - - - - - 1958979e by Duncan Coutts at 2026-11-13T21:34:00+00:00 Make stg_sig_install part of the IOManager API This is the external entry point for user code to install signal handlers. So it becomes part of the I/O manager API. Eventually it will dispatch to the I/O manager implementations (which will do different things), but for now it dispatches to the same old implementation, under a new name. - - - - - 736dc58b by Duncan Coutts at 2026-11-13T21:34:00+00:00 Document that nocldstop is a totally mad implicit parameter and it's part of our public API! - - - - - c8d97278 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Move user signal init and fini into the I/O manager Initially just moved into start/stopIOManager, but a later step will be to make that be part of the initialisation of each I/O manager. Why? Because they cannot have a common init if they have different data structures that need initialisation. In fact they already do have different code and data (threaded/non-threaded, windows/other), but this is fudged using CPP. - - - - - 07dc4dd5 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Push {init,fini}{UserSignals,ConsoleEvents} down into I/O managers So they become part of individual I/O managers, which will allow us to start specialising them more sanely. Rename the Win32 ones to {init,fini}ConsoleEvents. - - - - - c27c24db by Duncan Coutts at 2026-11-13T21:34:00+00:00 Push installUserSignalHandler down into individual I/O managers This will allow us to specialise them. Initally they all use the same (existing) generic_handler. - - - - - 67256a71 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Specialise MIO's installing of signals It already used a separate implementation, but using cpp via the signal "generic_handler". Now it is much more direct and local. MIO writes signals into a pipe and reads and dispatches them at the other end (in Haskell). - - - - - 43c7fc3b by Duncan Coutts at 2026-11-13T21:34:00+00:00 Introduce an I/O manager data structure shared between all caps Accessed via iomgr->shared, but it points to the same struct for all capabilities. We initialise and free this with cap 0. We will use it initially for signal handling, since only one capability needs to dispatch signals. This is a fairly general mechanism, so I/O managers may be able to make use of it for other purposes, especially once in-RTS ones are adapted to work in the threaded RTS with multiple capabilities. - - - - - 7d645e6f by Duncan Coutts at 2026-11-13T21:34:00+00:00 Use the shared I/O manager data structure for MIO for signals Move the MIO I/O manager's timer manager control fd from a global variable into the shared I/O manager struct. Soon we will use the same mechanism for the same purpose of signal handling for the other posix I/O managers. - - - - - 9766c249 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Extract and add more utility functions for the signal self-pipe trick Extract a utility function writeSignalInfoToPipe from the MIO I/O manager so we can use it in other I/O managers too. Also add related utility functions readSignalInfoFromPipe, newNonBlockingPipe and startSignalHandler. We will use these in the poll and selectbis I/O managers. - - - - - 91fa99e9 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Redo the scheme for special fds in the poll and selectbis I/O managers At the moment we only have one special fd for these I/O managers: the pipe (or eventfd) for interrupting them when they're sleeping. We're about to gain a second for signal handling. The existing scheme was rather ad-hoc and hard to extend. It was already complicated by the fact that the interrupt fd is optional. The signal fd will also be optional in the sense that cap 0 will have it but cap >0 will not. So the new scheme is that we add special aiop entries into the aiop_table, and for the most part we can treat these uniformly with other aiops. But the action we take for them if/when they are ready is handled specially. This seems much cleaner and will be easy to extend for signals. - - - - - c76c061d by Duncan Coutts at 2026-11-13T21:34:00+00:00 Use the signal self-pipe trick for the poll and selectbis I/O managers So now the low level signal handler delivers signals into a pipe that is monitored by the I/O manager (on cap 0). The I/O manager then dispatches the handler (starting a Haskell thread). There are two important reasons for this change: 1. This method does not have any windows of time where signals can be missed for arbitrary periods of time. The previous method has a race where a signal delivered just before the I/O manager calls poll/select will not notice the signal until some unrelated event causes poll/select to return. See issue #27540. The new method ensures that a fd is ready and so poll/select will notice immediately (if the signal was delivered prior to the call to poll/select) or later. 2. The existing method relies on temporarily masking signals for concurrency safety which cannot work reliably in a multi-threaded process. Fixing this is a necessary pre-requisite to enable these in-RTS I/O managers to work in the threaded RTS. The issue is that startPendingSignalHandlers and the low level signal handler share the pending_handler_buf data structure, and so startPendingSignalHandlers has to mask delivery of signals to avoid the shared data being mutated concurrently. But masking signals in a multi-threaded process is not generally effective because the signal can still be delivered in any thread Document the self-pipe trick in a Note. This fixes issue #27540. - - - - - ec5927d6 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Move the legacy user signal handler into legacy select I/O manager We do not convert the legacy select I/O manager to use the self-pipe trick. It keeps the existing code, but since it is now the only user of it, then that code can move from being a shared utility to live with the select I/O manager. There are some minor modifications to the code moved so that it is not entangled with other remaining shared signal utils. - - - - - 64f11335 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Deprecate (un)blockUserSignals for external RTS users Encourage external users to stop using these actions. - - - - - 9bd0767a by Duncan Coutts at 2026-11-13T21:34:00+00:00 Split interruptStgRts and remove wakeUpRtsViaTicker. Split the two uses of interruptStgRts into their posix and win32 cases, and move them to their use sites. Make the posix case not rely on wakeUpRts or wakeUpRtsViaTicker. The use of wakeUpRtsViaTicker while it seemed to cleverly solve overly-close coupling in one area, causes overly-close coupling in another area: ticker(s). And we want to start improving the ticker(s) but the existence of wakeUpRtsViaTicker holds that back. Delete wakeUpRtsViaTicker and its related implementation pieces. In the posix case, we add interruptIOManagerFromSignal, which is specific for the circumstance of use within a signal handler. The implementation of this for the in-RTS I/O managers is just interruptIOManager but for MIO it has to wake up the timer manager thread. All of this seems pretty unnecessary since it is all to support a ctl-c handler that is almost never used since it is duplicated and overridden by a ctl-c handler installed by runMainIO. See issue #28066. - - - - - 92625eac by Duncan Coutts at 2026-11-13T21:34:00+00:00 Simplify execution of signal handlers We make a few related changes: 1. Don't use malloc/free for passing siginfo_t from C to Haskell, use heap allocated byte arrays indead. The Haskell side already uses ForeignPtr which can use byte array directly. 2. Don't redundantly fork IO threads for signal handlers, when invoked from in-RTS I/O managers. The I/O manager already creates a new IO thread, but the existing runHandlers would then redundantly forkIO again. This is a waste of resources but also means signal handlers end up getting scheduled last. 3. Schedule signal handler threads first. This should fix #28067 Introduce new runHandler/runHandlerPtr to replace the existing runHandlers/runHandlersPtr. Conveniently the old ones have a weird name (they only invoke one handler!) so we can use a sensible name, and leave the old ones (which are exported via base!) alone. Add deprecation warnings though, since nobody should be using them via base. Update the caller in ghc-internal: the MIO timer manager. This one does need to use forkIO, but it's clearer if that forkIO is done there. - - - - - c7ea4363 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Stop using signal-unsafe functions in signal handlers Fixes #19205 Unfortunately this does mean that the SIGQUIT functionality of dumping a stack trace has to be disabled for now. There's no short term practical way to allow that to work. The stack trace functions all use dynamic memory allocation using malloc/free, which is verboten in signal handlers. See #28071 about re-enabling the stack trace info. - - - - - 77be1810 by Duncan Coutts at 2026-11-13T21:34:00+00:00 Add a changelog entry for the signals changes - - - - - 37 changed files: - + changelog.d/signals-cleanup - libraries/ghc-internal/include/RtsIfaceSymbols.h - libraries/ghc-internal/src/GHC/Internal/Conc/Signal.hs - libraries/ghc-internal/src/GHC/Internal/Event/Control.hs - libraries/ghc-internal/src/GHC/Internal/Event/TimerManager.hs - rts/IOManager.c - rts/IOManager.h - rts/IOManagerInternals.h - rts/Prelude.h - rts/RtsFlags.c - rts/RtsSignals.h - rts/RtsStartup.c - rts/RtsSymbols.c - rts/Schedule.c - rts/Ticker.h - rts/configure.ac - rts/include/Rts.h - rts/include/rts/BlockSignals.h - rts/include/rts/IOInterface.h - rts/include/rts/RtsToHsIface.h - rts/include/rts/storage/Closures.h - rts/posix/MIO.c - rts/posix/MIO.h - rts/posix/Poll.c - rts/posix/Poll.h - rts/posix/Select.c - rts/posix/Select.h - rts/posix/SelectBis.c - rts/posix/SelectBis.h - rts/posix/Signals.c - rts/posix/Ticker.c - + rts/posix/UserSignals.c - + rts/posix/UserSignals.h - rts/rts.cabal - rts/win32/AwaitEvent.c - rts/win32/ConsoleHandler.c - rts/win32/ConsoleHandler.h The diff was not included because it is too large. View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ecc9b0518a603a96c82dd6902f290ac... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ecc9b0518a603a96c82dd6902f290ac... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help